Failed Conditions
Push — master ( dc4e55...720050 )
by Florent
19:46 queued 13:33
created

ScopePolicyManager::default()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
     * @return bool
52
     */
53
    public function has(string $scopePolicy): bool
54
    {
55
        return array_key_exists($scopePolicy, $this->scopePolicies);
56
    }
57
58
    /**
59
     * @param string $scopePolicyName
60
     * @return ScopePolicyInterface
61
     */
62
    public function get(string $scopePolicyName): ScopePolicyInterface
63
    {
64
        Assertion::keyExists($this->scopePolicies, $scopePolicyName, sprintf('The scope policy with name \'%s\' is not supported', $scopePolicyName));
65
66
        return $this->scopePolicies[$scopePolicyName];
67
    }
68
69
    /**
70
     * @return ScopePolicyInterface|null
71
     */
72
    public function default(): ?ScopePolicyInterface
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
73
    {
74
        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...
75
            return null;
76
        }
77
78
        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...
79
    }
80
81
    /**
82
     * @param array $scope
83
     * @param Client $client
84
     *
85
     * @return array
86
     */
87
    public function check(array $scope, Client $client): array
88
    {
89
        if (empty($scope)) {
90
            $policy = $this->getForClient($client);
91
92
            if (null !== $policy) {
93
                return $policy->checkScopePolicy($scope, $client);
94
            }
95
        }
96
97
        return $scope;
98
    }
99
100
    /**
101
     * @param Client $client
102
     *
103
     * @return ScopePolicyInterface|null
104
     */
105
    private function getForClient(Client $client): ?ScopePolicyInterface
106
    {
107
        if ($client->has('scope_policy') && $this->has($client->get('scope_policy'))) {
108
            $policyName = $client->get('scope_policy');
109
110
            return $this->get($policyName);
111
        }
112
113
        return $this->default();
114
    }
115
}
116