Passed
Push — master ( b37186...7c51ba )
by Peter
04:28
created

User::toJSON()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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