Failed Conditions
Push — master ( b8d841...bc596e )
by Florent
28:20
created

ClaimManager::getUserInfo()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.9666
c 0
b 0
f 0
cc 12
nc 9
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\OpenIdConnect\UserInfo\Claim;
15
16
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
17
18
class ClaimManager
19
{
20
    /**
21
     * @var Claim[]
22
     */
23
    private $claims = [];
24
25
    /**
26
     * @return ClaimManager
27
     */
28
    public function add(Claim $claim): self
29
    {
30
        $this->claims[$claim->name()] = $claim;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @return Claim[]
37
     */
38
    public function list(): array
39
    {
40
        return \array_keys($this->claims);
41
    }
42
43
    /**
44
     * @return Claim[]
45
     */
46
    public function all(): array
47
    {
48
        return $this->claims;
49
    }
50
51
    public function has(string $claim): bool
52
    {
53
        return \array_key_exists($claim, $this->claims);
54
    }
55
56
    public function get(string $claim): Claim
57
    {
58
        if (!$this->has($claim)) {
59
            throw new \InvalidArgumentException(\sprintf('Unsupported claim "%s".', $claim));
60
        }
61
62
        return $this->claims[$claim];
63
    }
64
65
    public function getUserInfo(UserAccount $userAccount, array $claims, array $claimLocales): array
66
    {
67
        $result = [];
68
        $claimLocales[] = null;
69
        foreach ($claims as $claimName => $config) {
70
            if ($this->has($claimName)) {
71
                $claim = $this->get($claimName);
72
                foreach ($claimLocales as $claimLocale) {
73
                    if ($claim->isAvailableForUserAccount($userAccount, $claimLocale)) {
74
                        $value = $claim->getForUserAccount($userAccount, $claimLocale);
75
                        switch (true) {
76
                            case \is_array($config) && \array_key_exists('value', $config):
77
                                if ($claim === $config['value']) {
78
                                    $result[$claimName] = $value;
79
                                }
80
81
                                break;
82
                            case \is_array($config) && \array_key_exists('values', $config) && \is_array($config['values']):
83
                                if (\in_array($claim, $config['values'], true)) {
84
                                    $result[$claimName] = $value;
85
                                }
86
87
                                break;
88
                            default:
89
                                $result[$claimName] = $value;
90
                        }
91
                    }
92
                }
93
            }
94
        }
95
96
        return $result;
97
    }
98
}
99