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

OauthAccessTokenRepository::isAccessTokenRevoked()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
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 Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
18
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
19
use Acme\App\Core\Port\Persistence\PersistenceServiceInterface;
20
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
21
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId;
22
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
23
use League\OAuth2\Server\Entities\ClientEntityInterface;
24
use League\OAuth2\Server\Entities\ScopeEntityInterface;
25
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
26
use function is_string;
27
28
final class OauthAccessTokenRepository implements AccessTokenRepositoryInterface
29
{
30
    /**
31
     * @var DqlQueryBuilderInterface
32
     */
33
    private $dqlQueryBuilder;
34
35
    /**
36
     * @var QueryServiceRouterInterface
37
     */
38
    private $queryService;
39
40
    /**
41
     * @var PersistenceServiceInterface
42
     */
43
    private $persistenceService;
44
45
    public function __construct(
46
        DqlQueryBuilderInterface $dqlQueryBuilder,
47
        QueryServiceRouterInterface $queryService,
48
        PersistenceServiceInterface $persistenceService
49
    ) {
50
        $this->dqlQueryBuilder = $dqlQueryBuilder;
51
        $this->queryService = $queryService;
52
        $this->persistenceService = $persistenceService;
53
    }
54
55
    /**
56
     * @param ScopeEntityInterface[] $scopes
57
     * @param UserId $userIdentifier
58
     */
59
    public function getNewToken(
60
        ClientEntityInterface $clientEntity,
61
        array $scopes,
62
        $userIdentifier = null
63
    ): AccessTokenEntityInterface {
64
        return new OauthAccessToken(
65
            is_string($userIdentifier) ? new UserId($userIdentifier) : $userIdentifier, $clientEntity, $scopes
0 ignored issues
show
Bug introduced by
It seems like \is_string($userIdentifi...fier) : $userIdentifier can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
66
        );
67
    }
68
69
    public function persistNewAccessToken(AccessTokenEntityInterface $oauthAccessToken): void
70
    {
71
        $this->persistenceService->upsert($oauthAccessToken);
72
    }
73
74
    /**
75
     * @param string $tokenId
76
     */
77
    public function revokeAccessToken($tokenId): void
78
    {
79
        try {
80
            $oauthAccessToken = $this->find($tokenId);
81
        } catch (EmptyQueryResultException $e) {
82
            return;
83
        }
84
85
        $oauthAccessToken->revoke();
86
        $this->persistenceService->upsert($oauthAccessToken);
87
    }
88
89
    /**
90
     * @param string $tokenId
91
     */
92
    public function isAccessTokenRevoked($tokenId): ?bool
93
    {
94
        try {
95
            return $this->find($tokenId)->isRevoked();
96
        } catch (EmptyQueryResultException $e) {
97
            return true;
98
        }
99
    }
100
101
    private function find(string $tokenId): OauthAccessToken
102
    {
103
        $dqlQuery = $this->dqlQueryBuilder->create(OauthAccessToken::class)
104
            ->where('OauthAccessToken.identifier = :identifier')
105
            ->setParameter('identifier', $tokenId)
106
            ->build();
107
108
        return $this->queryService->query($dqlQuery)->getSingleResult();
109
    }
110
}
111