Failed Conditions
Push — ng ( f9780e...ccd5de )
by Florent
11:07
created

ScopePolicyManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 109
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 11 3
A apply() 0 12 3
A all() 0 4 1
A has() 0 4 1
A get() 0 8 2
A default() 0 8 2
A getForClient() 0 10 3
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\Server\Scope\Policy;
15
16
use OAuth2Framework\Component\Server\Core\Client\Client;
17
18
final class ScopePolicyManager
19
{
20
    /**
21
     * @var ScopePolicy[]
22
     */
23
    private $scopePolicies = [];
24
25
    /**
26
     * @var string
27
     */
28
    private $defaultScopePolicy = null;
29
30
    /**
31
     * @param ScopePolicy $scopePolicy
32
     * @param bool        $isDefault
33
     *
34
     * @return ScopePolicyManager
35
     */
36
    public function add(ScopePolicy $scopePolicy, bool $isDefault = false): self
37
    {
38
        $name = $scopePolicy->name();
39
        $this->scopePolicies[$name] = $scopePolicy;
40
41
        if (true === $isDefault || 1 === count($this->scopePolicies)) {
42
            $this->defaultScopePolicy = $name;
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $scope
50
     * @param Client $client
51
     *
52
     * @return string
53
     */
54
    public function apply(string $scope, Client $client): string
55
    {
56
        if (empty($scope)) {
57
            $policy = $this->getForClient($client);
58
59
            if (null !== $policy) {
60
                return $policy->applyScopePolicy($scope, $client);
61
            }
62
        }
63
64
        return $scope;
65
    }
66
67
    /**
68
     * @return string[]
69
     */
70
    public function all(): array
71
    {
72
        return array_keys($this->scopePolicies);
73
    }
74
75
    /**
76
     * @param string $scopePolicy
77
     *
78
     * @return bool
79
     */
80
    public function has(string $scopePolicy): bool
81
    {
82
        return array_key_exists($scopePolicy, $this->scopePolicies);
83
    }
84
85
    /**
86
     * @param string $scopePolicyName
87
     *
88
     * @return ScopePolicy
89
     */
90
    private function get(string $scopePolicyName): ScopePolicy
91
    {
92
        if (!$this->has($scopePolicyName)) {
93
            throw new \InvalidArgumentException(sprintf('The scope policy with name "%s" is not supported', $scopePolicyName));
94
        }
95
96
        return $this->scopePolicies[$scopePolicyName];
97
    }
98
99
    /**
100
     * @return ScopePolicy|null
101
     */
102
    private function default(): ?ScopePolicy
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
103
    {
104
        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...
105
            return null;
106
        }
107
108
        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...
109
    }
110
111
    /**
112
     * @param Client $client
113
     *
114
     * @return ScopePolicy|null
115
     */
116
    private function getForClient(Client $client): ?ScopePolicy
117
    {
118
        if ($client->has('scope_policy') && $this->has($client->get('scope_policy'))) {
119
            $policyName = $client->get('scope_policy');
120
121
            return $this->get($policyName);
122
        }
123
124
        return $this->default();
125
    }
126
}
127