Test Setup Failed
Push — main ( abdcb9...2e5f68 )
by Slawomir
04:37
created

UserUpdater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 9
rs 10
1
<?php
2
3
namespace App\Modules\Security\Domain\Logic;
4
5
use App\Infrastructure\Events\Api\ApplicationEventPublisherInterface;
6
use App\Modules\Security\Api\Command\ChangeUserPasswordCommand;
7
use App\Modules\Security\Api\Command\RenameUserCommand;
8
use App\Modules\Security\Domain\Dto\ChangeExistingUserPasswordDto;
9
use App\Modules\Security\Domain\Dto\RenameExistingUserDto;
10
use App\Modules\Security\Domain\Event\Outbound\UserRenamedOEvent;
11
use App\Modules\Security\Domain\Repository\UserUpdatingRepositoryInterface;
12
use App\Modules\Security\Domain\Transactions\SecurityTransactionFactoryInterface;
13
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
14
15
trait UserUpdater
16
{
17
    use PasswordHasher {
18
        PasswordHasher::__construct as __hasherConstruct;
19
    }
20
21
    public function __construct(
22
        private ApplicationEventPublisherInterface  $eventPublisher,
23
        private SecurityTransactionFactoryInterface $transactionFactory,
24
        private UserUpdatingRepositoryInterface     $updatingRepository,
25
        private SecurityValidator                   $validator,
26
        private UserPasswordHasherInterface         $passwordHasher,
27
    )
28
    {
29
        $this->__hasherConstruct($this->passwordHasher);
30
    }
31
32
    /**
33
     * @param RenameUserCommand $command
34
     */
35
    public function renameUser(RenameUserCommand $command): void
36
    {
37
        $this->validator->preRenameUser($command);
38
        $this->transactionFactory
39
            ->createTransaction(function () use ($command) {
40
                $this->updatingRepository->renameUser(
41
                    new RenameExistingUserDto($command->getLogin(), $command->getNewLogin())
42
                );
43
            })
44
            ->afterCommit(function () use ($command) {
45
                $this->eventPublisher->publish(
46
                    new UserRenamedOEvent($command->getLogin(), $command->getNewLogin())
47
                );
48
            })
49
            ->execute();
50
    }
51
52
    /**
53
     * @param ChangeUserPasswordCommand $command
54
     */
55
    public function changePassword(ChangeUserPasswordCommand $command): void
56
    {
57
        $this->validator->preChangeUserPassword($command);
58
        $this->transactionFactory
59
            ->createTransaction(function () use ($command) {
60
                $this->updatingRepository->changePassword(
61
                    new ChangeExistingUserPasswordDto(
62
                        $command->getLogin(),
63
                        $this->hashPassword($command->getPassword())
64
                    )
65
                );
66
            })
67
            ->execute();
68
    }
69
}