Passed
Push — master ( 80fed1...32ef61 )
by Peter
09:46
created

User::addEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Form\Factory;
6
7
use AbterPhp\Admin\Domain\Entities\User as Entity;
8
use AbterPhp\Admin\Domain\Entities\UserGroup;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Admin\Form\Factory\UserGroup. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use AbterPhp\Admin\Domain\Entities\UserLanguage;
10
use AbterPhp\Admin\Orm\UserGroupRepo;
11
use AbterPhp\Admin\Orm\UserLanguageRepo;
12
use AbterPhp\Framework\Constant\Html5;
13
use AbterPhp\Framework\Form\Component\Option;
14
use AbterPhp\Framework\Form\Container\CheckboxGroup;
15
use AbterPhp\Framework\Form\Container\FormGroup;
16
use AbterPhp\Framework\Form\Element\Input;
17
use AbterPhp\Framework\Form\Element\MultiSelect;
18
use AbterPhp\Framework\Form\Element\Select;
19
use AbterPhp\Framework\Form\Extra\Help;
20
use AbterPhp\Framework\Form\IForm;
21
use AbterPhp\Framework\Form\Label\Label;
22
use AbterPhp\Framework\Html\Component;
23
use AbterPhp\Framework\I18n\ITranslator;
24
use Opulence\Orm\IEntity;
25
use Opulence\Sessions\ISession;
26
27
class User extends Base
28
{
29
    /** @var UserGroupRepo */
30
    protected $userGroupRepo;
31
32
    /** @var UserLanguageRepo */
33
    protected $userLanguageRepo;
34
35
    /**
36
     * User constructor.
37
     *
38
     * @param ISession         $session
39
     * @param ITranslator      $translator
40
     * @param UserGroupRepo    $userGroupRepo
41
     * @param UserLanguageRepo $userLanguageRepo
42
     */
43
    public function __construct(
44
        ISession $session,
45
        ITranslator $translator,
46
        UserGroupRepo $userGroupRepo,
47
        UserLanguageRepo $userLanguageRepo
48
    ) {
49
        parent::__construct($session, $translator);
50
51
        $this->userGroupRepo    = $userGroupRepo;
52
        $this->userLanguageRepo = $userLanguageRepo;
53
    }
54
55
    /**
56
     * @param string       $action
57
     * @param string       $method
58
     * @param string       $showUrl
59
     * @param IEntity|null $entity
60
     *
61
     * @return IForm
62
     */
63
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
64
    {
65
        assert($entity instanceof Entity, new \InvalidArgumentException());
66
67
        $this->createForm($action, $method)
68
            ->addDefaultElements()
69
            ->addJsOnly()
70
            ->addUsername($entity)
71
            ->addEmail($entity)
72
            ->addPassword()
73
            ->addPasswordConfirmed()
74
            ->addRawPassword($entity)
75
            ->addRawPasswordConfirmed($entity)
76
            ->addCanLogin($entity)
77
            ->addIsGravatarAllowed($entity)
78
            ->addUserGroups($entity)
79
            ->addUserLanguages($entity)
80
            ->addDefaultButtons($showUrl);
81
82
        $form = $this->form;
83
84
        $this->form = null;
85
86
        return $form;
87
    }
88
89
    /**
90
     * @return $this
91
     */
92
    protected function addJsOnly(): User
93
    {
94
        $content    = sprintf(
95
            '<i class="material-icons">warning</i>&nbsp;%s',
96
            $this->translator->translate('admin:jsOnly')
97
        );
98
        $attributes = [Html5::ATTR_CLASS => 'only-js-form-warning'];
99
100
        $this->form[] = new Component($content, [], $attributes, Html5::TAG_P);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param Entity $entity
107
     *
108
     * @return $this
109
     */
110
    protected function addUsername(Entity $entity): User
111
    {
112
        $input = new Input(
113
            'username',
114
            'username',
115
            $entity->getUsername(),
116
            [],
117
            [Html5::ATTR_NAME => [Input::AUTOCOMPLETE_OFF]]
118
        );
119
        $label = new Label('username', 'admin:userUsername');
120
121
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]);
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param Entity $entity
128
     *
129
     * @return $this
130
     */
131
    protected function addEmail(Entity $entity): User
132
    {
133
        $input = new Input(
134
            'email',
135
            'email',
136
            $entity->getEmail(),
137
            [],
138
            [Html5::ATTR_AUTOCOMPLETE => [Input::AUTOCOMPLETE_OFF], Html5::ATTR_TYPE => Input::TYPE_EMAIL]
139
        );
140
        $label = new Label('email', 'admin:userEmail');
141
142
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]);
143
144
        return $this;
