Test Failed
Push — dev ( ccede5...b27119 )
by Herberto
13:46
created

SecurityUser::isEqualTo()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Auth\Authentication;
16
17
use Acme\App\Core\Component\User\Domain\User\User;
18
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId;
19
use Acme\PhpExtension\ConstructableFromArrayInterface;
20
use Acme\PhpExtension\ConstructableFromArrayTrait;
21
use League\OAuth2\Server\Entities\Traits\EntityTrait;
22
use League\OAuth2\Server\Entities\UserEntityInterface;
23
use Symfony\Component\Security\Core\User\EquatableInterface;
24
use Symfony\Component\Security\Core\User\UserInterface;
25
26
final class SecurityUser
27
    implements UserInterface, UserEntityInterface, EquatableInterface, ConstructableFromArrayInterface
28
{
29
    use ConstructableFromArrayTrait;
30
    use EntityTrait;
31
32
    /**
33
     * @var UserId
34
     */
35
    private $userId;
36
37
    /**
38
     * @var string
39
     */
40
    private $username;
41
42
    /**
43
     * @var string
44
     */
45
    private $password;
46
47
    /**
48
     * @var string[]
49
     */
50
    private $roles;
51
52
    public function __construct(UserId $userId, string $username, string $password, array $roles = [])
53
    {
54
        $this->userId = $userId;
55
        $this->username = $username;
56
        $this->password = $password;
57
        $this->roles = $roles;
58
        $this->setIdentifier($userId->toScalar());
59
    }
60
61
    public static function fromUser(User $user): self
62
    {
63
        return new self($user->getId(), $user->getUsername(), $user->getPassword() ?? '', $user->getRoles());
64
    }
65
66
    public function getUserId(): UserId
67
    {
68
        return $this->userId;
69
    }
70
71
    public function getUsername(): string
72
    {
73
        return $this->username;
74
    }
75
76
    public function getPassword(): string
77
    {
78
        return $this->password;
79
    }
80
81
    /**
82
     * @var string[]
83
     */
84
    public function getRoles(): array
85
    {
86
        return $this->roles;
87
    }
88
89
    /**
90
     * Returns the salt that was originally used to encode the password.
91
     *
92
     * {@inheritdoc}
93
     */
94
    public function getSalt(): ?string
95
    {
96
        // See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html
97
        // we're using bcrypt in security.yml to encode the password, so
98
        // the salt value is built-in and you don't have to generate one
99
100
        return null;
101
    }
102
103
    /**
104
     * Removes sensitive data from the user.
105
     *
106
     * {@inheritdoc}
107
     */
108
    public function eraseCredentials(): void
109
    {
110
        // if you had a plainPassword property, you'd nullify it here
111
        // $this->plainPassword = null;
112
    }
113
114
    public function isEqualTo(UserInterface $user): bool
115
    {
116
        if (!$user instanceof self) {
117
            return false;
118
        }
119
120
        if ($this->password !== $user->getPassword()) {
121
            return false;
122
        }
123
124
        if ($this->getSalt() !== $user->getSalt()) {
125
            return false;
126
        }
127
128
        if ($this->username !== $user->getUsername()) {
129
            return false;
130
        }
131
132
        return true;
133
    }
134
}
135