AuthService::logout()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Auth\IdentityInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Yiisoft\User\CurrentUser;
0 ignored issues
show
Bug introduced by
The type Yiisoft\User\CurrentUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
final class AuthService
13
{
14
    public function __construct(
15
        private CurrentUser $currentUser,
16
        private UserRepository $userRepository,
17
        private IdentityRepository $identityRepository,
18
    ) {
19
    }
20
21
    public function login(string $login, string $password): bool
22
    {
23
        $user = $this->userRepository->findByLoginWithAuthIdentity($login);
24
25
        if ($user === null || !$user->validatePassword($password)) {
26
            return false;
27
        }
28
29
        return $this->currentUser->login($user->getIdentity());
30
    }
31
32
    /**
33
     * @throws Throwable
34
     */
35
    public function logout(): bool
36
    {
37
        $identity = $this->currentUser->getIdentity();
38
39
        if ($identity instanceof Identity) {
40
            $identity->regenerateCookieLoginKey();
41
            $this->identityRepository->save($identity);
42
        }
43
44
        return $this->currentUser->logout();
45
    }
46
47
    public function getIdentity(): IdentityInterface
48
    {
49
        return $this->currentUser->getIdentity();
50
    }
51
52
    public function isGuest(): bool
53
    {
54
        return $this->currentUser->isGuest();
55
    }
56
}
57