• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

prooph / event-store-client / 9555551525

17 Jun 2024 10:16PM UTC coverage: 70.262% (-1.1%) from 71.395%
9555551525

push

github

prolic
update coveralls repo token

3466 of 4933 relevant lines covered (70.26%)

67.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.19
/src/UserManagement/UsersClient.php
1
<?php
2

3
/**
4
 * This file is part of `prooph/event-store-client`.
5
 * (c) 2018-2024 Alexander Miertsch <kontakt@codeliner.ws>
6
 * (c) 2018-2024 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Prooph\EventStoreClient\UserManagement;
15

16
use Amp\DeferredFuture;
17
use Amp\Http\Client\Response;
18
use Prooph\EventStore\EndPoint;
19
use Prooph\EventStore\Exception\UnexpectedValueException;
20
use Prooph\EventStore\Transport\Http\EndpointExtensions;
21
use Prooph\EventStore\Transport\Http\HttpStatusCode;
22
use Prooph\EventStore\UserCredentials;
23
use Prooph\EventStore\UserManagement\ChangePasswordDetails;
24
use Prooph\EventStore\UserManagement\ResetPasswordDetails;
25
use Prooph\EventStore\UserManagement\UserCreationInformation;
26
use Prooph\EventStore\UserManagement\UserDetails;
27
use Prooph\EventStore\UserManagement\UserUpdateInformation;
28
use Prooph\EventStore\Util\Json;
29
use Prooph\EventStoreClient\Exception\UserCommandConflict;
30
use Prooph\EventStoreClient\Exception\UserCommandFailed;
31
use Prooph\EventStoreClient\Transport\Http\HttpClient;
32
use Throwable;
33

34
/** @internal */
35
class UsersClient
36
{
37
    private readonly HttpClient $client;
38

39
    private readonly EndpointExtensions $httpSchema;
40

41
    public function __construct(int $operationTimeout, bool $tlsTerminatedEndpoint, bool $verifyPeer)
42
    {
43
        $this->client = new HttpClient($operationTimeout, $verifyPeer);
161✔
44
        $this->httpSchema = EndpointExtensions::useHttps($tlsTerminatedEndpoint);
161✔
45
    }
46

47
    public function enable(
48
        EndPoint $endPoint,
49
        string $login,
50
        ?UserCredentials $userCredentials = null
51
    ): void {
52
        $this->sendPost(
1✔
53
            EndpointExtensions::formatStringToHttpUrl(
1✔
54
                $endPoint,
1✔
55
                $this->httpSchema,
1✔
56
                '/users/%s/command/enable',
1✔
57
                \urlencode($login)
1✔
58
            ),
1✔
59
            '',
1✔
60
            $userCredentials,
1✔
61
            HttpStatusCode::Ok
1✔
62
        );
1✔
63
    }
64

65
    public function disable(
66
        EndPoint $endPoint,
67
        string $login,
68
        ?UserCredentials $userCredentials = null
69
    ): void {
70
        $this->sendPost(
1✔
71
            EndpointExtensions::formatStringToHttpUrl(
1✔
72
                $endPoint,
1✔
73
                $this->httpSchema,
1✔
74
                '/users/%s/command/disable',
1✔
75
                \urlencode($login)
1✔
76
            ),
1✔
77
            '',
1✔
78
            $userCredentials,
1✔
79
            HttpStatusCode::Ok
1✔
80
        );
1✔
81
    }
82

83
    public function delete(
84
        EndPoint $endPoint,
85
        string $login,
86
        ?UserCredentials $userCredentials = null
87
    ): void {
88
        $this->sendDelete(
138✔
89
            EndpointExtensions::formatStringToHttpUrl(
138✔
90
                $endPoint,
138✔
91
                $this->httpSchema,
138✔
92
                '/users/%s',
138✔
93
                \urlencode($login)
138✔
94
            ),
138✔
95
            $userCredentials,
138✔
96
            HttpStatusCode::Ok
138✔
97
        );
138✔
98
    }
99

100
    /**
101
     * @return list<UserDetails>
102
     */
103
    public function listAll(
104
        EndPoint $endPoint,
105
        ?UserCredentials $userCredentials = null
106
    ): array {
107
        $body = $this->sendGet(
2✔
108
            EndpointExtensions::rawUrlToHttpUrl($endPoint, $this->httpSchema, '/users/'),
2✔
109
            $userCredentials,
2✔
110
            HttpStatusCode::Ok
2✔
111
        );
2✔
112

113
        if ('' === $body) {
2✔
114
            throw new UnexpectedValueException('No content received');
×
115
        }
116

117
        $data = Json::decode($body);
2✔
118

119
        return \array_map(
2✔
120
            fn (array $entry): UserDetails => UserDetails::fromArray($entry),
2✔
121
            $data['data']
2✔
122
        );
2✔
123
    }
124

125
    public function getCurrentUser(
126
        EndPoint $endPoint,
127
        ?UserCredentials $userCredentials = null
128
    ): UserDetails {
129
        $body = $this->sendGet(
2✔
130
            EndpointExtensions::rawUrlToHttpUrl(
2✔
131
                $endPoint,
2✔
132
                $this->httpSchema,
2✔
133
                '/users/$current'
2✔
134
            ),
2✔
135
            $userCredentials,
2✔
136
            HttpStatusCode::Ok
2✔
137
        );
2✔
138

139
        if ('' === $body) {
2✔
140
            throw new UnexpectedValueException('No content received');
×
141
        }
142

143
        $data = Json::decode($body);
2✔
144

145
        return UserDetails::fromArray($data['data']);
2✔
146
    }
147

148
    public function getUser(
149
        EndPoint $endPoint,
150
        string $login,
151
        ?UserCredentials $userCredentials = null
152
    ): UserDetails {
153
        $body = $this->sendGet(
3✔
154
            EndpointExtensions::formatStringToHttpUrl(
3✔
155
                $endPoint,
3✔
156
                $this->httpSchema,
3✔
157
                '/users/%s',
3✔
158
                \urlencode($login)
3✔
159
            ),
3✔
160
            $userCredentials,
3✔
161
            HttpStatusCode::Ok
3✔
162
        );
3✔
163

164
        if ('' === $body) {
3✔
165
            throw new UnexpectedValueException('No content received');
×
166
        }
167

168
        $data = Json::decode($body);
3✔
169

170
        return UserDetails::fromArray($data['data']);
3✔
171
    }
172

173
    public function createUser(
174
        EndPoint $endPoint,
175
        UserCreationInformation $newUser,
176
        ?UserCredentials $userCredentials = null
177
    ): void {
178
        $this->sendPost(
151✔
179
            EndpointExtensions::rawUrlToHttpUrl(
151✔
180
                $endPoint,
151✔
181
                $this->httpSchema,
151✔
182
                '/users/'
151✔
183
            ),
151✔
184
            Json::encode($newUser),
151✔
185
            $userCredentials,
151✔
186
            HttpStatusCode::Created
151✔
187
        );
151✔
188
    }
189

190
    public function updateUser(
191
        EndPoint $endPoint,
192
        string $login,
193
        UserUpdateInformation $updatedUser,
194
        ?UserCredentials $userCredentials = null
195
    ): void {
196
        $this->sendPut(
2✔
197
            EndpointExtensions::formatStringToHttpUrl(
2✔
198
                $endPoint,
2✔
199
                $this->httpSchema,
2✔
200
                '/users/%s',
2✔
201
                \urlencode($login)
2✔
202
            ),
2✔
203
            Json::encode($updatedUser),
2✔
204
            $userCredentials,
2✔
205
            HttpStatusCode::Ok
2✔
206
        );
2✔
207
    }
208

209
    public function changePassword(
210
        EndPoint $endPoint,
211
        string $login,
212
        ChangePasswordDetails $changePasswordDetails,
213
        ?UserCredentials $userCredentials = null
214
    ): void {
215
        $this->sendPost(
2✔
216
            EndpointExtensions::formatStringToHttpUrl(
2✔
217
                $endPoint,
2✔
218
                $this->httpSchema,
2✔
219
                '/users/%s/command/change-password',
2✔
220
                \urlencode($login)
2✔
221
            ),
2✔
222
            Json::encode($changePasswordDetails),
2✔
223
            $userCredentials,
2✔
224
            HttpStatusCode::Ok
2✔
225
        );
2✔
226
    }
227

228
    public function resetPassword(
229
        EndPoint $endPoint,
230
        string $login,
231
        ResetPasswordDetails $resetPasswordDetails,
232
        ?UserCredentials $userCredentials = null
233
    ): void {
234
        $this->sendPost(
1✔
235
            EndpointExtensions::formatStringToHttpUrl(
1✔
236
                $endPoint,
1✔
237
                $this->httpSchema,
1✔
238
                '/users/%s/command/reset-password',
1✔
239
                \urlencode($login)
1✔
240
            ),
1✔
241
            Json::encode($resetPasswordDetails),
1✔
242
            $userCredentials,
1✔
243
            HttpStatusCode::Ok
1✔
244
        );
1✔
245
    }
246

247
    private function sendGet(
248
        string $url,
249
        ?UserCredentials $userCredentials,
250
        int $expectedCode
251
    ): string {
252
        $deferred = new DeferredFuture();
7✔
253

254
        $this->client->get(
7✔
255
            $url,
7✔
256
            $userCredentials,
7✔
257
            function (Response $response) use ($deferred, $expectedCode, $url): void {
7✔
258
                if ($response->getStatus() === $expectedCode) {
7✔
259
                    $deferred->complete($response->getBody()->buffer());
7✔
260
                } else {
261
                    $deferred->error(new UserCommandFailed(
1✔
262
                        $response->getStatus(),
1✔
263
                        \sprintf(
1✔
264
                            'Server returned %d (%s) for GET on %s',
1✔
265
                            $response->getStatus(),
1✔
266
                            $response->getReason(),
1✔
267
                            $url
1✔
268
                        )
1✔
269
                    ));
1✔
270
                }
271
            },
7✔
272
            function (Throwable $exception) use ($deferred): void {
7✔
273
                $deferred->error($exception);
×
274
            }
7✔
275
        );
7✔
276

277
        return $deferred->getFuture()->await();
7✔
278
    }
279

280
    private function sendDelete(
281
        string $url,
282
        ?UserCredentials $userCredentials,
283
        int $expectedCode
284
    ): void {
285
        $deferred = new DeferredFuture();
138✔
286

287
        $this->client->delete(
138✔
288
            $url,
138✔
289
            $userCredentials,
138✔
290
            function (Response $response) use ($deferred, $expectedCode, $url): void {
138✔
291
                if ($response->getStatus() === $expectedCode) {
138✔
292
                    $deferred->complete($response->getBody()->buffer());
137✔
293
                } else {
294
                    $deferred->error(new UserCommandFailed(
1✔
295
                        $response->getStatus(),
1✔
296
                        \sprintf(
1✔
297
                            'Server returned %d (%s) for DELETE on %s',
1✔
298
                            $response->getStatus(),
1✔
299
                            $response->getReason(),
1✔
300
                            $url
1✔
301
                        )
1✔
302
                    ));
1✔
303
                }
304
            },
138✔
305
            function (Throwable $exception) use ($deferred): void {
138✔
306
                $deferred->error($exception);
×
307
            }
138✔
308
        );
138✔
309

310
        $deferred->getFuture()->await();
138✔
311
    }
312

313
    private function sendPut(
314
        string $url,
315
        string $content,
316
        ?UserCredentials $userCredentials,
317
        int $expectedCode
318
    ): void {
319
        $deferred = new DeferredFuture();
2✔
320

321
        $this->client->put(
2✔
322
            $url,
2✔
323
            $content,
2✔
324
            'application/json',
2✔
325
            $userCredentials,
2✔
326
            function (Response $response) use ($deferred, $expectedCode, $url): void {
2✔
327
                if ($response->getStatus() === $expectedCode) {
2✔
328
                    $deferred->complete();
1✔
329
                } else {
330
                    $deferred->error(new UserCommandFailed(
1✔
331
                        $response->getStatus(),
1✔
332
                        \sprintf(
1✔
333
                            'Server returned %d (%s) for PUT on %s',
1✔
334
                            $response->getStatus(),
1✔
335
                            $response->getReason(),
1✔
336
                            $url
1✔
337
                        )
1✔
338
                    ));
1✔
339
                }
340
            },
2✔
341
            function (Throwable $exception) use ($deferred): void {
2✔
342
                $deferred->error($exception);
×
343
            }
2✔
344
        );
2✔
345

346
        $deferred->getFuture()->await();
2✔
347
    }
348

349
    private function sendPost(
350
        string $url,
351
        string $content,
352
        ?UserCredentials $userCredentials,
353
        int $expectedCode
354
    ): void {
355
        $deferred = new DeferredFuture();
151✔
356

357
        $this->client->post(
151✔
358
            $url,
151✔
359
            $content,
151✔
360
            'application/json',
151✔
361
            $userCredentials,
151✔
362
            function (Response $response) use ($deferred, $expectedCode, $url): void {
151✔
363
                if ($response->getStatus() === $expectedCode) {
151✔
364
                    $deferred->complete();
151✔
365
                } elseif ($response->getStatus() === HttpStatusCode::Conflict) {
3✔
366
                    $deferred->error(new UserCommandConflict($response->getStatus(), $response->getReason()));
×
367
                } else {
368
                    $deferred->error(new UserCommandFailed(
3✔
369
                        $response->getStatus(),
3✔
370
                        \sprintf(
3✔
371
                            'Server returned %d (%s) for POST on %s',
3✔
372
                            $response->getStatus(),
3✔
373
                            $response->getReason(),
3✔
374
                            $url
3✔
375
                        )
3✔
376
                    ));
3✔
377
                }
378
            },
151✔
379
            function (Throwable $exception) use ($deferred): void {
151✔
380
                $deferred->error($exception);
×
381
            }
151✔
382
        );
151✔
383

384
        $deferred->getFuture()->await();
151✔
385
    }
386
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc