EuLoginUser   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Test Coverage

Coverage 85.85%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 46
eloc 81
c 7
b 2
f 0
dl 0
loc 269
ccs 91
cts 106
cp 0.8585
rs 8.72

36 Methods

Rating   Name   Duplication   Size   Complexity  
A getDomain() 0 3 1
A getTeleworkingPriority() 0 3 1
A getTicketType() 0 3 1
A getSso() 0 3 1
A getTelephoneNumber() 0 3 1
A get() 0 3 1
A getEmployeeType() 0 3 1
A getTimeZone() 0 3 1
A getDomainUsername() 0 3 1
A getLastName() 0 3 1
A getEmployeeNumber() 0 3 1
A getAuthenticationFactors() 0 3 1
A getFirstName() 0 3 1
A getProxyGrantingProtocol() 0 3 1
A getStrengths() 0 15 3
A getEmail() 0 3 1
A getAttributes() 0 31 5
A getLocale() 0 3 1
A getOrgId() 0 3 1
A getPgt() 0 3 1
A getGroups() 0 15 3
A getLoginDate() 0 3 1
A getUid() 0 3 1
A getSalt() 0 3 1
A getPassword() 0 3 1
A __construct() 0 3 1
A getAssuranceLevel() 0 3 1
A getDepartmentNumber() 0 3 1
A getRoles() 0 5 1
A getAttribute() 0 3 1
A eraseCredentials() 0 2 1
A getExtendedAttributes() 0 24 3
A isEqualTo() 0 3 1
A getUserIdentifier() 0 3 1
A getUserManager() 0 3 1
A getUsername() 0 11 1

How to fix   Complexity   

Complex Class

Complex classes like EuLoginUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EuLoginUser, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @see https://github.com/ecphp
8
 */
