AccessTokenRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

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

5 Methods

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