145
    }
146
147
    /**
148
     * @return $this
149
     */
150
    protected function addPassword(): User
151
    {
152
        $this->form[] = new Input(
153
            'password',
154
            'password',
155
            '',
156
            [],
157
            [Html5::ATTR_TYPE => [Input::TYPE_HIDDEN]]
158
        );
159
160
        return $this;
161
    }
162
163
    /**
164
     * @return $this
165
     */
166
    protected function addPasswordConfirmed(): User
167
    {
168
        $this->form[] = new Input(
169
            'password_confirmed',
170
            'password_confirmed',
171
            '',
172
            [],
173
            [Html5::ATTR_TYPE => [Input::TYPE_HIDDEN]]
174
        );
175
176
        return $this;
177
    }
178
179
    /**
180
     * @param Entity $entity
181
     *
182
     * @return $this
183
     */
184
    protected function addRawPassword(Entity $entity): User
185
    {
186
        $input = new Input(
187
            'raw_password',
188
            'raw_password',
189
            '',
190
            [],
191
            [
192
                Html5::ATTR_NAME => [Input::AUTOCOMPLETE_OFF],
193
                Html5::ATTR_TYPE => [Input::TYPE_PASSWORD],
194
            ]
195
        );
196
        $label = new Label('raw_password', 'admin:userPassword');
197
198
        $class = $entity->getId() ? '' : FormGroup::CLASS_REQUIRED;
199
200
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => $class]);
201
202
        return $this;
203
    }
204
205
    /**
206
     * @param Entity $entity
207
     *
208
     * @return $this
209
     */
210
    protected function addRawPasswordConfirmed(Entity $entity): User
211
    {
212
        $input = new Input(
213
            'raw_password_confirmed',
214
            'raw_password_confirmed',
215
            '',
216
            [],
217
            [
218
                Html5::ATTR_NAME => [Input::AUTOCOMPLETE_OFF],
219
                Html5::ATTR_TYPE => [Input::TYPE_PASSWORD],
220
            ]
221
        );
222
        $label = new Label('raw_password_confirmed', 'admin:userConfirmPassword');
223
224
        $class = $entity->getId() ? '' : FormGroup::CLASS_REQUIRED;
225
226
        $this->form[] = new FormGroup($input, $label, null, [], [Html5::ATTR_CLASS => $class]);
227
228
        return $this;
229
    }
230
231
    /**
232
     * @param Entity $entity
233
     *
234
     * @return $this
235
     */
236
    protected function addCanLogin(Entity $entity): User
237
    {
238
        $attributes = [Html5::ATTR_TYPE => [Input::TYPE_CHECKBOX]];
239
        if ($entity->canLogin()) {
240
            $attributes[Html5::ATTR_CHECKED] = null;
241
        }
242
        $input = new Input(
243
            'can_login',
244
            'can_login',
245
            '1',
246
            [],
247
            $attributes
248
        );
249
        $label = new Label('can_login', 'admin:userCanLogin');
250
        $help  = new Help('admin:userCanLoginHelp');
251
252
        $this->form[] = new CheckboxGroup($input, $label, $help);
253
254
        return $this;
255
    }
256
257
    /**
258
     * @param Entity $entity
259
     *
260
     * @return $this
261
     */
262
    protected function addIsGravatarAllowed(Entity $entity): User
263
    {
264
        $attributes = [Html5::ATTR_TYPE => [Input::TYPE_CHECKBOX]];
265
        if ($entity->isGravatarAllowed()) {
266
            $attributes[Html5::ATTR_CHECKED] = null;
267
        }
268
        $input = new Input(
269
            'is_gravatar_allowed',
270
            'is_gravatar_allowed',
271
            '1',
272
            [],
273
            $attributes
274
        );
275
        $label = new Label(
276
            'is_gravatar_allowed',
277
            'admin:userIsGravatarAllowed'
278
        );
279
        $help  = new Help('admin:userIsGravatarAllowedHelp');
280
281
        $this->form[] = new CheckboxGroup($input, $label, $help);
282
283
        return $this;
284
    }
285
286
    /**
287
     * @param Entity $entity
288
     *
289
     * @return $this
290
     */
291
    protected function addUserGroups(Entity $entity): User
