Failed Conditions
Push — master ( a399af...7c3864 )
by Florent
04:57
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
    public function add(Claim $claim): void
26
    {
27
        $this->claims[$claim->name()] = $claim;
28
    }
29
30
    /**
31
     * @return Claim[]
32
     */
33
    public function list(): array
34
    {
35
        return \array_keys($this->claims);
36
    }
37
38
    /**
39
     * @return Claim[]
40
     */
41
    public function all(): array
42
    {
43
        return $this->claims;
44
    }
45
46
    public function has(string $claim): bool
47
    {
48
        return \array_key_exists($claim, $this->claims);
49
    }
50
51
    public function get(string $claim): Claim
52
    {
53
        if (!$this->has($claim)) {
54
            throw new \InvalidArgumentException(\sprintf('Unsupported claim "%s".', $claim));
55
        }
56
57
        return $this->claims[$claim];
58
    }
59
60
    public function getUserInfo(UserAccount $userAccount, array $claims, array $claimLocales): array
61
    {
62
        $result = [];
63
        $claimLocales[] = null;
64
        foreach ($claims as $claimName => $config) {
65
            if ($this->has($claimName)) {
66
                $claim = $this->get($claimName);
67
                foreach ($claimLocales as $claimLocale) {
68
                    if ($claim->isAvailableForUserAccount($userAccount, $claimLocale)) {
69
                        $value = $claim->getForUserAccount($userAccount, $claimLocale);
70
                        switch (true) {
71
                            case \is_array($config) && \array_key_exists('value', $config):
72
                                if ($claim === $config['value']) {
73
                                    $result[$claimName] = $value;
74
                                }
75
76
                                break;
77
                            case \is_array($config) && \array_key_exists('values', $config) && \is_array($config['values']):
78
                                if (\in_array($claim, $config['values'], true)) {
79
                                    $result[$claimName] = $value;
80
                                }
81
82
                                break;
83
                            default:
84
                                $result[$claimName] = $value;
85
                        }
86
                    }
87
                }
88
            }
89
        }
90
91
        return $result;
92
    }
93
}
94