User::setUsername()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Domain\Entities;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
9
class User implements IStringerEntity
10
{
11
    protected string $id;
12
13
    protected string $username;
14
15
    protected string $email;
16
17
    protected string $password;
18
19
    protected bool $canLogin;
20
21
    protected bool $isGravatarAllowed;
22
23
    protected UserLanguage $userLanguage;
24
25
    /** @var UserGroup[] */
26
    protected array $userGroups;
27
28
    /**
29
     * User constructor.
30
     *
31
     * @param string       $id
32
     * @param string       $username
33
     * @param string       $email
34
     * @param string       $password
35
     * @param bool         $canLogin
36
     * @param bool         $isGravatarAllowed
37
     * @param UserLanguage $userLanguage
38
     * @param UserGroup[]  $userGroups
39
     */
40
    public function __construct(
41
        string $id,
42
        string $username,
43
        string $email,
44
        string $password,
45
        bool $canLogin,
46
        bool $isGravatarAllowed,
47
        UserLanguage $userLanguage,
48
        array $userGroups = []
49
    ) {
50
        $this->id                = $id;
51
        $this->username          = $username;
52
        $this->email             = $email;
53
        $this->password          = $password;
54
        $this->canLogin          = $canLogin;
55
        $this->isGravatarAllowed = $isGravatarAllowed;
56
        $this->userLanguage      = $userLanguage;
57
58
        $this->setUserGroups($userGroups);
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getId()
65
    {
66
        return $this->id;
67
    }
68
69
    /**
70
     * @param string $id
71
     */
72
    public function setId($id)
73
    {
74
        $this->id = $id;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getUsername(): string
81
    {
82
        return $this->username;
83
    }
84
85
    /**
86
     * @param string $username
87
     *
88
     * @return $this
89
     */
90
    public function setUsername(string $username): User
91
    {
92
        $this->username = $username;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getEmail(): string
101
    {
102
        return $this->email;
103
    }
104
105
    /**
106
     * @param string $email
107
     *
108
     * @return $this
109
     */
110
    public function setEmail(string $email): User
111
    {
112
        $this->email = $email;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getPassword(): string
121
    {
122
        return $this->password;
123
    }
124
125
    /**
126
     * @param string $password
127
     *
128
     * @return $this
129
     */
130
    public function setPassword(string $password): User
131
    {
132
        $this->password = $password;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    public function canLogin(): bool
141
    {
142
        return $this->canLogin;
143
    }
144
145
    /**
146
     * @param bool $canLogin
147
     *
148
     * @return $this
149
     */
150
    public function setCanLogin(bool $canLogin): User
151
    {
152
        $this->canLogin = $canLogin;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function isGravatarAllowed(): bool
161
    {
162
        return $this->isGravatarAllowed;
163
    }
164
165
    /**
166
     * @param bool $isGravatarAllowed
167
     *
168
     * @return $this
169
     */
170
    public function setIsGravatarAllowed(bool $isGravatarAllowed): User
171
    {
172
        $this->isGravatarAllowed = $isGravatarAllowed;
173
174
        return $this;
175
    }
176
177
    /**
178
     * @return UserLanguage
179
     */
180
    public function getUserLanguage(): UserLanguage
181
    {
182
        return $this->userLanguage;
183
    }
184
185
    /**
186
     * @param UserLanguage $userLanguage
187
     *
188
     * @return $this
189
     */
190
    public function setUserLanguage(UserLanguage $userLanguage): User
191
    {
192
        $this->userLanguage = $userLanguage;
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return UserGroup[]
199
     */
200
    public function getUserGroups(): array
201
    {
202
        return $this->userGroups;
203
    }
204
205
    /**
206
     * @param UserGroup[] $userGroups
207
     *
208
     * @return $this
209
     */
210
    public function setUserGroups(array $userGroups): User
211
    {
212
        foreach ($userGroups as $userGroup) {
213
            assert($userGroup instanceof UserGroup, new \InvalidArgumentException());
214
        }
215
216
        $this->userGroups = $userGroups;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function __toString(): string
225
    {
226
        return $this->getUsername();
227
    }
228
229
    /**
230
     * @return array|null
231
     */
232
    public function toData(): ?array
233
    {
234
        $userGroupIds = [];
235
        foreach ($this->getUserGroups() as $userGroup) {
236
            $userGroupIds[] = $userGroup->getId();
237
        }
238
239
        $userLanguageId = $this->getUserLanguage()->getId();
240
241
        return [
242
            "id"                  => $this->getId(),
243
            "username"            => $this->getUsername(),
244
            "email"               => $this->getEmail(),
245
            "can_login"           => $this->canLogin(),
246
            "is_gravatar_allowed" => $this->isGravatarAllowed(),
247
            "user_group_ids"      => $userGroupIds,
248
            "user_language_id"    => $userLanguageId,
249
        ];
250
    }
251
252
    /**
253
     * @return string
254
     */
255
    public function toJSON(): string
256
    {
257
        return json_encode($this->toData());
258
    }
259
}
260