Failed Conditions
Pull Request — master (#22)
by Florent
14:38
created

ScopeRepository::getSupportedScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 OAuth2Framework\Component\Server\Model\Client\Client;
17
use OAuth2Framework\Component\Server\Util\Scope;
18
19
final class ScopeRepository implements ScopeRepositoryInterface
20
{
21
    /**
22
     * @var string[]
23
     */
24
    private $availableScopes = [];
25
26
    /**
27
     * ScopeManager constructor.
28
     *
29
     * @param array $availableScopes
30
     */
31
    public function __construct(array $availableScopes = [])
32
    {
33
        $this->availableScopes = $availableScopes;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getSupportedScopes(): array
40
    {
41
        return $this->availableScopes;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getAvailableScopesForClient(Client $client): array
48
    {
49
        return ($client->has('scope')) ? $this->convertToArray($client->get('scope')) : $this->getSupportedScopes();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function areRequestedScopesAvailable(array $requestedScopes, array $availableScopes): bool
56
    {
57
        return 0 === count(array_diff($requestedScopes, $availableScopes));
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function convertToArray(string $scopes): array
64
    {
65
        Scope::checkScopeCharset($scopes);
66
        $scopes = explode(' ', $scopes);
67
68
        foreach ($scopes as $scope) {
69
            Scope::checkScopeUsedOnce($scope, $scopes);
70
        }
71
72
        return $scopes;
73
    }
74
}
75