Failed Conditions
Push — master ( 3df084...2d5f15 )
by Florent
06:05
created

Client::getOwnerId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\ServerBundle\Entity;
15
16
use OAuth2Framework\Component\Core\Client\Client as ClientInterface;
17
use OAuth2Framework\Component\Core\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
20
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
21
22
/**
23
 * This class is used for every client types.
24
 * A client is a resource owner with a set of allowed grant types and can perform requests against
25
 * available endpoints.
26
 */
27
class Client implements ClientInterface
28
{
29
    protected $clientId;
30
    protected $ownerId;
31
    protected $parameter;
32
    protected $deleted;
33
34
    public function __construct(ClientId $clientId, DataBag $parameters, ?UserAccountId $ownerId)
35
    {
36
        $this->clientId = $clientId;
37
        $this->parameter = $parameters;
38
        $this->ownerId = $ownerId;
39
        $this->deleted = false;
40
    }
41
42
    public function getClientId(): ClientId
43
    {
44
        return $this->clientId;
45
    }
46
47
    public function getOwnerId(): ?UserAccountId
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
48
    {
49
        return $this->ownerId;
50
    }
51
52
    public function setParameter(DataBag $parameter): void
53
    {
54
        $this->parameter = $parameter;
55
    }
56
57
    public function markAsDeleted(): void
58
    {
59
        $this->deleted = true;
60
    }
61
62
    public function isDeleted(): bool
63
    {
64
        return $this->deleted;
65
    }
66
67
    public function isGrantTypeAllowed(string $grant_type): bool
68
    {
69
        $grant_types = $this->has('grant_types') ? $this->get('grant_types') : [];
70
        if (!\is_array($grant_types)) {
71
            throw new \InvalidArgumentException('The metadata "grant_types" must be an array.');
72
        }
73
74
        return \in_array($grant_type, $grant_types, true);
75
    }
76
77
    public function isResponseTypeAllowed(string $response_type): bool
78
    {
79
        $response_types = $this->has('response_types') ? $this->get('response_types') : [];
80
        if (!\is_array($response_types)) {
81
            throw new \InvalidArgumentException('The metadata "response_types" must be an array.');
82
        }
83
84
        return \in_array($response_type, $response_types, true);
85
    }
86
87
    public function isPublic(): bool
88
    {
89
        return 'none' === $this->getTokenEndpointAuthenticationMethod();
90
    }
91
92
    public function getTokenEndpointAuthenticationMethod(): string
93
    {
94
        if ($this->has('token_endpoint_auth_method')) {
95
            return $this->get('token_endpoint_auth_method');
96
        }
97
98
        return 'client_secret_basic';
99
    }
100
101
    public function getClientCredentialsExpiresAt(): int
102
    {
103
        if ($this->has('client_secret_expires_at')) {
104
            return $this->get('client_secret_expires_at');
105
        }
106
107
        return 0;
108
    }
109
110
    public function areClientCredentialsExpired(): bool
111
    {
112
        if (0 === $this->getClientCredentialsExpiresAt()) {
113
            return false;
114
        }
115
116
        return \time() > $this->getClientCredentialsExpiresAt();
117
    }
118
119
    public function getPublicId(): ResourceOwnerId
120
    {
121
        if (null === $this->clientId) {
122
            throw new \RuntimeException('Client not initialized.');
123
        }
124
125
        return $this->clientId;
126
    }
127
128
    public function has(string $key): bool
129
    {
130
        return $this->parameter->has($key);
131
    }
132
133
    public function get(string $key)
134
    {
135
        if (!$this->has($key)) {
136
            throw new \InvalidArgumentException(\Safe\sprintf('Configuration value with key "%s" does not exist.', $key));
137
        }
138
139
        return $this->parameter->get($key);
140
    }
141
142
    public function all(): array
143
    {
144
        $all = $this->parameter->all();
145
        $all['client_id'] = $this->getPublicId()->getValue();
146
147
        return $all;
148
    }
149
150
    public function jsonSerialize()
151
    {
152
        $data = [
153
            'client_id' => $this->getPublicId()->getValue(),
154
            'owner_id' => $this->getOwnerId() ? $this->getOwnerId()->getValue() : null,
155
            'parameters' => (object) $this->all(),
156
            'is_deleted' => $this->isDeleted(),
157
        ];
158
159
        return $data;
160
    }
161
}
162