/**
  * @param User|null      $user
  * @param string         $type
  * @param \DateTime|null $expiration
  * @param mixed|null     $data
  *
  * @return Token
  */
 public function create(User $user = null, $type, \DateTime $expiration = null, $data = null)
 {
     $this->removeExpiredTokens();
     $n = 0;
     do {
         if ($n++ > 1024) {
             throw new \RuntimeException('Unable to create a token.');
         }
         $value = $this->random->generateString(32, self::LETTERS_AND_NUMBERS);
         $found = null !== $this->om->getRepository('Phraseanet:Token')->find($value);
     } while ($found);
     $token = new Token();
     $token->setUser($user)->setType($type)->setValue($value)->setExpiration($expiration)->setData($data);
     $this->om->persist($token);
     $this->om->flush();
     return $token;
 }
 /**
  * {@inheritdoc}
  */
 public function getRepository()
 {
     return $this->om->getRepository('Phraseanet:Task');
 }