-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientBuilder.php
182 lines (151 loc) · 4.05 KB
/
ClientBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Kcharfi\Laravel\Keycloak\Admin;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\HandlerStack;
use Kcharfi\Laravel\Keycloak\Admin\Enums\GrantType;
use Kcharfi\Laravel\Keycloak\Admin\Guzzle\DefaultHeadersMiddleware;
use Kcharfi\Laravel\Keycloak\Admin\Guzzle\TokenMiddleware;
use Kcharfi\Laravel\Keycloak\Admin\Hydrator\Hydrator;
use Kcharfi\Laravel\Keycloak\Admin\Resources\ResourceFactory;
use Kcharfi\Laravel\Keycloak\Admin\Token\TokenManager;
class ClientBuilder
{
private $guzzle;
private $config;
private $token;
private $tokenManager;
public function __construct()
{
}
/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @param Config $config
*/
public function setConfig(Config $config): void
{
$this->config = $config;
}
/**
* @param Config $config
* @return ClientBuilder
*/
public function withConfig(Config $config): self
{
$this->config = $config;
return $this;
}
/**
* @param ClientInterface $guzzle
* @return ClientBuilder
*/
public function withGuzzle(ClientInterface $guzzle): self
{
$this->guzzle = $guzzle;
return $this;
}
/**
* @param string $realm
* @return ClientBuilder
*/
public function withRealm(string $realm): self
{
$this->realm = $realm;
return $this;
}
/**
* @param string $url
* @return ClientBuilder
*/
public function withServerUrl(string $url): self
{
$this->serverUrl = $url;
return $this;
}
/**
* @param null|string $clientId
* @return ClientBuilder
*/
public function withClientId(?string $clientId): self
{
$this->getConfig()->setClientId($clientId);
return $this;
}
/**
* @param null|string $secret
* @return ClientBuilder
*/
public function withClientSecret(?string $secret): self
{
$this->getConfig()->setClientSecret($secret);
return $this;
}
/**
* @param null|string $grantType
* @return ClientBuilder
*/
public function withGrantType(?string $grantType = GrantType::PASSWORD): self
{
$this->getConfig()->setGrantType($grantType);
return $this;
}
/**
* @param string $username
* @return ClientBuilder
*/
public function withUsername(string $username): self
{
$this->getConfig()->setUsername($username);
return $this;
}
/**
* @param string $password
* @return ClientBuilder
*/
public function withPassword(string $password): self
{
$this->getConfig()->setPassword($password);
return $this;
}
/**
* @param null|string $token
* @return ClientBuilder
*/
public function withAuthToken(?string $token): self
{
$this->token = $token;
return $this;
}
public function build()
{
$guzzle = $this->guzzle ?? $this->buildGuzzle();
if (null == ($tokenManager = $this->tokenManager)) {
$tokenManager = $this->buildTokenManager($guzzle);
}
$tokenMiddleware = new TokenMiddleware($tokenManager, $this->getConfig()->getRealm());
$stack = HandlerStack::create();
$stack->push($tokenMiddleware);
$stack->push(new DefaultHeadersMiddleware());
$client = new GuzzleClient(['http_errors' => false, 'handler' => $stack,
'base_uri' => $this->getConfig()->getUrl()]);
$factory = new ResourceFactory($client, new Hydrator());
return new Client($factory, $this->getConfig()->getRealm(), $this->getConfig()->getClientId());
}
/**
* @return ClientInterface
*/
private function buildGuzzle()
{
return new GuzzleClient(['base_uri' => $this->getConfig()->getUrl()]);
}
private function buildTokenManager(ClientInterface $guzzle)
{
return new TokenManager($this->getConfig(), $guzzle);
}
}