Client::findAllByUserId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Client.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.2.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\behaviors\SplitToArrayBehavior;
19
use sweelix\oauth2\server\interfaces\ClientModelInterface;
20
use Yii;
21
use yii\validators\UrlValidator;
22
23
/**
24
 * This is the client model
25
 *
26
 * @author Philippe Gaultier <[email protected]>
27
 * @copyright 2010-2017 Philippe Gaultier
28
 * @license http://www.sweelix.net/license license
29
 * @version 1.2.0
30
 * @link http://www.sweelix.net
31
 * @package sweelix\oauth2\server\models
32
 * @since 1.0.0
33
 *
34
 * @property string $id
35
 * @property string $secret
36
 * @property string|array $redirectUri
37
 * @property array $grantTypes
38
 * @property string $userId
39
 * @property array $scopes
40
 * @property string $name
41
 * @property bool $isPublic
42
 */
43
class Client extends BaseModel implements ClientModelInterface
44
{
45
    /**
46
     * @inheritdoc
47
     */
48
    public function behaviors()
49 25
    {
50
        $behaviors = parent::behaviors();
51 25
        $behaviors['emptyArray'] = [
52 25
            'class' => EmptyArrayBehavior::class,
53 25
            'attributes' => ['scopes', 'grantTypes'],
54 25
        ];
55
        $behaviors['splitToArray'] = [
56 25
            'class' => SplitToArrayBehavior::class,
57 25
            'attributes' => ['redirectUri'],
58 25
        ];
59
        return $behaviors;
60 25
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function rules()
66 21
    {
67
        return [
68
            [['id', 'secret', 'name'], 'string'],
69 21
            [['redirectUri'], function($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70 21
                $data = $this->{$attribute};
71 4
72
                if (is_array($data) === false) {
73 4
                    $data = explode(' ', $data);
74 3
                }
75 3
                foreach($data as $redirectUri) {
76 4
                    $isLocalhost = strncmp('http://localhost', $redirectUri, 16);
77 4
                    $isSecureLocalhost = strncmp('https://localhost', $redirectUri, 17);
78 4
                    if (($isLocalhost !== 0) && ($isSecureLocalhost !== 0)) {
79 4
                        $validator = new UrlValidator();
80 4
                        if ($validator->validate($redirectUri, $error) === false) {
81 4
                            $this->addError($attribute, $error);
82 1
                            break;
83 1
                        }
84
                    }
85 4
                }
86 4
            }],
87 21
            [['scopes'], 'scope'],
88 21
            [['isPublic'], 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true],
89 21
            [['id', 'secret', 'isPublic'], 'required'],
90 21
        ];
91 21
    }
92
93
    /**
94
     * @return \sweelix\oauth2\server\interfaces\ClientServiceInterface
95
     * @throws \yii\base\InvalidConfigException
96
     */
97 25
    protected static function getDataService()
98
    {
99 25
        return Yii::createObject('sweelix\oauth2\server\interfaces\ClientServiceInterface');
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 21
    public function key()
106
    {
107 21
        return 'id';
108
    }
109
110
    /**
111
     * @return array definition of model attributes
112
     * @since 1.0.0
113
     */
114 21
    public function attributesDefinition()
115
    {
116
        return [
117 21
            'id' => 'string',
118 21
            'secret' => 'string',
119 21
            'redirectUri' => 'array',
120 21
            'grantTypes' => 'array',
121 21
            'userId' => 'string',
122 21
            'scopes' => 'array',
123 21
            'name' => 'string',
124 21
            'isPublic' => 'bool',
125 21
        ];
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131 22
    public static function findOne($id)
132
    {
133 22
        return self::getDataService()->findOne($id);
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 21
    public function save($runValidation = true, $attributes = null)
140
    {
141 21
        if ($runValidation && !$this->validate($attributes)) {
142 3
            Yii::info('Model not inserted due to validation error.', __METHOD__);
143 3
            $result = false;
144 3
        } else {
145 21
            $result = self::getDataService()->save($this, $attributes);
146
        }
147 21
        return $result;
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 1
    public function delete()
154
    {
155 1
        return self::getDataService()->delete($this);
156
    }
157
158
    /**
159
     * @inheritdoc
160
     */
161 5
    public function hasUser($userId)
162
    {
163 5
        return self::getDataService()->hasUser($this, $userId);
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169 3
    public function addUser($userId)
170
    {
171 3
        return self::getDataService()->addUser($this, $userId);
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177 4
    public function removeUser($userId)
178
    {
179 4
        return self::getDataService()->removeUser($this, $userId);
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185 1
    public static function findAllByUserId($userId)
186
    {
187 1
        return self::getDataService()->findAllByUserId($userId);
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193
    public static function findAll()
194
    {
195
        return self::getDataService()->findAll();
196
    }
197
}
198