Passed
Branch refactor_config_class (788a46)
by Jens
08:38
created

Credentials   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 31
dl 0
loc 291
ccs 70
cts 72
cp 0.9722
rs 9.8
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A setProject() 0 5 1
A getProject() 0 3 1
A getAnonymousId() 0 3 1
A setGrantType() 0 5 1
A setAnonymousId() 0 5 1
A getClientId() 0 3 1
A setBearerToken() 0 4 1
A getBearerToken() 0 3 1
A getGrantType() 0 3 1
A getPassword() 0 3 1
A getRefreshToken() 0 3 1
B check() 0 15 6
A setClientSecret() 0 5 1
A setUsername() 0 5 1
A setClientId() 0 5 1
A getClientSecret() 0 3 1
A getScope() 0 19 4
A setScope() 0 11 3
A getUsername() 0 3 1
A setRefreshToken() 0 5 1
A setPassword() 0 5 1
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Client;
7
8
use Commercetools\Core\ConfigObject;
9
use Commercetools\Core\Error\InvalidArgumentException;
10
use Commercetools\Core\Error\Message;
11
12
class Credentials extends ConfigObject
13
{
14
    const USER_NAME = 'username';
15
    const PASSWORD = 'password';
16
    const REFRESH_TOKEN = 'refresh_token';
17
    const BEARER_TOKEN = 'bearer_token';
18
    const ANONYMOUS_ID = 'anonymous_id';
19
    const GRANT_TYPE = 'grant_type';
20
21
    const GRANT_TYPE_CLIENT = 'client_credentials';
22
    const GRANT_TYPE_PASSWORD = 'password';
23
    const GRANT_TYPE_REFRESH = 'refresh_token';
24
    const GRANT_TYPE_ANONYMOUS = 'anonymous_token';
25
    const GRANT_TYPE_BEARER_TOKEN = 'bearer_token';
26
27
    /**
28
     * @var string
29
     */
30
    protected $clientSecret;
31
32
    /**
33
     * @var string
34
     */
35
    protected $clientId;
36
37
    /**
38
     * @var string
39
     */
40
    protected $project;
41
42
    /**
43
     * @var array
44
     */
45
    protected $scope = ['manage_project'];
46
47
    protected $grantType = self::GRANT_TYPE_CLIENT;
48
49
    /**
50
     * @var string
51
     */
52
    protected $username;
53
54
    /**
55
     * @var string
56
     */
57
    protected $password;
58
59
    /**
60
     * @var string
61
     */
62
    protected $refreshToken;
63
64
    /**
65
     * @var string
66
     */
67
    protected $bearerToken;
68
69
    /**
70
     * @var string
71
     */
72
    protected $anonymousId;
73
74
    /**
75
     * @return string
76
     */
77 87
    public function getClientSecret()
78
    {
79 87
        return $this->clientSecret;
80
    }
81
82
    /**
83
     * @param string $clientSecret
84
     * @return Credentials
85
     */
86 92
    public function setClientSecret($clientSecret)
87
    {
88 92
        $this->clientSecret = $clientSecret;
89
90 92
        return $this;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 91
    public function getClientId()
97
    {
98 91
        return $this->clientId;
99
    }
100
101
    /**
102
     * @param string $clientId
103
     * @return Credentials
104
     */
105 92
    public function setClientId($clientId)
106
    {
107 92
        $this->clientId = $clientId;
108
109 92
        return $this;
110
    }
111
112
    /**
113
     * @return string
114
     */
115 617
    public function getProject()
116
    {
117 617
        return $this->project;
118
    }
119
120
    /**
121
     * @param string $project
122
     * @return Credentials
123
     */
124 102
    public function setProject($project)
125
    {
126 102
        $this->project = $project;
127
128 102
        return $this;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 578
    public function getScope()
135
    {
136 578
        $scope = $this->scope;
137 578
        $project = $this->getProject();
138
139 578
        $permissions = [];
140 578
        foreach ($scope as $key => $value) {
141 573
            if (is_numeric($key)) { // scope defined as string e.g. scope:project_key
142 572
                if (strpos($value, ':') === false) { // scope without project key
143 571
                    $value = $value . ':' . $project;
144
                }
145 572
                $permissions[] = $value;
146
            } else { // scope defined as array
147 573
                $permissions[] = $key . ':' . $value;
148
            }
149
        }
150 578
        $scope = implode(' ', $permissions);
151
152 578
        return $scope;
153
    }
154
155
    /**
156
     * @param string $scope
157
     * @return $this
158
     */
159 55
    public function setScope($scope)
160
    {
161 55
        if (empty($scope)) {
162 5
            $scope = [];
163
        }
164 55
        if (!is_array($scope)) {
165 24
            $scope = explode(' ', $scope);
166
        }
167 55
        $this->scope = $scope;
168
169 55
        return $this;
170
    }
171
172
    /**
173
     * @return string
174
     */
175 577
    public function getGrantType()
176
    {
177 577
        return $this->grantType;
178
    }
179
180
    /**
181
     * @param string $grantType
182
     * @return $this
183
     */
184 36
    public function setGrantType($grantType)
185
    {
186 36
        $this->grantType = $grantType;
187
188 36
        return $this;
189
    }
190
191
    /**
192
     * @return string
193
     */
194 19
    public function getUsername()
195
    {
196 19
        return $this->username;
197
    }
198
199
    /**
200
     * @param string $username
201
     * @return $this
202
     */
203 24
    public function setUsername($username)
204
    {
205 24
        $this->username = $username;
206
207 24
        return $this;
208
    }
209
210
    /**
211
     * @return string
212
     */
213 19
    public function getPassword()
214
    {
215 19
        return $this->password;
216
    }
217
218
    /**
219
     * @param string $password
220
     * @return $this
221
     */
222 24
    public function setPassword($password)
223
    {
224 24
        $this->password = $password;
225
226 24
        return $this;
227
    }
228
229
    /**
230
     * @return string
231
     */
232 28
    public function getRefreshToken()
233
    {
234 28
        return $this->refreshToken;
235
    }
236
237
    /**
238
     * @param string $refreshToken
239
     * @return $this
240
     */
241 28
    public function setRefreshToken($refreshToken)
242
    {
243 28
        $this->refreshToken = $refreshToken;
244
245 28
        return $this;
246
    }
247
248
    /**
249
     * @return string
250
     */
251 11
    public function getAnonymousId()
252
    {
253 11
        return $this->anonymousId;
254
    }
255
256
    /**
257
     * @param string $anonymousId
258
     * @return $this
259
     */
260 4
    public function setAnonymousId($anonymousId)
261
    {
262 4
        $this->anonymousId = $anonymousId;
263
264 4
        return $this;
265
    }
266
267
    /**
268
     * @return string
269
     */
270 2
    public function getBearerToken()
271
    {
272 2
        return $this->bearerToken;
273
    }
274
275
    /**
276
     * @param string $bearerToken
277
     * @return Credentials
278
     */
279 2
    public function setBearerToken($bearerToken)
280
    {
281 2
        $this->bearerToken = $bearerToken;
282 2
        return $this;
283
    }
284
285
    /**
286
     * @return bool
287
     */
288 88
    public function check()
289
    {
290 88
        if (is_null($this->getClientId()) && $this->getGrantType() !== self::GRANT_TYPE_BEARER_TOKEN) {
0 ignored issues
show
introduced by
The condition is_null($this->getClientId()) is always false.
Loading history...
291 4
            throw new InvalidArgumentException(Message::NO_CLIENT_ID);
292
        }
293
294 84
        if (is_null($this->getClientSecret()) && $this->getGrantType() !== self::GRANT_TYPE_BEARER_TOKEN) {
0 ignored issues
show
introduced by
The condition is_null($this->getClientSecret()) is always false.
Loading history...
295
            throw new InvalidArgumentException(Message::NO_CLIENT_SECRET);
296
        }
297
298 84
        if (is_null($this->getProject())) {
0 ignored issues
show
introduced by
The condition is_null($this->getProject()) is always false.
Loading history...
299
            throw new InvalidArgumentException(Message::NO_PROJECT_ID);
300
        }
301
302 84
        return true;
303
    }
304
}
305