ScopeRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A finalizeScopes() 0 15 2
A getScopeEntityByIdentifier() 0 19 2
1
<?php
2
3
/**
4
 * @author      Alex Bilbie <[email protected]>
5
 * @copyright   Copyright (c) Alex Bilbie
6
 * @license     http://mit-license.org/
7
 *
8
 * @link        https://github.com/thephpleague/oauth2-server
9
 */
10
11
declare(strict_types=1);
12
13
namespace OAuth2ServerExamples\Repositories;
14
15
use League\OAuth2\Server\Entities\ClientEntityInterface;
16
use League\OAuth2\Server\Entities\ScopeEntityInterface;
17
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
18
use OAuth2ServerExamples\Entities\ScopeEntity;
19
20
use function array_key_exists;
21
22
class ScopeRepository implements ScopeRepositoryInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getScopeEntityByIdentifier($scopeIdentifier): ?ScopeEntityInterface
28
    {
29
        $scopes = [
30
            'basic' => [
31
                'description' => 'Basic details about you',
32
            ],
33
            'email' => [
34
                'description' => 'Your email address',
35
            ],
36
        ];
37
38
        if (array_key_exists($scopeIdentifier, $scopes) === false) {
39
            return null;
40
        }
41
42
        $scope = new ScopeEntity();
43
        $scope->setIdentifier($scopeIdentifier);
44
45
        return $scope;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function finalizeScopes(
52
        array $scopes,
53
        $grantType,
54
        ClientEntityInterface $clientEntity,
55
        $userIdentifier = null,
56
        $authCodeId = null
57
    ): array {
58
        // Example of programatically modifying the final scope of the access token
59
        if ((int) $userIdentifier === 1) {
60
            $scope = new ScopeEntity();
61
            $scope->setIdentifier('email');
62
            $scopes[] = $scope;
63
        }
64
65
        return $scopes;
66
    }
67
}
68