9
10
declare(strict_types=1);
11
12
namespace EcPhp\EuLoginBundle\Security\Core\User;
13
14
use EcPhp\CasBundle\Security\Core\User\CasUserInterface;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
use function array_key_exists;
18
19
final class EuLoginUser implements EuLoginUserInterface
20
{
21
    private CasUserInterface $user;
22
23 5
    public function __construct(CasUserInterface $user)
24
    {
25 5
        $this->user = $user;
26
    }
27
28
    public function eraseCredentials(): void
29
    {
30
        // null
31
    }
32
33 1
    public function get(string $key, $default = null)
34
    {
35 1
        return $this->user->get($key, $default);
36
    }
37
38 1
    public function getAssuranceLevel(): ?string
39
    {
40 1
        return $this->user->getAttribute('assuranceLevel');
41
    }
42
43
    public function getAttribute(string $key, $default = null)
44
    {
45
        return $this->user->getAttribute($key, $default);
46
    }
47
48 4
    public function getAttributes(): array
49
    {
50 4
        $attributes = $this->user->getAttributes();
51
52
        /** @Todo Ugly. Refactor this when JSON format will be available. */
53
        $propertyToMangle = [
54 4
            ['extendedAttributes', 'extendedAttribute'],
55
            ['groups', 'group'],
56
            ['strengths', 'strength'],
57
            ['authenticationFactors', 'authenticationFactor'],
58
        ];
59
60 4
        foreach ($propertyToMangle as [$parent, $child]) {
61 4
            if (!array_key_exists($parent, $attributes)) {
62 1
                continue;
63
            }
64
65 4
            if (!array_key_exists($child, $attributes[$parent])) {
0 ignored issues
show
Bug introduced by
It seems like $attributes[$parent] can also be of type string; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

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

65
            if (!array_key_exists($child, /** @scrutinizer ignore-type */ $attributes[$parent])) {
Loading history...
66 4
                continue;
67
            }
68
69 4
            $attributes[$parent][$child] = (array) $attributes[$parent][$child];
70
71 4
            if (array_key_exists(0, $attributes[$parent][$child])) {
72 3
                continue;
73
            }
74
75 4
            $attributes[$parent][$child] = [$attributes[$parent][$child]];
76
        }
77
78 4
        return $attributes;
79
    }
80
81 1
    public function getAuthenticationFactors(): array
82
    {
83 1
        return $this->user->getAttribute('authenticationFactors', []);
84
    }
85
86 1
    public function getDepartmentNumber(): ?string
87
    {
88 1
        return $this->user->getAttribute('departmentNumber');
89
    }
90
91 1
    public function getDomain(): ?string
92
    {
93 1
        return $this->user->getAttribute('domain');
94
    }
95
96 1
    public function getDomainUsername(): ?string
97
    {
98 1
        return $this->user->getAttribute('domainUsername');
99
    }
100
101 1
    public function getEmail(): ?string
102
    {
103 1
        return $this->user->getAttribute('email');
104
    }
105
106 1
    public function getEmployeeNumber(): ?string
107
    {
108 1
        return $this->user->getAttribute('employeeNumber');
109
    }
110
111 1
    public function getEmployeeType(): ?string
112
    {
113 1
        return $this->user->getAttribute('employeeType');
114
    }
115
116 1
    public function getExtendedAttributes(): array
117
    {
118 1
        $attributes = $this->getAttributes();
119
120 1
        if (!array_key_exists('extendedAttributes', $attributes)) {
121
            return [];
122
        }
123
124 1
        $extendedAttributes = $attributes['extendedAttributes'];
125
126 1
        if (!array_key_exists('extendedAttribute', $extendedAttributes)) {
0 ignored issues
show
Bug introduced by
It seems like $extendedAttributes can also be of type string; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

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

126
        if (!array_key_exists('extendedAttribute', /** @scrutinizer ignore-type */ $extendedAttributes)) {
Loading history...
127
            return [];
128
        }
129
130 1
        $extendedAttributes = $attributes['extendedAttributes']['extendedAttribute'];
131
132 1
        return array_reduce(
133
            $extendedAttributes,
134 1
            static function (array $carry, array $item): array {
135 1
                $carry[$item['@attributes']['name']] = $item['attributeValue'];
136
137 1
                return $carry;
138
            },
139 1
            []
140
        );
141
    }
142
143 1
    public function getFirstName(): ?string
144
    {
145 1
        return $this->user->getAttribute('firstName');
146
    }
147
148 3
    public function getGroups(): array
149
    {
150 3
        $attributes = $this->getAttributes();
151
152 3
        if (!array_key_exists('groups', $attributes)) {
153
            return [];
154
        }
155
156 3
        $groups = $attributes['groups'];
157
158 3
        if (!array_key_exists('group', $groups)) {
0 ignored issues
show
Bug introduced by
It seems like $groups can also be of type string; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

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

158
        if (!array_key_exists('group', /** @scrutinizer ignore-type */ $groups)) {
Loading history...
159 1
            return [];
160
        }
161
162 2
        return $groups['group'];
163
    }
164
165 1
    public function getLastName(): ?string
166
    {
167 1
        return $this->user->getAttribute('lastName');
168
    }
169
170 1
    public function getLocale(): ?string
171
    {
172 1
        return $this->user->getAttribute('locale');
173
    }
174
175 1
    public function getLoginDate(): ?string
176
    {
177 1
        return $this->user->getAttribute('loginDate');
178
    }
179
180 1
    public function getOrgId(): ?string
181
    {
182 1
        return $this->user->getAttribute('orgId');
183
    }
184
185 1
    public function getPassword()
186
    {
187 1
        return null;
188
    }
189
190 1
    public function getPgt(): ?string
191
    {
192 1
        return $this->user->getPgt();
193
    }
194
195 1
    public function getProxyGrantingProtocol(): ?string
196
    {
197 1
        return $this->user->getAttribute('proxyGrantingProtocol');
198
    }
199
200
    public function getRoles(): array
201
    {
202
        $default = ['ROLE_CAS_AUTHENTICATED'];
203
204
        return array_merge($this->getGroups(), $default);
205
    }
206
207 1
    public function getSalt()
208
    {
209 1
        return null;
210
    }
211
212 1
    public function getSso(): ?string
213
    {
214 1
        return $this->user->getAttribute('sso');
215
    }
216
217 2
    public function getStrengths(): array
218
    {
219 2
        $attributes = $this->getAttributes();
220
221 2
        if (!array_key_exists('strengths', $attributes)) {
222
            return [];
223
        }
224
225 2
        $strengths = $attributes['strengths'];
226
227 2
        if (!array_key_exists('strength', $strengths)) {
0 ignored issues
show
Bug introduced by
It seems like $strengths can also be of type string; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

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

227
        if (!array_key_exists('strength', /** @scrutinizer ignore-type */ $strengths)) {
Loading history...
228
            return [];
229
        }
230
231 2
        return (array) $strengths['strength'];
232
    }
233
234 1
    public function getTelephoneNumber(): ?string
235
    {
236 1
        return $this->user->getAttribute('telephoneNumber');
237
    }
238
239 1
    public function getTeleworkingPriority(): ?string
240
    {
241 1
        return $this->user->getAttribute('teleworkingPriority');
242
    }
243
244 1
    public function getTicketType(): ?string
245
    {
246 1
        return $this->user->getAttribute('ticketType');
247
    }
248
249 1
    public function getTimeZone(): ?string
250
    {
251 1
        return $this->user->getAttribute('timeZone');
252
    }
253
254 1
    public function getUid(): ?string
255
    {
256 1
        return $this->user->getAttribute('uid');
257
    }
258
259 1
    public function getUserIdentifier(): string
260
    {
261 1
        return $this->user->getUserIdentifier();
0 ignored issues
show
Bug introduced by
The method getUserIdentifier() does not exist on EcPhp\CasBundle\Security...e\User\CasUserInterface. It seems like you code against a sub-type of said class. However, the method does not exist in EcPhp\EuLoginBundle\Secu...er\EuLoginUserInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

261
        return $this->user->/** @scrutinizer ignore-call */ getUserIdentifier();
Loading history...
262
    }
263
264 1
    public function getUserManager(): ?string
265
    {
266 1
        return $this->user->getAttribute('userManager');
267
    }
268
269
    /**
270
     * @deprecated since Symfony 5.3, use getUserIdentifier() instead
271
     */
272
    public function getUsername()
273
    {
274
        trigger_deprecation(
275
            'ecphp/eu-login-bundle',
276
            '2.3.8',
277
            'The method "%s::getUsername()" is deprecated, use %s::getUserIdentifier() instead.',
278
            EuLoginUser::class,
279
            EuLoginUser::class
280
        );
281
282
        return $this->getUserIdentifier();
283
    }
284
285
    public function isEqualTo(UserInterface $user): bool
286
    {
287
        return $this->user->isEqualTo($user);
288
    }
289
}
290