/** * Generate a new token and persist it into DB * * @param string $scope Purpose of this token (where will it be used) * @param string $ownerType Owner of this entity (could be an Entity class name) * @param integer $ownerId Identifier of the owner * @param integer $usesMax Maximum number of uses for this Token before it is invalidated * @param \DateTime $expiresAt Optional expiry time, after which the Token becomes invalid * @param array $data Optional additional data to check against, for advanced validation conditions * * @return Token */ public function generate($scope, $ownerType, $ownerId, $usesMax = 1, \DateTime $expiresAt = null, array $data = null) { $hash = md5($this->secureRandom->nextBytes(32) . $scope . $ownerType . $ownerId); $token = new Token(); $token->setHash($hash)->setScope($scope)->setOwnerType($ownerType)->setOwnerId($ownerId)->setUsesMax($usesMax); if (!empty($data)) { $token->setData($data); } if ($expiresAt instanceof \DateTime) { $token->setExpiresAt($expiresAt); } $this->em->persist($token); $this->em->flush($token); return $token; }