UserService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 53
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A logout() 0 4 1
A login() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use App\Exception\BadRequestException;
8
use App\Queue\LoggingAuthorizationHandler;
9
use App\Queue\UserLoggedInMessage;
10
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...
11
use Yiisoft\Auth\IdentityRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Auth\IdentityRepositoryInterface 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...
12
use Yiisoft\Definitions\Exception\InvalidConfigException;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Definitions\Exce...\InvalidConfigException 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...
13
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...
14
use Yiisoft\Queue\QueueFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Queue\QueueFactoryInterface 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...
15
16
final class UserService
17
{
18
    private IdentityRepositoryInterface $identityRepository;
19
    private CurrentUser $currentUser;
20
    private QueueFactoryInterface $queueFactory;
21
22
    public function __construct(
23
        CurrentUser $currentUser,
24
        IdentityRepositoryInterface $identityRepository,
25
        QueueFactoryInterface $queueFactory
26
    ) {
27
        $this->currentUser = $currentUser;
28
        $this->identityRepository = $identityRepository;
29
        $this->queueFactory = $queueFactory;
30
    }
31
32
    /**
33
     * @param string $login
34
     * @param string $password
35
     *
36
     * @throws InvalidConfigException
37
     * @throws BadRequestException
38
     *
39
     * @return IdentityInterface
40
     */
41
    public function login(string $login, string $password): IdentityInterface
42
    {
43
        $identity = $this->identityRepository->findByLogin($login);
0 ignored issues
show
Bug Best Practice introduced by
The property identityRepository does not exist on App\User\UserService. Did you maybe forget to declare it?
Loading history...
44
        if ($identity === null) {
45
            throw new BadRequestException('No such user.');
46
        }
47
48
        if (!$identity->validatePassword($password)) {
49
            throw new BadRequestException('Invalid password.');
50
        }
51
52
        if (!$this->currentUser->login($identity)) {
53
            throw new BadRequestException();
54
        }
55
56
        $identity->resetToken();
57
        $this->identityRepository->save($identity);
58
59
        $queueMessage = new UserLoggedInMessage($identity->getId(), time());
60
        $this->queueFactory->get(LoggingAuthorizationHandler::CHANNEL)->push($queueMessage);
0 ignored issues
show
Bug Best Practice introduced by
The property queueFactory does not exist on App\User\UserService. Did you maybe forget to declare it?
Loading history...
61
62
        return $identity;
63
    }
64
65
    public function logout(User $user): void
66
    {
67
        $user->resetToken();
0 ignored issues
show
Bug introduced by
The method resetToken() does not exist on App\User\User. ( Ignorable by Annotation )

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

67
        $user->/** @scrutinizer ignore-call */ 
68
               resetToken();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        $this->identityRepository->save($user);
0 ignored issues
show
Bug Best Practice introduced by
The property identityRepository does not exist on App\User\UserService. Did you maybe forget to declare it?
Loading history...
69
    }
70
}
71