Completed
Pull Request — master (#246)
by Beñat
03:48
created

EditProfileHandler::__invoke()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types = 1);
14
15
namespace Kreta\IdentityAccess\Application\Command;
16
17
use BenGorFile\File\Domain\Model\FileMimeType;
18
use BenGorFile\File\Domain\Model\FileName;
19
use BenGorFile\File\Domain\Model\Filesystem;
20
use BenGorUser\User\Domain\Model\Exception\UserDoesNotExistException;
21
use BenGorUser\User\Domain\Model\UserEmail;
22
use BenGorUser\User\Domain\Model\UserId;
23
use Kreta\IdentityAccess\Domain\Model\User\FullName;
24
use Kreta\IdentityAccess\Domain\Model\User\User;
25
use Kreta\IdentityAccess\Domain\Model\User\UserEmailAlreadyExistsException;
26
use Kreta\IdentityAccess\Domain\Model\User\Username;
27
use Kreta\IdentityAccess\Domain\Model\User\UsernameAlreadyExistsException;
28
use Kreta\IdentityAccess\Domain\Model\User\UserRepository;
29
30
class EditProfileHandler
31
{
32
    private $repository;
33
    private $filesystem;
34
35
    public function __construct(UserRepository $repository, Filesystem $filesystem)
36
    {
37
        $this->repository = $repository;
38
        $this->filesystem = $filesystem;
39
    }
40
41
    public function __invoke(EditProfileCommand $command)
42
    {
43
        $id = new UserId($command->id());
44
        $email = new UserEmail($command->email());
45
        $username = new Username($command->username());
46
        $fullName = new FullName($command->firstName(), $command->lastName());
47
48
        $imageName = FileName::fromHash($command->imageName());
49
        $imageMimeType = new FileMimeType($command->imageMimeType());
50
        $uploadedImage = $command->uploadedImage();
51
52
        /** @var User $user */
53
        $user = $this->repository->userOfId($id);
54
        $this->checkUserExists($user);
55
        $this->checkEmailAndUsernameUniqueness($user, $username, $email);
56
57
        $user->editProfile($email, $username, $fullName);
58
59
        if ($uploadedImage) {
60
            $this->filesystem->write($imageName, $uploadedImage);
61
            $user->uploadImage($imageName, $imageMimeType);
62
        }
63
64
        $this->repository->persist($user);
65
    }
66
67
    private function checkUserExists(User $user = null)
68
    {
69
        if (null === $user) {
70
            throw new UserDoesNotExistException();
71
        }
72
    }
73
74
    private function checkEmailAndUsernameUniqueness(User $user, Username $username, UserEmail $email)
75
    {
76
        $this->checkUsernameExists($user, $username);
77
        $this->checkEmailExists($user, $email);
78
    }
79
80
    private function checkUsernameExists(User $user, Username $username)
81
    {
82
        $anotherUser = $this->repository->userOfUsername($username);
83
        if (null !== $anotherUser && !$user->id()->equals($anotherUser->id())) {
84
            throw new UsernameAlreadyExistsException($username);
85
        }
86
    }
87
88
    private function checkEmailExists(User $user, UserEmail $email)
89
    {
90
        $anotherUser = $this->repository->userOfEmail($email);
91
        if (null !== $anotherUser && !$user->id()->equals($anotherUser->id())) {
92
            throw new UserEmailAlreadyExistsException($email);
93
        }
94
    }
95
}
96