Passed
Push — master ( 5d259d...069a18 )
by Conrad
02:00
created

AccessTokenRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

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

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\AccessToken;
6
use AdvancedLearning\Oauth2Server\Models\AccessToken as AccessTokenModel;
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 = AccessTokenModel::create();
19
20
        $newToken->Identifier = $accessTokenEntity->getIdentifier();
21
        $newToken->MemberID = $accessTokenEntity->getClient()->getIdentifier();
0 ignored issues
show
Documentation introduced by Conrad Dobbs
The property MemberID does not exist on object<AdvancedLearning\...ver\Models\AccessToken>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
22
        $newToken->Name = $accessTokenEntity->getClient()->getName();
23
        $newToken->ExpiryDateTime = $accessTokenEntity->getExpiryDateTime()->format('Y-m-d H:i');
0 ignored issues
show
Bug introduced by Conrad Dobbs
The property ExpiryDateTime does not seem to exist. Did you mean ExpiryDatetime?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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 AccessToken($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)
61
    {
62
        $token = $this->findToken($tokenId);
63
64
        // return true if there is no matching token
65
        return !$token || $token->Revoked;
66
    }
67
68
    /**
69
     * Find the Token for passed id.
70
     *
71
     * @param mixed $tokenId The id of the token.
72
     *
73
     * @return AccessTokenModel
74
     */
75
    public function findToken($tokenId)
76
    {
77
        return AccessTokenModel::get()->byID($tokenId);
78
    }
79
}
80