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

persistNewRefreshToken()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
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 League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
22
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
23
24
final class OauthRefreshTokenRepository implements RefreshTokenRepositoryInterface
25
{
26
    /**
27
     * @var DqlQueryBuilderInterface
28
     */
29
    private $dqlQueryBuilder;
30
31
    /**
32
     * @var QueryServiceRouterInterface
33
     */
34
    private $queryService;
35
36
    /**
37
     * @var PersistenceServiceInterface
38
     */
39
    private $persistenceService;
40
41
    public function __construct(
42
        DqlQueryBuilderInterface $dqlQueryBuilder,
43
        QueryServiceRouterInterface $queryService,
44
        PersistenceServiceInterface $persistenceService
45
    ) {
46
        $this->dqlQueryBuilder = $dqlQueryBuilder;
47
        $this->queryService = $queryService;
48
        $this->persistenceService = $persistenceService;
49
    }
50
51
    public function getNewRefreshToken(): RefreshTokenEntityInterface
52
    {
53
        return new OauthRefreshToken();
54
    }
55
56
    public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshToken): void
57
    {
58
        $this->persistenceService->upsert($refreshToken);
59
    }
60
61
    /**
62
     * @param OauthRefreshTokenId $refreshTokenId
63
     */
64
    public function revokeRefreshToken($refreshTokenId): void
65
    {
66
        try {
67
            $refreshToken = $this->find($refreshTokenId);
68
        } catch (EmptyQueryResultException $e) {
69
            return;
70
        }
71
72
        $refreshToken->revoke();
73
        $this->persistenceService->upsert($refreshToken);
74
    }
75
76
    /**
77
     * @param OauthRefreshTokenId $refreshTokenId
78
     */
79
    public function isRefreshTokenRevoked($refreshTokenId): bool
80
    {
81
        try {
82
            $refreshToken = $this->find($refreshTokenId);
83
        } catch (EmptyQueryResultException $e) {
84
            return true;
85
        }
86
87
        return $refreshToken->isRevoked()
88
            ?: $refreshToken->getAccessToken()->isRevoked();
89
    }
90
91 View Code Duplication
    private function find(OauthRefreshTokenId $refreshTokenId): OauthRefreshToken
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        $dqlQuery = $this->dqlQueryBuilder->create(OauthRefreshToken::class)
94
            ->where('OauthAccessToken.identifier = :identifier')
95
            ->setParameter('identifier', $refreshTokenId)
96
            ->build();
97
98
        return $this->queryService->query($dqlQuery)->getSingleResult();
99
    }
100
}
101