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

Oauth2RefreshToken::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\models;
4
5
use DateTimeImmutable;
6
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
7
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface;
8
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2RefreshTokenInterface;
9
use rhertogh\Yii2Oauth2Server\models\behaviors\DateTimeBehavior;
10
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2EntityIdentifierTrait;
11
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2ExpiryDateTimeTrait;
12
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2TokenTrait;
13
use yii\base\InvalidConfigException;
14
use yii\helpers\ArrayHelper;
15
16
/**
17
 * @property DateTimeImmutable $expiry_date_time
18
 * @property Oauth2AccessTokenInterface[] $accessTokenRelation
19
 */
20
class Oauth2RefreshToken extends base\Oauth2RefreshToken implements Oauth2RefreshTokenInterface
21
{
22
    use Oauth2EntityIdentifierTrait;
23
    use Oauth2ExpiryDateTimeTrait;
24
    use Oauth2TokenTrait;
25
26
    /////////////////////////////
27
    /// ActiveRecord Settings ///
28
    /////////////////////////////
29
30
    /**
31
     * @inheritDoc
32
     */
33 12
    public function behaviors()
34
    {
35 12
        return ArrayHelper::merge(parent::behaviors(), [
36 12
            'dateTimeBehavior' => DateTimeBehavior::class
37 12
        ]);
38
    }
39
40
    /**
41
     * Wrapper for parent's getAccessToken() relation to avoid name conflicts
42
     */
43 4
    public function getAccessTokenRelation()
44
    {
45 4
        return parent::getAccessToken();
46
    }
47
48
    ////////////////////////
49
    /// Static Functions ///
50
    ////////////////////////
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public static function findAllByAccessTokenIds($accessTokenIds)
56
    {
57
        return static::find()->where(['access_token_id' => $accessTokenIds])->all();
58
    }
59
60
    /////////////////////////
61
    /// Getters & Setters ///
62
    /////////////////////////
63
64
    /**
65
     * @inheritDoc
66
     */
67 12
    public function __set($name, $value)
68
    {
69 12
        if ($name === 'access_token_id' && $this->isRelationPopulated('accessTokenRelation')) {
70 2
            unset($this['accessTokenRelation']);
71
        }
72 12
        parent::__set($name, $value);
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78 12
    public function setAttribute($name, $value)
79
    {
80 12
        if ($name === 'access_token_id' && $this->isRelationPopulated('accessTokenRelation')) {
81 1
            unset($this['accessTokenRelation']);
82
        }
83 12
        parent::setAttribute($name, $value);
84
    }
85
86
    /**
87
     * @inheritDoc
88
     * @throws InvalidConfigException
89
     */
90 4
    public function setAccessToken(AccessTokenEntityInterface $accessToken)
91
    {
92 4
        if (!($accessToken instanceof Oauth2AccessTokenInterface)) {
93 1
            throw new InvalidConfigException(
94 1
                get_class($accessToken) . ' must implement ' . Oauth2AccessTokenInterface::class
95 1
            );
96
        }
97
98 3
        $this->access_token_id = $accessToken->getPrimaryKey();
99 3
        $this->populateRelation('accessTokenRelation', $accessToken);
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105 3
    public function getAccessToken()
106
    {
107 3
        return $this->accessTokenRelation;
108
    }
109
}
110