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