/**
  * @param string $username
  * @param string $password
  * @return AccessToken|null
  */
 public function invoke($username, $password)
 {
     $user = $this->findUser($username);
     if (!$user) {
         return false;
     }
     if ($user->getEncodedPassword() !== $this->passwordEncoder->encode($password)) {
         return false;
     }
     $token = $this->tokenGenerator->generate();
     $period = $this->now() + self::PERIOD_HOURS * 3600;
     $accessToken = new AccessToken();
     $accessToken->setUser($user)->setToken($token)->setPeriod($period);
     $this->accessTokenRepository->add($accessToken);
     $this->entityManager->flush();
     return $accessToken;
 }
 /**
  * @test
  */
 public function generate()
 {
     $generator = new TokenGenerator();
     $this->assertRegExp('/^[a-z0-9\\.\\/]{40}$/i', $generator->generate());
 }