Issues (104)

src/Service/Execute/User.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Service\Execute;
6
7
use AbterPhp\Admin\Domain\Entities\User as Entity;
8
use AbterPhp\Admin\Domain\Entities\UserGroup;
0 ignored issues
show
This use statement conflicts with another class in this namespace, AbterPhp\Admin\Service\Execute\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;
0 ignored issues
show
This use statement conflicts with another class in this namespace, AbterPhp\Admin\Service\Execute\UserLanguage. 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...
10
use AbterPhp\Admin\Orm\UserRepo as GridRepo;
11
use AbterPhp\Admin\Validation\Factory\User as ValidatorFactory;
12
use AbterPhp\Framework\Crypto\Crypto;
13
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
14
use Opulence\Events\Dispatchers\IEventDispatcher;
15
use Opulence\Http\Requests\UploadedFile;
16
use Opulence\Orm\IUnitOfWork;
17
use Opulence\Validation\IValidator;
18
19
class User extends RepoServiceAbstract
20
{
21
    private Crypto $crypto;
22
23
    /**
24
     * User constructor.
25
     *
26
     * @param GridRepo                $repo
27
     * @param ValidatorFactory        $validatorFactory
28
     * @param IUnitOfWork             $unitOfWork
29
     * @param IEventDispatcher        $eventDispatcher
30
     * @param Crypto                  $crypto
31
     */
32
    public function __construct(
33
        GridRepo $repo,
34
        ValidatorFactory $validatorFactory,
35
        IUnitOfWork $unitOfWork,
36
        IEventDispatcher $eventDispatcher,
37
        Crypto $crypto
38
    ) {
39
        parent::__construct(
40
            $repo,
41
            $validatorFactory,
42
            $unitOfWork,
43
            $eventDispatcher
44
        );
45
46
        $this->crypto                  = $crypto;
47
    }
48
49
    /**
50
     * @param int $additionalData
51
     *
52
     * @return IValidator
53
     */
54
    protected function getValidator(int $additionalData): IValidator
55
    {
56
        if ($this->validator) {
57
            return $this->validator;
58
        }
59
60
        $this->validator = $this->validatorFactory->createValidator();
61
62
        return $this->validator;
63
    }
64
65
    /**
66
     * @param string $entityId
67
     *
68
     * @return Entity
69
     */
70
    public function createEntity(string $entityId): IStringerEntity
71
    {
72
        $userLanguage = new UserLanguage(
73
            '',
74
            '',
75
            ''
76
        );
77
78
        return new Entity(
79
            $entityId,
80
            '',
81
            '',
82
            '',
83
            false,
84
            false,
85
            $userLanguage
86
        );
87
    }
88
89
    /**
90
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
91
     *
92
     * @param IStringerEntity $entity
93
     * @param array           $postData
94
     * @param UploadedFile[]  $fileData
95
     *
96
     * @return Entity
97
     */
98
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
99
    {
100
        assert($entity instanceof Entity, new \InvalidArgumentException('Invalid entity'));
101
102
        $username          = $postData['username'] ?: '';
103
        $email             = $postData['email'] ?: '';
104
        $password          = $postData['password'] ?: '';
105
        $isGravatarAllowed = $postData['is_gravatar_allowed'] ?? false;
106
        $canLogin          = $postData['can_login'] ?? false;
107
        $userLanguage      = $this->createUserLanguage($postData);
108
        $userGroups        = $this->createUserGroups($postData);
109
110
        $entity->setUsername($username)
111
            ->setEmail($email)
112
            ->setIsGravatarAllowed((bool)$isGravatarAllowed)
113
            ->setCanLogin((bool)$canLogin)
114
            ->setUserLanguage($userLanguage)
115
            ->setUserGroups($userGroups);
116
117
        if ($password) {
118
            $entity->setPassword($this->crypto->hashCrypt($password));
119
        }
120
121
        return $entity;
122
    }
123
124
    /**
125
     * @param array $postData
126
     *
127
     * @return UserLanguage
128
     */
129
    protected function createUserLanguage(array $postData): UserLanguage
130
    {
131
        $userLanguageId = $postData['user_language_id'] ?? '';
132
133
        return new UserLanguage($userLanguageId, '', '');
134
    }
135
136
    /**
137
     * @param array $postData
138
     *
139
     * @return array
140
     */
141
    protected function createUserGroups(array $postData): array
142
    {
143
        $userGroups = [];
144
        if (!empty($postData['user_group_ids'])) {
145
            foreach ($postData['user_group_ids'] as $userGroupId) {
146
                $userGroups[] = new UserGroup(
147
                    (string)$userGroupId,
148
                    '',
149
                    ''
150
                );
151
            }
152
        }
153
154
        return $userGroups;
155
    }
156
}
157