292
    {
293
        $allUserGroups = $this->getAllUserGroups();
294
295
        $userGroupIds = [];
296
        foreach ($entity->getUserGroups() as $userGroup) {
297
            $userGroupIds[] = $userGroup->getId();
298
        }
299
300
        $options = $this->createUserGroupOptions($allUserGroups, $userGroupIds);
301
302
        $this->form[] = new FormGroup(
303
            $this->createUserGroupSelect($options),
304
            $this->createUserGroupLabel()
305
        );
306
307
        return $this;
308
    }
309
310
    /**
311
     * @return UserGroup[]
312
     * @throws \Opulence\Orm\OrmException
313
     */
314
    protected function getAllUserGroups(): array
315
    {
316
        return $this->userGroupRepo->getAll();
317
    }
318
319
    /**
320
     * @param UserGroup[] $allUserGroups
321
     * @param UserGroup[] $userGroupIds
322
     *
323
     * @return array
324
     */
325
    protected function createUserGroupOptions(array $allUserGroups, array $userGroupIds): array
326
    {
327
        $options = [];
328
        foreach ($allUserGroups as $userGroup) {
329
            $isSelected = in_array($userGroup->getId(), $userGroupIds, true);
330
            $options[]  = new Option($userGroup->getId(), $userGroup->getName(), $isSelected);
331
        }
332
333
        return $options;
334
    }
335
336
    /**
337
     * @param Option[] $options
338
     *
339
     * @return Select
340
     */
341
    protected function createUserGroupSelect(array $options): Select
342
    {
343
        $size       = $this->getMultiSelectSize(
344
            count($options),
345
            static::MULTISELECT_MIN_SIZE,
346
            static::MULTISELECT_MAX_SIZE
347
        );
348
        $attributes = [Html5::ATTR_SIZE => [$size]];
349
350
        $select = new MultiSelect('user_group_ids', 'user_group_ids[]', [], $attributes);
351
352
        foreach ($options as $option) {
353
            $select[] = $option;
354
        }
355
356
        return $select;
357
    }
358
359
    /**
360
     * @return Label
361
     */
362
    protected function createUserGroupLabel(): Label
363
    {
364
        return new Label('user_group_ids', 'admin:userGroups');
365
    }
366
367
    /**
368
     * @param Entity $entity
369
     *
370
     * @return $this
371
     */
372
    protected function addUserLanguages(Entity $entity): User
373
    {
374
        $allUserGroups = $this->getAllUserLanguages();
375
        $userGroupId   = $entity->getUserLanguage()->getId();
376
377
        $options = $this->createUserLanguageOptions($allUserGroups, $userGroupId);
378
379
        $this->form[] = new FormGroup(
380
            $this->createUserLanguageSelect($options),
381
            $this->createUserLanguageLabel(),
382
            null,
383
            [],
384
            [Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]
385
        );
386
387
        return $this;
388
    }
389
390
    /**
391
     * @return UserLanguage[]
392
     * @throws \Opulence\Orm\OrmException
393
     */
394
    protected function getAllUserLanguages(): array
395
    {
396
        return $this->userLanguageRepo->getAll();
397
    }
398
399
    /**
400
     * @param UserLanguage[] $allUserLanguages
401
     * @param string         $userLanguageId
402
     *
403
     * @return array
404
     */
405
    protected function createUserLanguageOptions(array $allUserLanguages, string $userLanguageId): array
406
    {
407
        $options = [];
408
        foreach ($allUserLanguages as $userLanguage) {
409
            $isSelected = $userLanguageId === $userLanguage->getId();
410
            $options[]  = new Option($userLanguage->getId(), $userLanguage->getName(), $isSelected);
411
        }
412
413
        return $options;
414
    }
415
416
    /**
417
     * @param Option[] $options
418
     *
419
     * @return Select
420
     */
421
    protected function createUserLanguageSelect(array $options): Select
422
    {
423
        $size       = $this->getMultiSelectSize(
424
            count($options),
425
            static::MULTISELECT_MIN_SIZE,
426
            static::MULTISELECT_MAX_SIZE
427
        );
428
        $attributes = [Html5::ATTR_SIZE => [$size]];
429
430
        $select = new MultiSelect('user_language_id', 'user_language_id', [], $attributes);
431
432
        foreach ($options as $option) {
433
            $select[] = $option;
434
        }
435
436
        return $select;
437
    }
438
439
    /**
440
     * @return Label
441
     */
442
    protected function createUserLanguageLabel(): Label
443
    {
444
        return new Label('user_language_id', 'admin:userLanguages');
445
    }
446
}
447