Passed
Pull Request — master (#45)
by Alexander
03:38
created

UserService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 2
b 0
f 0
dl 0
loc 41
ccs 15
cts 18
cp 0.8333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A logout() 0 4 1
A login() 0 18 4
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use App\Exception\BadRequestException;
8
use Yiisoft\Auth\IdentityInterface;
9
use Yiisoft\Auth\IdentityRepositoryInterface;
10
use Yiisoft\User\CurrentUser;
11
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 2
    }
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);
0 ignored issues
show
Bug introduced by
The method findByLogin() does not exist on Yiisoft\Auth\IdentityRepositoryInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityRepositoryInterface such as App\User\UserRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        /** @scrutinizer ignore-call */ 
32
        $identity = $this->identityRepository->findByLogin($login);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method save() does not exist on Yiisoft\Auth\IdentityRepositoryInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityRepositoryInterface such as App\User\UserRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        $this->identityRepository->/** @scrutinizer ignore-call */ 
46
                                   save($identity);
Loading history...
46 1
        return $identity;
47
    }
48
49 1
    public function logout(User $user): void
50
    {
51 1
        $user->resetToken();
52 1
        $this->identityRepository->save($user);
53 1
    }
54
}
55