Failed Conditions
Pull Request — master (#22)
by Florent
08:54
created

ScopePolicyManager::getForClient()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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\Server\Model\Scope;
15
16
use Assert\Assertion;
17
use OAuth2Framework\Component\Server\Model\Client\Client;
18
19
final class ScopePolicyManager
20
{
21
    /**
22
     * @var ScopePolicyInterface[]
23
     */
24
    private $scopePolicies = [];
25
26
    /**
27
     * @var string
28
     */
29
    private $defaultScopePolicy = null;
30
31
    /**
32
     * @param ScopePolicyInterface $scopePolicy
33
     * @param bool                 $isDefault
34
     *
35
     * @return ScopePolicyManager
36
     */
37
    public function add(ScopePolicyInterface $scopePolicy, bool $isDefault = false): ScopePolicyManager
38
    {
39
        $name = $scopePolicy->name();
40
        $this->scopePolicies[$name] = $scopePolicy;
41
42
        if (true === $isDefault || 1 === count($this->scopePolicies)) {
43
            $this->defaultScopePolicy = $name;
44
        }
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param string $scopePolicy
51
     *
52
     * @return bool
53
     */
54
    public function has(string $scopePolicy): bool
55
    {
56
        return array_key_exists($scopePolicy, $this->scopePolicies);
57
    }
58
59
    /**
60
     * @param string $scopePolicyName
61
     *
62
     * @return ScopePolicyInterface
63
     */
64
    public function get(string $scopePolicyName): ScopePolicyInterface
65
    {
66
        Assertion::keyExists($this->scopePolicies, $scopePolicyName, sprintf('The scope policy with name \'%s\' is not supported', $scopePolicyName));
67
68
        return $this->scopePolicies[$scopePolicyName];
69
    }
70
71
    /**
72
     * @return ScopePolicyInterface|null
73
     */
74
    public function default(): ?ScopePolicyInterface
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
75
    {
76
        if (null === $this->defaultScopePolicy) {
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
77
            return null;
78
        }
79
80
        return $this->scopePolicies[$this->defaultScopePolicy];
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
81
    }
82
83
    /**
84
     * @param array  $scope
85
     * @param Client $client
86
     *
87
     * @return array
88
     */
89
    public function check(array $scope, Client $client): array
90
    {
91
        if (empty($scope)) {
92
            $policy = $this->getForClient($client);
93
94
            if (null !== $policy) {
95
                return $policy->checkScopePolicy($scope, $client);
96
            }
97
        }
98
99
        return $scope;
100
    }
101
102
    /**
103
     * @param Client $client
104
     *
105
     * @return ScopePolicyInterface|null
106
     */
107
    private function getForClient(Client $client): ?ScopePolicyInterface
108
    {
109
        if ($client->has('scope_policy') && $this->has($client->get('scope_policy'))) {
110
            $policyName = $client->get('scope_policy');
111
112
            return $this->get($policyName);
113
        }
114
115
        return $this->default();
116
    }
117
}
118