User::getMobile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Core\Component\User\Domain\User;
16
17
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId;
18
19
/**
20
 * Defines the properties of the User entity to represent the application users.
21
 * See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class.
22
 *
23
 * Tip: if you have an existing database, you can generate these entity class automatically.
24
 * See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
25
 *
26
 * @author Ryan Weaver <[email protected]>
27
 * @author Javier Eguiluz <[email protected]>
28
 * @author Herberto Graca <[email protected]>
29
 */
30
class User implements \Serializable
31
{
32
    public const ROLE_ADMIN = 'ROLE_ADMIN';
33
    public const ROLE_EDITOR = 'ROLE_EDITOR';
34
35
    /**
36
     * @var UserId
37
     */
38
    private $id;
39
40
    /**
41
     * @var string
42
     */
43
    private $fullName;
44
45
    /**
46
     * @var string
47
     */
48
    private $username;
49
50
    /**
51
     * @var string
52
     */
53
    private $email;
54
55
    /**
56
     * @var string
57
     */
58
    private $mobile;
59
60
    /**
61
     * @var string
62
     */
63
    private $password;
64
65
    /**
66
     * @var array
67
     */
68
    private $roles = [];
69
70
    private function __construct()
71
    {
72
        $this->id = new UserId();
73
    }
74
75
    public static function constructWithoutPassword(
76
        string $username,
77
        string $email,
78
        string $mobile,
79
        string $fullName,
80
        string $role
81
    ): self {
82
        $user = new self();
83
        $user->setFullName($fullName);
84
        $user->setUsername($username);
85
        $user->setEmail($email);
86
        $user->setMobile($mobile);
87
        $user->setRoles([$role]);
88
89
        return $user;
90
    }
91
92
    public function getId(): UserId
93
    {
94
        return $this->id;
95
    }
96
97
    public function setFullName(string $fullName): void
98
    {
99
        $this->fullName = $fullName;
100
    }
101
102
    public function getFullName(): string
103
    {
104
        return $this->fullName;
105
    }
106
107
    public function getUsername(): string
108
    {
109
        return $this->username;
110
    }
111
112
    public function setUsername(string $username): void
113
    {
114
        $this->username = $username;
115
    }
116
117
    public function getEmail(): string
118
    {
119
        return $this->email;
120
    }
121
122
    public function setEmail(string $email): void
123
    {
124
        $this->email = $email;
125
    }
126
127
    public function getMobile(): string
128
    {
129
        return $this->mobile;
130
    }
131
132
    public function hasMobile(): bool
133
    {
134
        return !empty($this->mobile);
135
    }
136
137
    public function setMobile(string $mobile): void
138
    {
139
        $this->mobile = $mobile;
140
    }
141
142
    public function getPassword(): ?string
143
    {
144
        return $this->password;
145
    }
146
147
    public function setPassword(string $password): void
148
    {
149
        $this->password = $password;
150
    }
151
152
    /**
153
     * Returns the roles or permissions granted to the user for security.
154
     */
155
    public function getRoles(): array
156
    {
157
        $roles = $this->roles;
158
159
        // guarantees that a user always has at least one role for security
160
        if (empty($roles)) {
161
            $roles[] = self::ROLE_EDITOR;
162
        }
163
164
        return array_unique($roles);
165
    }
166
167
    public function setRoles(array $roles): void
168
    {
169
        $this->roles = $roles;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function serialize(): string
176
    {
177
        // add $this->salt too if you don't use Bcrypt or Argon2i
178
        return serialize([$this->id, $this->username, $this->password]);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function unserialize($serialized): void
185
    {
186
        // add $this->salt too if you don't use Bcrypt or Argon2i
187
        [$this->id, $this->username, $this->password] = unserialize($serialized, [UserId::class]);
188
    }
189
190
    public function isAdmin(): bool
191
    {
192
        foreach ($this->getRoles() as $role) {
193
            if ($role === self::ROLE_ADMIN) {
194
                return true;
195
            }
196
        }
197
198
        return false;
199
    }
200
}
201