/**
  * Authenticates a user by its credentials
  *
  * @param AuthDTO $authDTO
  *
  * @return \Sententiaregum\User\Domain\Value\ApiKey
  *
  * @throws AuthenticationException If the username is invalid
  */
 public function createToken(AuthDTO $authDTO)
 {
     $user = $this->userRepository->findOneByName($authDTO->getUsername());
     if (!$user) {
         throw AuthenticationException::fromInvalidUsername();
     }
     try {
         $token = $user->authenticateToken($authDTO, $this->apiKeyGenerator);
     } catch (AuthenticationException $ex) {
         // modify authentication failure
         $this->userRepository->modify($user);
         throw $ex;
     }
     $this->userRepository->modify($user);
     return $token;
 }
 /**
  * Compares the credentials with a data transfer object
  *
  * @param AuthDTO $credentials
  *
  * @return boolean
  */
 public function compare(AuthDTO $credentials)
 {
     return $this->getUsername() === $credentials->getUsername() && $this->getPassword()->compare($credentials->getPassword());
 }