Test Failed
Push — master ( de46cf...ec82a1 )
by Alexander
03:16
created

AuthService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 43
ccs 15
cts 15
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentity() 0 3 1
A logout() 0 10 2
A login() 0 9 3
A isGuest() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth;
6
7
use App\User\UserRepository;
8
use Throwable;
9
use Yiisoft\Auth\IdentityInterface;
10
use Yiisoft\User\CurrentUser;
11
12
final class AuthService
13
{
14
    public function __construct(
15
        private CurrentUser $currentUser,
16
        private UserRepository $userRepository,
17
        private IdentityRepository $identityRepository,
18
    ) {
19 10
    }
20
21
    public function login(string $login, string $password): bool
22
    {
23
        $user = $this->userRepository->findByLoginWithAuthIdentity($login);
24 10
25 10
        if ($user === null || !$user->validatePassword($password)) {
26 10
            return false;
27
        }
28
29 4
        return $this->currentUser->login($user->getIdentity());
30
    }
31 4
32
    /**
33 4
     * @throws Throwable
34 3
     */
35
    public function logout(): bool
36
    {
37 1
        $identity = $this->currentUser->getIdentity();
38
39
        if ($identity instanceof Identity) {
40
            $identity->regenerateCookieLoginKey();
41
            $this->identityRepository->save($identity);
42
        }
43 1
44
        return $this->currentUser->logout();
45 1
    }
46
47 1
    public function getIdentity(): IdentityInterface
48 1
    {
49 1
        return $this->currentUser->getIdentity();
50
    }
51
52 1
    public function isGuest(): bool
53
    {
54
        return $this->currentUser->isGuest();
55
    }
56
}
57