| Total Complexity | 6 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 13 | final class UserService |
||
| 14 | { |
||
| 15 | private IdentityRepositoryInterface $identityRepository; |
||
| 16 | private CurrentUser $currentUser; |
||
| 17 | |||
| 18 | public function __construct(CurrentUser $currentUser, IdentityRepositoryInterface $identityRepository) |
||
| 19 | { |
||
| 20 | $this->currentUser = $currentUser; |
||
| 21 | $this->identityRepository = $identityRepository; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $login |
||
| 26 | * @param string $password |
||
| 27 | * |
||
| 28 | * @throws BadRequestException |
||
| 29 | */ |
||
| 30 | public function login(string $login, string $password): IdentityInterface |
||
| 31 | { |
||
| 32 | $identity = $this->identityRepository->findByLogin($login); |
||
| 33 | if ($identity === null) { |
||
| 34 | throw new BadRequestException('No such user.'); |
||
| 35 | } |
||
| 36 | |||
| 37 | if (!$identity->validatePassword($password)) { |
||
| 38 | throw new BadRequestException('Invalid password.'); |
||
| 39 | } |
||
| 40 | |||
| 41 | if (!$this->currentUser->login($identity)) { |
||
| 42 | throw new BadRequestException(); |
||
| 43 | } |
||
| 44 | |||
| 45 | $identity->resetToken(); |
||
| 46 | $this->identityRepository->save($identity); |
||
| 47 | return $identity; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function logout(User $user): void |
||
| 54 | } |
||
| 55 | } |
||
| 56 |