Passed
Pull Request — master (#572)
by Alexander
02:59 queued 01:28
created

UserService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Application\User\Service;
6
7
use App\Application\Exception\BadRequestException;
8
use App\Application\User\Entity\User;
9
use App\Queue\LoggingAuthorizationHandler;
10
use App\Queue\UserLoggedInMessage;
11
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...
12
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...
13
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...
14
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...
15
use Yiisoft\Yii\Queue\QueueFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\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...
16
17
final class UserService
18
{
19
    private IdentityRepositoryInterface $identityRepository;
20
    private CurrentUser $currentUser;
21
    private QueueFactoryInterface $queueFactory;
22
23
    public function __construct(
24
        CurrentUser $currentUser,
25
        IdentityRepositoryInterface $identityRepository,
26
        QueueFactoryInterface $queueFactory
27
    ) {
28
        $this->currentUser = $currentUser;
29
        $this->identityRepository = $identityRepository;
30
        $this->queueFactory = $queueFactory;
31
    }
32
33
    /**
34
     * @param string $login
35
     * @param string $password
36
     *
37
     * @throws InvalidConfigException
38
     * @throws BadRequestException
39
     *
40
     * @return IdentityInterface
41
     */
42
    public function login(string $login, string $password): IdentityInterface
43
    {
44
        $identity = $this->identityRepository->findByLogin($login);
45
        if ($identity === null) {
46
            throw new BadRequestException('No such user.');
47
        }
48
49
        if (!$identity->validatePassword($password)) {
50
            throw new BadRequestException('Invalid password.');
51
        }
52
53
        if (!$this->currentUser->login($identity)) {
54
            throw new BadRequestException();
55
        }
56
57
        $identity->resetToken();
58
        $this->identityRepository->save($identity);
59
60
        $queueMessage = new UserLoggedInMessage($identity->getId(), time());
61
        $this->queueFactory->get(LoggingAuthorizationHandler::CHANNEL)->push($queueMessage);
62
63
        return $identity;
64
    }
65
66
    public function logout(User $user): void
67
    {
68
        $user->resetToken();
69
        $this->identityRepository->save($user);
70
    }
71
}
72