Failed Conditions
Push — master ( 547d98...1bf1d9 )
by Florent
08:07
created

Client::withOwnerId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Core\Client;
15
16
use OAuth2Framework\Component\Core\DataBag\DataBag;
17
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
18
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwner;
19
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
20
21
/**
22
 * Class ClientCredentials.
23
 *
24
 * This class is used for every client types.
25
 * A client is a resource owner with a set of allowed grant types and can perform requests against
26
 * available endpoints.
27
 */
28
class Client implements ResourceOwner, \JsonSerializable
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
29
{
30
    /**
31
     * @var bool
32
     */
33
    private $deleted = false;
34
35
    /**
36
     * @var UserAccountId|null
37
     */
38
    private $ownerId = null;
39
40
    /**
41
     * @var ClientId|null
42
     */
43
    private $clientId = null;
44
45
    /**
46
     * @var DataBag
47
     */
48
    protected $parameters;
49
50
    /**
51
     * ClientCredentials constructor.
52
     */
53
    private function __construct()
54
    {
55
        $this->parameters = DataBag::create([]);
56
    }
57
58
    /**
59
     * @return Client
60
     */
61
    public static function createEmpty(): self
62
    {
63
        return new self();
64
    }
65
66
    /**
67
     * @param ClientId           $clientId
68
     * @param DataBag            $parameters
69
     * @param UserAccountId|null $ownerId
70
     *
71
     * @return Client
72
     */
73
    public function create(ClientId $clientId, DataBag $parameters, ? UserAccountId $ownerId): self
74
    {
75
        $clone = clone $this;
76
        $clone->clientId = $clientId;
77
        $clone->parameters = $parameters;
78
        $clone->ownerId = $ownerId;
79
80
        return $clone;
81
    }
82
83
    /**
84
     * @return UserAccountId|null
85
     */
86
    public function getOwnerId(): ? UserAccountId
87
    {
88
        return $this->ownerId;
89
    }
90
91
    /**
92
     * @param UserAccountId $ownerId
93
     *
94
     * @return Client
95
     */
96
    public function withOwnerId(UserAccountId $ownerId): self
97
    {
98
        if ($this->getOwnerId()->getValue() === $ownerId->getValue()) {
99
            return $this;
100
        }
101
102
        $clone = clone $this;
103
        $clone->ownerId = $ownerId;
104
105
        return $clone;
106
    }
107
108
    /**
109
     * @param DataBag $parameters
110
     *
111
     * @return Client
112
     */
113
    public function withParameters(DataBag $parameters): self
114
    {
115
        $clone = clone $this;
116
        $clone->parameters = $parameters;
117
118
        return $clone;
119
    }
120
121
    /**
122
     * @return Client
123
     */
124
    public function markAsDeleted(): self
125
    {
126
        $clone = clone $this;
127
        $clone->deleted = true;
128
129
        return $clone;
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isDeleted(): bool
136
    {
137
        return $this->deleted;
138
    }
139
140
    /**
141
     * @param string $grant_type
142
     *
143
     * @return bool
144
     */
145
    public function isGrantTypeAllowed(string $grant_type): bool
146
    {
147
        $grant_types = $this->has('grant_types') ? $this->get('grant_types') : [];
148
        if (!is_array($grant_types)) {
149
            throw new \InvalidArgumentException('The metadata "grant_types" must be an array.');
150
        }
151
152
        return in_array($grant_type, $grant_types);
153
    }
154
155
    /**
156
     * @param string $response_type
157
     *
158
     * @return bool
159
     */
160
    public function isResponseTypeAllowed(string $response_type): bool
161
    {
162
        $response_types = $this->has('response_types') ? $this->get('response_types') : [];
163
        if (!is_array($response_types)) {
164
            throw new \InvalidArgumentException('The metadata "response_types" must be an array.');
165
        }
166
167
        return in_array($response_type, $response_types);
168
    }
169
170
    /**
171
     * @return bool
172
     */
173
    public function isPublic(): bool
174
    {
175
        return 'none' === $this->getTokenEndpointAuthenticationMethod();
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getTokenEndpointAuthenticationMethod(): string
182
    {
183
        if ($this->has('token_endpoint_auth_method')) {
184
            return $this->get('token_endpoint_auth_method');
185
        }
186
187
        return 'client_secret_basic';
188
    }
189
190
    /**
191
     * @return int
192
     */
193
    public function getClientCredentialsExpiresAt(): int
194
    {
195
        if ($this->has('client_secret_expires_at')) {
196
            return $this->get('client_secret_expires_at');
197
        }
198
199
        return 0;
200
    }
201
202
    /**
203
     * @return bool
204
     */
205
    public function areClientCredentialsExpired(): bool
206
    {
207
        if (0 === $this->getClientCredentialsExpiresAt()) {
208
            return false;
209
        }
210
211
        return time() > $this->getClientCredentialsExpiresAt();
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217
    public function getPublicId(): ResourceOwnerId
218
    {
219
        if (null === $this->clientId) {
220
            throw new \RuntimeException('Client not initialized.');
221
        }
222
223
        return $this->clientId;
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    public function has(string $key): bool
230
    {
231
        return $this->parameters->has($key);
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function get(string $key)
238
    {
239
        if (!$this->has($key)) {
240
            throw new \InvalidArgumentException(sprintf('Configuration value with key "%s" does not exist.', $key));
241
        }
242
243
        return $this->parameters->get($key);
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function all(): array
250
    {
251
        $all = $this->parameters->all();
252
        $all['client_id'] = $this->getPublicId()->getValue();
253
254
        return $all;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260
    public function jsonSerialize()
261
    {
262
        $data = [
263
            'type' => get_class($this),
264
            'client_id' => $this->getPublicId()->getValue(),
265
            'owner_id' => $this->getOwnerId() ? $this->getOwnerId()->getValue() : null,
266
            'parameters' => (object) $this->all(),
267
            'is_deleted' => $this->isDeleted(),
268
        ];
269
270
        return $data;
271
    }
272
273
    /**
274
     * @param \stdClass $json
275
     *
276
     * @return Client
277
     */
278
    public static function createFromJson(\stdClass $json): self
279
    {
280
        $clientId = ClientId::create($json->client_id);
281
        $ownerId = null !== $json->owner_id ? UserAccountId::create($json->owner_id) : null;
282
        $parameters = DataBag::create((array) $json->parameters);
283
        $deleted = $json->is_deleted;
284
285
        $client = new self();
286
        $client->clientId = $clientId;
287
        $client->ownerId = $ownerId;
288
        $client->parameters = $parameters;
289
        $client->deleted = $deleted;
290
291
        return $client;
292
    }
293
}
294