Completed
Push — devel ( 5aa576...293fc2 )
by Philippe
07:12 queued 04:28
created

AccessToken::findAllByUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * AccessToken.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.1.0
11
 * @link http://www.sweelix.net
12
 * @package sweelix\oauth2\server\models
13
 */
14
15
namespace sweelix\oauth2\server\models;
16
17
use sweelix\oauth2\server\behaviors\EmptyArrayBehavior;
18
use sweelix\oauth2\server\interfaces\AccessTokenModelInterface;
19
use Yii;
20
21
/**
22
 * This is the access token model
23
 *
24
 * @author Philippe Gaultier <[email protected]>
25
 * @copyright 2010-2017 Philippe Gaultier
26
 * @license http://www.sweelix.net/license license
27
 * @version 1.1.0
28
 * @link http://www.sweelix.net
29
 * @package sweelix\oauth2\server\models
30
 * @since 1.0.0
31
 *
32
 * @property string $id
33
 * @property string $clientId
34
 * @property string $userId
35
 * @property string $expiry
36
 * @property array $scopes
37
 */
38
class AccessToken extends BaseModel implements AccessTokenModelInterface
39
{
40
41
    /**
42
     * @inheritdoc
43
     */
44 9
    public function behaviors()
45
    {
46 9
        $behaviors = parent::behaviors();
47 9
        $behaviors['emptyArray'] = [
48 9
            'class' => EmptyArrayBehavior::className(),
49 9
            'attributes' => ['scopes'],
50
        ];
51 9
        return $behaviors;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 9
    public function rules()
58
    {
59
        return [
60 9
            [['id', 'clientId'], 'string'],
61 9
            [['scopes'], 'scope'],
62 9
        ];
63
    }
64
65
    /**
66
     * @return \sweelix\oauth2\server\interfaces\AccessTokenServiceInterface
67
     */
68 10
    protected static function getDataService()
69
    {
70 10
        return Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenServiceInterface');
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 9
    public function key()
77
    {
78 9
        return 'id';
79
    }
80
81
    /**
82
     * @return array definition of model attributes
83
     * @since 1.0.0
84
     */
85 9
    public function attributesDefinition()
86
    {
87
        return [
88 9
            'id' => 'string',
89 9
            'clientId' => 'string',
90 9
            'userId' => 'string',
91 9
            'expiry' => 'string',
92 9
            'scopes' => 'array',
93 9
        ];
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 9
    public static function findOne($id)
100
    {
101 9
        return self::getDataService()->findOne($id);
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 9
    public function save($runValidation = true, $attributes = null)
108
    {
109 9
        if ($runValidation && !$this->validate($attributes)) {
110 1
            Yii::info('Model not inserted due to validation error.', __METHOD__);
111 1
            $result = false;
112 1
        } else {
113 9
            $result = self::getDataService()->save($this, $attributes);
114
        }
115 9
        return $result;
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121 2
    public function delete()
122
    {
123 2
        return self::getDataService()->delete($this);
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 2
    public static function findAllByUserId($userId)
130
    {
131 2
        return self::getDataService()->findAllByUserId($userId);
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137 2
    public static function findAllByClientId($clientId)
138
    {
139 2
        return self::getDataService()->findAllByClientId($clientId);
140
    }
141
}
142