Passed
Push — master ( bc25fe...f18d9c )
by Peter
03:21
created

User::getValidator()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 4
nop 1
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
Bug introduced by
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
Bug introduced by
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\ExistingUser as ValidatorFactory;
12
use AbterPhp\Admin\Validation\Factory\NewUser as NewUserValidatorFactory;
13
use AbterPhp\Framework\Crypto\Crypto;
14
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
15
use Opulence\Events\Dispatchers\IEventDispatcher;
16
use Opulence\Http\Requests\UploadedFile;
17
use Opulence\Orm\IUnitOfWork;
18
use Opulence\Validation\IValidator;
19
20
class User extends RepoServiceAbstract
21
{
22
    /** @var Crypto */
23
    private $crypto;
24
25
    /** @var Crypto */
26
    private $newUserValidatorFactory;
27
28
    /**
29
     * User constructor.
30
     *
31
     * @param GridRepo                $repo
32
     * @param ValidatorFactory        $validatorFactory
33
     * @param IUnitOfWork             $unitOfWork
34
     * @param IEventDispatcher        $eventDispatcher
35
     * @param Crypto                  $crypto
36
     * @param NewUserValidatorFactory $newUserValidatorFactory
37
     */
38
    public function __construct(
39
        GridRepo $repo,
40
        ValidatorFactory $validatorFactory,
41
        IUnitOfWork $unitOfWork,
42
        IEventDispatcher $eventDispatcher,
43
        Crypto $crypto,
44
        NewUserValidatorFactory $newUserValidatorFactory
45
    ) {
46
        parent::__construct(
47
            $repo,
48
            $validatorFactory,
49
            $unitOfWork,
50
            $eventDispatcher
51
        );
52
53
        $this->crypto                  = $crypto;
54
        $this->newUserValidatorFactory = $newUserValidatorFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $newUserValidatorFactory of type AbterPhp\Admin\Validation\Factory\NewUser is incompatible with the declared type AbterPhp\Framework\Crypto\Crypto of property $newUserValidatorFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
    }
56
57
    /**
58
     * @param int $additionalData
59
     *
60
     * @return IValidator
61
     */
62
    protected function getValidator(int $additionalData): IValidator
63
    {
64
        if ($additionalData == static::CREATE) {
65
            $this->validator = $this->newUserValidatorFactory->createValidator();
0 ignored issues
show
Bug introduced by
The method createValidator() does not exist on AbterPhp\Framework\Crypto\Crypto. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            /** @scrutinizer ignore-call */ 
66
            $this->validator = $this->newUserValidatorFactory->createValidator();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
68
        if ($this->validator) {
69
            return $this->validator;
70
        }
71
72
        $this->validator = $this->validatorFactory->createValidator();
73
74
        return $this->validator;
75
    }
76
77
    /**
78
     * @param string $entityId
79
     *
80
     * @return Entity
81
     */
82
    public function createEntity(string $entityId): IStringerEntity
83
    {
84
        $userLanguage = new UserLanguage(
85
            '',
86
            '',
87
            ''
88
        );
89
        $entity       = new Entity(
90
            $entityId,
91
            '',
92
            '',
93
            '',
94
            false,
95
            false,
96
            $userLanguage
97
        );
98
99
        return $entity;
100
    }
101
102
    /**
103
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
104
     *
105
     * @param IStringerEntity $entity
106
     * @param array           $postData
107
     * @param UploadedFile[]  $fileData
108
     *
109
     * @return Entity
110
     */
111
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
112
    {
113
        assert($entity instanceof Entity, new \InvalidArgumentException('Invalid entity'));
114
115
        $username          = isset($postData['username']) ? (string)$postData['username'] : '';
116
        $email             = isset($postData['email']) ? (string)$postData['email'] : '';
117
        $password          = isset($postData['password']) ? (string)$postData['password'] : '';
118
        $isGravatarAllowed = isset($postData['is_gravatar_allowed']) ? (bool)$postData['is_gravatar_allowed'] : false;
119
        $canLogin          = isset($postData['can_login']) ? (bool)$postData['can_login'] : false;
120
        $userLanguage      = $this->createUserLanguage($postData);
121
        $userGroups        = $this->createUserGroups($postData);
122
123
        $entity->setUsername($username)
124
            ->setEmail($email)
125
            ->setIsGravatarAllowed($isGravatarAllowed)
126
            ->setCanLogin($canLogin)
127
            ->setUserLanguage($userLanguage)
128
            ->setUserGroups($userGroups);
129
130
        if ($password) {
131
            $entity->setPassword($this->crypto->hashCrypt($password));
132
        }
133
134
        return $entity;
135
    }
136
137
    /**
138
     * @param array $postData
139
     *
140
     * @return UserLanguage
141
     */
142
    protected function createUserLanguage(array $postData): UserLanguage
143
    {
144
        $userLanguageId = isset($postData['user_language_id']) ? (string)$postData['user_language_id'] : '';
145
146
        return new UserLanguage($userLanguageId, '', '');
147
    }
148
149
    /**
150
     * @param array $postData
151
     *
152
     * @return array
153
     */
154
    protected function createUserGroups(array $postData): array
155
    {
156
        $userGroups = [];
157
        if (!empty($postData['user_group_ids'])) {
158
            foreach ($postData['user_group_ids'] as $userGroupId) {
159
                $userGroups[] = new UserGroup(
160
                    (string)$userGroupId,
161
                    '',
162
                    ''
163
                );
164
            }
165
        }
166
167
        return $userGroups;
168
    }
169
}
170