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

OauthScopeRepository::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
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 Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
18
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
19
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
20
use League\OAuth2\Server\Entities\ClientEntityInterface;
21
use League\OAuth2\Server\Entities\ScopeEntityInterface;
22
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
23
24
final class OauthScopeRepository implements ScopeRepositoryInterface
25
{
26
    /**
27
     * @var DqlQueryBuilderInterface
28
     */
29
    private $dqlQueryBuilder;
30
31
    /**
32
     * @var QueryServiceRouterInterface
33
     */
34
    private $queryService;
35
36
    public function __construct(
37
        DqlQueryBuilderInterface $dqlQueryBuilder,
38
        QueryServiceRouterInterface $queryService
39
    ) {
40
        $this->dqlQueryBuilder = $dqlQueryBuilder;
41
        $this->queryService = $queryService;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getScopeEntityByIdentifier($identifier): ?ScopeEntityInterface
48
    {
49
        return $this->find($identifier);
50
    }
51
52
    /**
53
     * Given a client, grant type and optional user identifier validate the set of scopes requested are valid and
54
     * optionally append additional scopes or remove requested scopes.
55
     *
56
     * @param ScopeEntityInterface[] $scopes
57
     * @param string $grantType
58
     * @param string|null $userIdentifier
59
     *
60
     * @return ScopeEntityInterface[]
61
     */
62
    public function finalizeScopes(
63
        array $scopes,
64
        $grantType,
65
        ClientEntityInterface $clientEntity,
66
        $userIdentifier = null
67
    ): array {
68
        $filteredScopes = [];
69
70
        foreach ($scopes as $scope) {
71
            if (OauthScope::hasScope($scope->getIdentifier())) {
72
                $filteredScopes[] = $scope;
73
            }
74
        }
75
76
        return $filteredScopes;
77
    }
78
79
    private function find(string $oauthScopeId): ?OauthScope
80
    {
81
        $dqlQuery = $this->dqlQueryBuilder->create(OauthScope::class)
82
            ->where('OauthScope.identifier = :identifier')
83
            ->setParameter('identifier', $oauthScopeId)
84
            ->build();
85
86
        try {
87
            return $this->queryService->query($dqlQuery)->getSingleResult();
88
        } catch (EmptyQueryResultException $e) {
89
            return null;
90
        }
91
    }
92
}
93