Passed
Push — master ( fb60c6...a721e7 )
by Rutger
03:14
created

persistNewAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\components\repositories;
4
5
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
6
use League\OAuth2\Server\Entities\ClientEntityInterface;
7
use rhertogh\Yii2Oauth2Server\components\repositories\base\Oauth2BaseTokenRepository;
8
use rhertogh\Yii2Oauth2Server\components\repositories\traits\Oauth2ModelRepositoryTrait;
9
use rhertogh\Yii2Oauth2Server\helpers\DiHelper;
10
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2AccessTokenRepositoryInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2IdentifierInterface;
12
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2UserIdentifierInterface;
13
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface;
14
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface;
15
use yii\base\InvalidConfigException;
16
use yii\db\Connection;
17
18
class Oauth2AccessTokenRepository extends Oauth2BaseTokenRepository implements Oauth2AccessTokenRepositoryInterface
19
{
20
    use Oauth2ModelRepositoryTrait;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $_revocationValidation = true;
26
27
    /**
28
     * @inheritDoc
29
     * @return class-string<Oauth2AccessTokenInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Oauth2AccessTokenInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Oauth2AccessTokenInterface>.
Loading history...
30
     */
31 5
    public function getModelClass()
32
    {
33 5
        return Oauth2AccessTokenInterface::class;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     * @param Oauth2ClientInterface $clientEntity
39
     * @throws InvalidConfigException
40
     */
41 3
    public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
42
    {
43
        /** @var Oauth2AccessTokenInterface $accessToken */
44 3
        $accessToken = static::getNewTokenInternally([
0 ignored issues
show
Bug Best Practice introduced by
The method rhertogh\Yii2Oauth2Serve...getNewTokenInternally() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        /** @scrutinizer ignore-call */ 
45
        $accessToken = static::getNewTokenInternally([
Loading history...
45 3
            'client' => $clientEntity,
46 3
            'userIdentifier' => $userIdentifier,
47 3
        ]);
48
49 3
        $accessToken->setScopes($scopes);
50
51 3
        return $accessToken;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     * @throws \yii\db\Exception
57
     * @throws InvalidConfigException
58
     */
59 2
    public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
60
    {
61 2
        static::persistToken($accessTokenEntity);
0 ignored issues
show
Bug Best Practice introduced by
The method rhertogh\Yii2Oauth2Serve...ository::persistToken() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        static::/** @scrutinizer ignore-call */ 
62
                persistToken($accessTokenEntity);
Loading history...
Bug introduced by
$accessTokenEntity of type League\OAuth2\Server\Ent...essTokenEntityInterface is incompatible with the type rhertogh\Yii2Oauth2Serve...uth2IdentifierInterface expected by parameter $model of rhertogh\Yii2Oauth2Serve...ository::persistToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        static::persistToken(/** @scrutinizer ignore-type */ $accessTokenEntity);
Loading history...
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 1
    public function revokeAccessToken($tokenIdentifier)
68
    {
69 1
        static::revokeToken($tokenIdentifier);
0 ignored issues
show
Bug Best Practice introduced by
The method rhertogh\Yii2Oauth2Serve...pository::revokeToken() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        static::/** @scrutinizer ignore-call */ 
70
                revokeToken($tokenIdentifier);
Loading history...
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 2
    public function isAccessTokenRevoked($tokenIdentifier)
76
    {
77 2
        $validation = $this->getRevocationValidation();
78 2
        if ($validation === true) {
79 1
            return static::isTokenRevoked($tokenIdentifier);
0 ignored issues
show
Bug Best Practice introduced by
The method rhertogh\Yii2Oauth2Serve...itory::isTokenRevoked() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            return static::/** @scrutinizer ignore-call */ isTokenRevoked($tokenIdentifier);
Loading history...
80 2
        } elseif ($validation === false) {
0 ignored issues
show
introduced by
The condition $validation === false is always true.
Loading history...
81 1
            return false;
82 2
        } elseif (is_callable($validation)) {
83 1
            return call_user_func($validation, $tokenIdentifier);
84
        } else {
85 1
            throw new InvalidConfigException('Access Token Revocation Validation must be a boolean or callable');
86
        }
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92 2
    public function getRevocationValidation()
93
    {
94 2
        return $this->_revocationValidation;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 1
    public function setRevocationValidation($validation)
101
    {
102 1
        $this->_revocationValidation = $validation;
103 1
        return $this;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function revokeAccessTokensByUserId($userId)
110
    {
111
        $class = $this->getModelClass();
112
        /** @var class-string<Oauth2AccessTokenInterface> $className */
113
        $className = DiHelper::getValidatedClassName($class);
114
115
        $db = $className::getDb();
116
117
        $transaction = $db->beginTransaction();
118
119
        try {
120
            /** @var Oauth2AccessTokenInterface[] $accessTokens */
121
            $accessTokens = $className::findAllByUserId($userId);
122
            foreach ($accessTokens as $accessToken) {
123
                $accessToken->setRevokedStatus(true);
124
                $accessToken->persist();
125
            }
126
            $transaction->commit();
127
        } catch (\Exception $e) {
128
            $transaction->rollBack();
129
            throw $e;
130
        }
131
132
        return $accessTokens;
133
    }
134
}
135