/**
  * @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;
 }
 /**
  * @param $userId
  * @param $tokenString
  * @return AccessToken|null
  */
 protected function findAccessToken($userId, $tokenString)
 {
     $qb = $this->accessTokenRepository->createQueryBuilder('a');
     $query = $qb->select()->innerJoin('a.user', 'u', 'WITH', 'u.id = :user_id')->where('a.token = :token_string')->setParameter('user_id', $userId)->setParameter('token_string', $tokenString)->getQuery();
     return $query->getOneOrNullResult();
 }