Test Setup Failed
Push — dev ( b27119...389162 )
by Herberto
04:46
created

OauthScopeRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getScopeEntityByIdentifier() 0 4 1
A finalizeScopes() 0 16 3
A find() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Acme\App\Infrastructure\Auth\Authentication\Oauth;
6
7
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
8
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
9
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
10
use League\OAuth2\Server\Entities\ClientEntityInterface;
11
use League\OAuth2\Server\Entities\ScopeEntityInterface;
12
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
13
14
final class OauthScopeRepository implements ScopeRepositoryInterface
15
{
16
    /**
17
     * @var DqlQueryBuilderInterface
18
     */
19
    private $dqlQueryBuilder;
20
21
    /**
22
     * @var QueryServiceRouterInterface
23
     */
24
    private $queryService;
25
26
    public function __construct(
27
        DqlQueryBuilderInterface $dqlQueryBuilder,
28
        QueryServiceRouterInterface $queryService
29
    ) {
30
        $this->dqlQueryBuilder = $dqlQueryBuilder;
31
        $this->queryService = $queryService;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getScopeEntityByIdentifier($identifier): ?ScopeEntityInterface
38
    {
39
        return $this->find($identifier);
40
    }
41
42
    /**
43
     * Given a client, grant type and optional user identifier validate the set of scopes requested are valid and
44
     * optionally append additional scopes or remove requested scopes.
45
     *
46
     * @param ScopeEntityInterface[] $scopes
47
     * @param string $grantType
48
     * @param ClientEntityInterface $clientEntity
49
     * @param null|string $userIdentifier
50
     *
51
     * @return ScopeEntityInterface[]
52
     */
53
    public function finalizeScopes(
54
        array $scopes,
55
        $grantType,
56
        ClientEntityInterface $clientEntity,
57
        $userIdentifier = null
58
    ): array {
59
        $filteredScopes = [];
60
61
        foreach ($scopes as $scope) {
62
            if (OauthScope::hasScope($scope->getIdentifier())) {
63
                $filteredScopes[] = $scope;
64
            }
65
        }
66
67
        return $filteredScopes;
68
    }
69
70
    private function find(string $oauthScopeId): ?OauthScope
71
    {
72
        $dqlQuery = $this->dqlQueryBuilder->create(OauthScope::class)
73
            ->where('OauthScope.identifier = :identifier')
74
            ->setParameter('identifier', $oauthScopeId)
75
            ->build();
76
77
        try {
78
            return $this->queryService->query($dqlQuery)->getSingleResult();
79
        } catch (EmptyQueryResultException $e) {
80
            return null;
81
        }
82
    }
83
}
84