Passed
Push — master ( de5894...b396dd )
by Conrad
07:34
created

RefreshTokenRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 8
dl 0
loc 68
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A persistNewRefreshToken() 0 20 2
A getNewRefreshToken() 0 4 1
A revokeRefreshToken() 0 7 2
A isRefreshTokenRevoked() 0 7 2
A findToken() 0 4 1
1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\Repositories;
4
5
6
use AdvancedLearning\Oauth2Server\Entities\RefreshTokenEntity;
7
use AdvancedLearning\Oauth2Server\Models\AccessToken;
8
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
9
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
10
11
class RefreshTokenRepository implements RefreshTokenRepositoryInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
17
    {
18
        $newToken = AccessToken::create();
19
20
        $newToken->Identifier = $refreshTokenEntity->getIdentifier();
21
        $newToken->Name = $refreshTokenEntity->getAccessToken()->getClient()->getName();
22
        $newToken->ExpiryDateTime = $refreshTokenEntity->getExpiryDateTime()->format('Y-m-d H:i');
23
24
        // turn scopes into space separated string
25
        $newToken->Scopes = '';
26
        $separator = '';
27
        foreach ($refreshTokenEntity->getAccessToken()->getScopes() as $scope) {
28
            $newToken->Scopes .= $separator . $scope->getIdentifier();
29
            $separator = ' ';
30
        }
31
32
        $newToken->write();
33
34
        return $newToken;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getNewRefreshToken()
41
    {
42
        return new RefreshTokenEntity();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function revokeRefreshToken($tokenId)
49
    {
50
        if ($token = $this->findToken($tokenId)) {
51
            $token->Revoked = true;
52
            $token->write();
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function isRefreshTokenRevoked($tokenId)
60
    {
61
        $token = $this->findToken($tokenId);
62
63
        // return true if there is no matching token
64
        return empty($token) || $token->Revoked;
65
    }
66
67
    /**
68
     * Find the Token for passed id.
69
     *
70
     * @param string $tokenId The id of the token.
71
     *
72
     * @return AccessToken|null
73
     */
74
    public function findToken(string $tokenId): ?AccessToken
75
    {
76
        return AccessToken::get()->filter(['Identifier' => $tokenId])->first();
77
    }
78
}
79