Test Setup Failed
Push — oauth2 ( 63c070 )
by Herberto
11:43 queued 07:12
created

OauthScope::hasScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Auth\Authentication\Oauth;
16
17
use League\OAuth2\Server\Entities\ScopeEntityInterface;
18
use League\OAuth2\Server\Entities\Traits\EntityTrait;
19
20
class OauthScope implements ScopeEntityInterface
21
{
22
    use EntityTrait;
23
24
    public const SCOPE_EDITOR = 'editor';
25
    public const SCOPE_ADMIN = 'admin';
26
27
    /**
28
     * @var array
29
     */
30
    public static $scopes = [
31
        self::SCOPE_ADMIN,
32
        self::SCOPE_EDITOR,
33
    ];
34
35
    public function __construct(string $identifier)
36
    {
37
        if (!self::hasScope($identifier)) {
38
            throw new InvalidOauthScopeException($identifier);
39
        }
40
41
        $this->setIdentifier($identifier);
42
    }
43
44
    public static function constructEditorScope(): self
45
    {
46
        return new self(self::SCOPE_EDITOR);
47
    }
48
49
    public static function constructAdminScope(): self
50
    {
51
        return new self(self::SCOPE_ADMIN);
52
    }
53
54
    public static function hasScope(string $identifier): bool
55
    {
56
        return $identifier === '*' || array_key_exists($identifier, array_flip(static::$scopes));
57
    }
58
59
    public function jsonSerialize(): string
60
    {
61
        return $this->getIdentifier();
62
    }
63
}
64