Passed
Pull Request — master (#1620)
by Tarmo
63:23
created

User::setUserGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Rest/DTO/User/User.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\DTO\User;
10
11
use App\DTO\RestDto;
12
use App\Entity\Interfaces\EntityInterface;
13
use App\Entity\Interfaces\UserGroupAwareInterface;
14
use App\Entity\User as Entity;
15
use App\Entity\UserGroup as UserGroupEntity;
16
use App\Enum\Language;
0 ignored issues
show
Bug introduced by
The type App\Enum\Language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use App\Enum\Locale;
0 ignored issues
show
Bug introduced by
The type App\Enum\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use App\Service\Localization;
19
use App\Validator\Constraints as AppAssert;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use function array_map;
22
23
/**
24
 * Class User
25
 *
26
 * @package App\DTO\User
27
 * @author TLe, Tarmo Leppänen <[email protected]>
28
 *
29
 * @method Entity|EntityInterface update(EntityInterface $entity)
30
 */
31
#[AppAssert\UniqueEmail]
32
#[AppAssert\UniqueUsername]
33
class User extends RestDto
34
{
35
    /**
36
     * @var array<string, string>
37
     */
38
    protected static array $mappings = [
39
        'password' => 'updatePassword',
40
        'userGroups' => 'updateUserGroups',
41
    ];
42
43
    #[Assert\NotBlank]
44
    #[Assert\NotNull]
45
    #[Assert\Length(min: 2, max: 255)]
46
    protected string $username = '';
47
48
    #[Assert\NotBlank]
49
    #[Assert\NotNull]
50
    #[Assert\Length(min: 2, max: 255)]
51
    protected string $firstName = '';
52
53
    #[Assert\NotBlank]
54
    #[Assert\NotNull]
55
    #[Assert\Length(min: 2, max: 255)]
56
    protected string $lastName = '';
57
58
    #[Assert\NotBlank]
59
    #[Assert\NotNull]
60
    #[Assert\Email]
61
    protected string $email = '';
62
63
    #[Assert\NotBlank]
64
    #[Assert\NotNull]
65
    #[AppAssert\Language]
66
    protected Language $language;
67
68
    #[Assert\NotBlank]
69
    #[Assert\NotNull]
70
    #[AppAssert\Locale]
71
    protected Locale $locale;
72
73
    #[Assert\NotBlank]
74
    #[Assert\NotNull]
75
    #[AppAssert\Timezone]
76
    protected string $timezone = Localization::DEFAULT_TIMEZONE;
77
78
    /**
79
     * @var UserGroupEntity[]|array<int, UserGroupEntity>
80
     */
81
    #[AppAssert\EntityReferenceExists(entityClass: UserGroupEntity::class)]
82
    protected array $userGroups = [];
83
84 9
    protected string $password = '';
85
86 9
    public function __construct()
87
    {
88
        $this->language = Language::getDefault();
89 21
        $this->locale = Locale::getDefault();
90
    }
91 21
92
    public function getUsername(): string
93 21
    {
94
        return $this->username;
95 21
    }
96
97
    public function setUsername(string $username): self
98 8
    {
99
        $this->setVisited('username');
100 8
101
        $this->username = $username;
102
103 19
        return $this;
104
    }
105 19
106
    public function getFirstName(): string
107 19
    {
108
        return $this->firstName;
109 19
    }
110
111
    public function setFirstName(string $firstName): self
112 8
    {
113
        $this->setVisited('firstName');
114 8
115
        $this->firstName = $firstName;
116
117 19
        return $this;
118
    }
119 19
120
    public function getLastName(): string
121 19
    {
122
        return $this->lastName;
123 19
    }
124
125
    public function setLastName(string $lastName): self
126 11
    {
127
        $this->setVisited('lastName');
128 11
129
        $this->lastName = $lastName;
130
131 23
        return $this;
132
    }
133 23
134
    public function getEmail(): string
135 23
    {
136
        return $this->email;
137 23
    }
138
139
    public function setEmail(string $email): self
140 6
    {
141
        $this->setVisited('email');
142 6
143
        $this->email = $email;
144
145 11
        return $this;
146
    }
147 11
148
    public function getLanguage(): Language
149 11
    {
150
        return $this->language;
151 11
    }
152
153
    public function setLanguage(Language $language): self
154 7
    {
155
        $this->setVisited('language');
156 7
157
        $this->language = $language;
158
159 12
        return $this;
160
    }
161 12
162
    public function getLocale(): Locale
163 12
    {
164
        return $this->locale;
165 12
    }
166
167
    public function setLocale(Locale $locale): self
168 6
    {
169
        $this->setVisited('locale');
170 6
171
        $this->locale = $locale;
172
173 11
        return $this;
174
    }
175 11
176
    public function getTimezone(): string
177 11
    {
178
        return $this->timezone;
179 11
    }
180
181
    public function setTimezone(string $timezone): self
182
    {
183
        $this->setVisited('timezone');
184
185 10
        $this->timezone = $timezone;
186
187 10
        return $this;
188
    }
189
190
    /**
191
     * @return array<int, UserGroupEntity>
192
     */
193 16
    public function getUserGroups(): array
194
    {
195 16
        return $this->userGroups;
196
    }
197 16
198
    /**
199 16
     * @param array<int, UserGroupEntity> $userGroups
200
     */
201
    public function setUserGroups(array $userGroups): self
202 5
    {
203
        $this->setVisited('userGroups');
204 5
205
        $this->userGroups = $userGroups;
206
207 12
        return $this;
208
    }
209 12
210 12
    public function getPassword(): string
211
    {
212 12
        return $this->password;
213
    }
214
215 12
    public function setPassword(?string $password = null): self
216
    {
217
        if ($password !== null) {
218
            $this->setVisited('password');
219
220
            $this->password = $password;
221
        }
222
223 15
        return $this;
224
    }
225 15
226 15
    /**
227 15
     * {@inheritdoc}
228 15
     *
229 15
     * @param EntityInterface|Entity $entity
230 15
     */
231 15
    public function load(EntityInterface $entity): self
232 15
    {
233 15
        if ($entity instanceof Entity) {
234
            $this->id = $entity->getId();
235
            $this->username = $entity->getUsername();
236 15
            $this->firstName = $entity->getFirstName();
237
            $this->lastName = $entity->getLastName();
238 15
            $this->email = $entity->getEmail();
239
            $this->language = $entity->getLanguage();
240
            $this->locale = $entity->getLocale();
241 15
            $this->timezone = $entity->getTimezone();
242
243
            /** @var array<int, UserGroupEntity> $groups */
244
            $groups = $entity->getUserGroups()->toArray();
245
246
            $this->userGroups = $groups;
247 3
        }
248
249 3
        return $this;
250
    }
251 3
252
    /**
253
     * Method to update User entity password.
254
     */
255
    protected function updatePassword(Entity $entity, string $value): self
256
    {
257
        $entity->setPlainPassword($value);
258
259 2
        return $this;
260
    }
261 2
262
    /**
263 2
     * Method to update User entity user groups.
264 2
     *
265
     * @param array<int, UserGroupEntity> $value
266
     */
267
    protected function updateUserGroups(UserGroupAwareInterface $entity, array $value): self
268 2
    {
269
        $entity->clearUserGroups();
270
271
        array_map(
272
            static fn (UserGroupEntity $userGroup): UserGroupAwareInterface => $entity->addUserGroup($userGroup),
273
            $value,
274
        );
275
276
        return $this;
277
    }
278
}
279