Completed
Push — devel ( b03b2f...7508db )
by Philippe
05:13
created

Client::removeUser()   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
 * Client.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2016 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version XXX
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\ClientModelInterface;
19
use Yii;
20
21
/**
22
 * This is the client model
23
 *
24
 * @author Philippe Gaultier <[email protected]>
25
 * @copyright 2010-2016 Philippe Gaultier
26
 * @license http://www.sweelix.net/license license
27
 * @version XXX
28
 * @link http://www.sweelix.net
29
 * @package sweelix\oauth2\server\models
30
 * @since XXX
31
 *
32
 * @property string $id
33
 * @property string $secret
34
 * @property string $redirectUri
35
 * @property array $grantTypes
36
 * @property string $userId
37
 * @property array $scopes
38
 * @property string $name
39
 * @property bool $isPublic
40
 */
41
class Client extends BaseModel implements ClientModelInterface
42
{
43
44
    /**
45
     * @inheritdoc
46
     */
47 5
    public function behaviors()
48
    {
49 5
        $behaviors = parent::behaviors();
50 5
        $behaviors['emptyArray'] = [
51 5
            'class' => EmptyArrayBehavior::className(),
52 5
            'attributes' => ['scopes', 'grantTypes'],
53
        ];
54 5
        return $behaviors;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 5
    public function rules()
61
    {
62
        return [
63 5
            [['id', 'secret', 'userId', 'name'], 'string'],
64 5
            [['redirectUri'], 'url', 'when' => function($model) {
65 1
                $isLocalhost = strncmp('http://localhost', $model->redirectUri, 16);
66 1
                $isSecureLocalhost = strncmp('https://localhost', $model->redirectUri, 17);
67 1
                return (($isLocalhost !== 0) && ($isSecureLocalhost !== 0));
68 5
            }],
69 5
            [['scopes'], 'scope'],
70 5
            [['isPublic'], 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true],
71 5
            [['id', 'secret', 'isPublic'], 'required'],
72 5
        ];
73
    }
74
75
    /**
76
     * @return \sweelix\oauth2\server\interfaces\ClientServiceInterface
77
     */
78 5
    protected static function getDataService()
79
    {
80 5
        return Yii::createObject('sweelix\oauth2\server\interfaces\ClientServiceInterface');
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 5
    public function key()
87
    {
88 5
        return 'id';
89
    }
90
91
    /**
92
     * @return array definition of model attributes
93
     * @since XXX
94
     */
95 5
    public function attributesDefinition()
96
    {
97
        return [
98 5
            'id' => 'string',
99 5
            'secret' => 'string',
100 5
            'redirectUri' => 'string',
101 5
            'grantTypes' => 'array',
102 5
            'userId' => 'string',
103 5
            'scopes' => 'array',
104 5
            'name' => 'string',
105 5
            'isPublic' => 'bool',
106 5
        ];
107
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112 4
    public static function findOne($id)
113
    {
114 4
        return self::getDataService()->findOne($id);
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120 5
    public function save($runValidation = true, $attributes = null)
121
    {
122 5
        if ($runValidation && !$this->validate($attributes)) {
123 2
            Yii::info('Model not inserted due to validation error.', __METHOD__);
124 2
            $result = false;
125 2
        } else {
126 5
            $result = self::getDataService()->save($this, $attributes);
127
        }
128 5
        return $result;
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134 1
    public function delete()
135
    {
136 1
        return self::getDataService()->delete($this);
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142 1
    public function hasUser($userId)
143
    {
144 1
        return self::getDataService()->hasUser($this, $userId);
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150 1
    public function addUser($userId)
151
    {
152 1
        return self::getDataService()->addUser($this, $userId);
153
    }
154
155 1
    public function removeUser($userId)
156
    {
157 1
        return self::getDataService()->removeUser($this, $userId);
158
    }
159
160
}
161