Completed
Push — master ( 289dee...aec877 )
by Alexandre
03:35
created

ScopePolicyManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: GCC-MED
5
 * Date: 15/01/2018
6
 * Time: 09:54
7
 */
8
9
namespace OAuth2\ScopePolicy;
10
11
12
use OAuth2\Config;
13
use OAuth2\Roles\ClientInterface;
14
use OAuth2\ScopePolicy\Policies\DefaultScopePolicy;
15
use OAuth2\Server;
16
17
class ScopePolicyManager
18
{
19
    /**
20
     * @var Server
21
     */
22
    protected $server;
23
    protected $defaultScopePolicy;
24
    protected $supportedScopes;
25
    protected $resourceOwnerRestrictedScopes = null;
26
27
    public function __construct(Server $server)
28
    {
29
        $this->server = $server;
30
        $this->defaultScopePolicy = new DefaultScopePolicy($server->getConfigurationRepository());
31
32
        $supportedScopes = $this->server->getConfigurationRepository()->getConfig(Config::SUPPORTED_SCOPES);
33
        $supportedScopes = is_string($supportedScopes) ? explode(' ', $supportedScopes) : $supportedScopes;
34
        $this->supportedScopes = $supportedScopes;
35
    }
36
37
    public function getDefaultScopes(ClientInterface $client): ?array
38
    {
39
        if ($scopePolicy = $client->getScopePolicy()) {
40
            return $scopePolicy->getDefaultScopes($client);
41
        }
42
        return $this->defaultScopePolicy->getDefaultScopes($client);
43
    }
44
45
    public function getSupportedScopes(ClientInterface $client)
46
    {
47
        return $client->getSupportedScopes() ? $client->getSupportedScopes() : $this->supportedScopes;
48
    }
49
50
    public function getScopeArray(ClientInterface $client, ?string $scope): ?array
51
    {
52
        if ($scope) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $scope of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53
            $scope = explode(' ', $scope);
54
        } else {
55
            $scope = $this->getDefaultScopes($client);
56
        }
57
        return $scope;
58
    }
59
60
    public function checkScope(ClientInterface $client, ?array $scope): bool
61
    {
62
        $supportedScopes = $this->getSupportedScopes($client);
63
64
        if (is_array($scope) && is_array($supportedScopes) && !empty(array_diff($scope, $supportedScopes))) {
65
            return false;
66
        }
67
68
        return true;
69
    }
70
71
    public function getResourceOwnerRestrictedScopes() : ?array {
72
        return $this->resourceOwnerRestrictedScopes;
73
    }
74
75
    /**
76
     * @see https://www.oauth.com/oauth2-servers/scope/checkboxes/
77
     * if null, scopes are not restricted
78
     * @param array|null $resourceOwnerRestrictedScopes
79
     */
80
    public function setResourceOwnerRestrictedScopes(?array $resourceOwnerRestrictedScopes = null)
81
    {
82
        $this->resourceOwnerRestrictedScopes = $resourceOwnerRestrictedScopes;
83
    }
84
85
}