Skip to content

Commit 8c4c5ea

Browse files
authored
Merge pull request #161 from ToshY/feature/160
Add BunnyValidator for more flexible validation
2 parents c23a109 + 4437373 commit 8c4c5ea

26 files changed

+1052
-478
lines changed

phpmd.baseline.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/StreamAPI.php"/>
1919
<violation rule="PHPMD\Rule\Design\LongParameterList" file="src/TokenAuthentication.php" method="sign"/>
2020
<violation rule="PHPMD\Rule\CleanCode\BooleanArgumentFlag" file="src/TokenAuthentication.php"/>
21-
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/Validator/ParameterValidator.php" method="validate"/>
22-
<violation rule="PHPMD\Rule\Design\NpathComplexity" file="src/Validator/ParameterValidator.php" method="validate"/>
23-
<violation rule="PHPMD\Rule\Design\LongMethod" file="src/Validator/ParameterValidator.php" method="validate"/>
21+
<violation rule="PHPMD\Rule\CyclomaticComplexity" file="src/Validation/ParameterValidator.php" method="validate"/>
22+
<violation rule="PHPMD\Rule\Design\NpathComplexity" file="src/Validation/ParameterValidator.php" method="validate"/>
23+
<violation rule="PHPMD\Rule\Design\LongMethod" file="src/Validation/ParameterValidator.php" method="validate"/>
2424
</phpmd-baseline>

src/BaseAPI.php

Lines changed: 133 additions & 263 deletions
Large diffs are not rendered by default.

src/EdgeScriptingAPI.php

Lines changed: 36 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@
88
use ToshY\BunnyNet\Client\BunnyClient;
99
use ToshY\BunnyNet\Enum\Host;
1010
use ToshY\BunnyNet\Exception\BunnyClientResponseException;
11-
use ToshY\BunnyNet\Exception\InvalidTypeForKeyValueException;
12-
use ToshY\BunnyNet\Exception\InvalidTypeForListValueException;
1311
use ToshY\BunnyNet\Exception\JSONException;
14-
use ToshY\BunnyNet\Exception\ParameterIsRequiredException;
1512
use ToshY\BunnyNet\Helper\BodyContentHelper;
13+
use ToshY\BunnyNet\Model\API\EdgeScripting\Code\GetCode;
14+
use ToshY\BunnyNet\Model\API\EdgeScripting\Code\SetCode;
1615
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\AddEdgeScript;
1716
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\DeleteEdgeScript;
1817
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\GetEdgeScript;
1918
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\GetEdgeScriptStatistics;
2019
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\ListEdgeScripts;
2120
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\RotateDeploymentKey;
2221
use ToshY\BunnyNet\Model\API\EdgeScripting\EdgeScript\UpdateEdgeScript;
23-
use ToshY\BunnyNet\Model\API\EdgeScripting\Code\GetCode;
24-
use ToshY\BunnyNet\Model\API\EdgeScripting\Code\SetCode;
2522
use ToshY\BunnyNet\Model\API\EdgeScripting\Release\GetActiveReleases;
2623
use ToshY\BunnyNet\Model\API\EdgeScripting\Release\GetReleases;
2724
use ToshY\BunnyNet\Model\API\EdgeScripting\Release\PublishRelease;
@@ -37,17 +34,19 @@
3734
use ToshY\BunnyNet\Model\API\EdgeScripting\Variable\UpdateVariable;
3835
use ToshY\BunnyNet\Model\API\EdgeScripting\Variable\UpsertVariable;
3936
use ToshY\BunnyNet\Model\Client\Interface\BunnyClientResponseInterface;
40-
use ToshY\BunnyNet\Validator\ParameterValidator;
37+
use ToshY\BunnyNet\Validation\BunnyValidator;
4138

4239
class EdgeScriptingAPI
4340
{
4441
/**
4542
* @param string $apiKey
4643
* @param BunnyClient $client
44+
* @param BunnyValidator $validator
4745
*/
4846
public function __construct(
4947
protected readonly string $apiKey,
5048
protected readonly BunnyClient $client,
49+
protected readonly BunnyValidator $validator = new BunnyValidator(),
5150
) {
5251
$this->client
5352
->setApiKey($this->apiKey)
@@ -75,9 +74,7 @@ public function getCode(int $id): BunnyClientResponseInterface
7574
* @throws ClientExceptionInterface
7675
* @throws Exception\BunnyClientResponseException
7776
* @throws Exception\JSONException
78-
* @throws Exception\InvalidTypeForKeyValueException
79-
* @throws Exception\InvalidTypeForListValueException
80-
* @throws Exception\ParameterIsRequiredException
77+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
8178
* @return BunnyClientResponseInterface
8279
* @param int $id
8380
* @param array<string,mixed> $body
@@ -88,7 +85,7 @@ public function setCode(
8885
): BunnyClientResponseInterface {
8986
$endpoint = new SetCode();
9087

91-
ParameterValidator::validate($body, $endpoint->getBody());
88+
$this->validator->body($body, $endpoint);
9289

9390
return $this->client->request(
9491
endpoint: $endpoint,
@@ -100,10 +97,8 @@ public function setCode(
10097
/**
10198
* @throws ClientExceptionInterface
10299
* @throws BunnyClientResponseException
103-
* @throws InvalidTypeForKeyValueException
104-
* @throws InvalidTypeForListValueException
105100
* @throws JSONException
106-
* @throws ParameterIsRequiredException
101+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
107102
* @param int $id
108103
* @param array<string,mixed> $query
109104
* @return BunnyClientResponseInterface
@@ -112,7 +107,7 @@ public function deleteEdgeScript(int $id, array $query = []): BunnyClientRespons
112107
{
113108
$endpoint = new DeleteEdgeScript();
114109

115-
ParameterValidator::validate($query, $endpoint->getQuery());
110+
$this->validator->query($query, $endpoint);
116111

117112
return $this->client->request(
118113
endpoint: $endpoint,
@@ -142,9 +137,7 @@ public function getEdgeScript(int $id): BunnyClientResponseInterface
142137
* @throws ClientExceptionInterface
143138
* @throws Exception\BunnyClientResponseException
144139
* @throws Exception\JSONException
145-
* @throws Exception\InvalidTypeForKeyValueException
146-
* @throws Exception\InvalidTypeForListValueException
147-
* @throws Exception\ParameterIsRequiredException
140+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
148141
* @return BunnyClientResponseInterface
149142
* @param int $id
150143
* @param array<string,mixed> $body
@@ -153,7 +146,7 @@ public function updateEdgeScript(int $id, array $body): BunnyClientResponseInter
153146
{
154147
$endpoint = new UpdateEdgeScript();
155148

156-
ParameterValidator::validate($body, $endpoint->getBody());
149+
$this->validator->body($body, $endpoint);
157150

158151
return $this->client->request(
159152
endpoint: $endpoint,
@@ -165,10 +158,8 @@ public function updateEdgeScript(int $id, array $body): BunnyClientResponseInter
165158
/**
166159
* @throws ClientExceptionInterface
167160
* @throws BunnyClientResponseException
168-
* @throws InvalidTypeForKeyValueException
169-
* @throws InvalidTypeForListValueException
170161
* @throws JSONException
171-
* @throws ParameterIsRequiredException
162+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
172163
* @param int $id
173164
* @param array<string,mixed> $query
174165
* @return BunnyClientResponseInterface
@@ -177,7 +168,7 @@ public function getEdgeScriptStatistics(int $id, array $query = []): BunnyClient
177168
{
178169
$endpoint = new GetEdgeScriptStatistics();
179170

180-
ParameterValidator::validate($query, $endpoint->getQuery());
171+
$this->validator->query($query, $endpoint);
181172

182173
return $this->client->request(
183174
endpoint: $endpoint,
@@ -190,17 +181,15 @@ public function getEdgeScriptStatistics(int $id, array $query = []): BunnyClient
190181
* @throws ClientExceptionInterface
191182
* @throws Exception\BunnyClientResponseException
192183
* @throws Exception\JSONException
193-
* @throws Exception\InvalidTypeForKeyValueException
194-
* @throws Exception\InvalidTypeForListValueException
195-
* @throws Exception\ParameterIsRequiredException
184+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
196185
* @param array<string,mixed> $query
197186
* @return BunnyClientResponseInterface
198187
*/
199188
public function listEdgeScripts(array $query = []): BunnyClientResponseInterface
200189
{
201190
$endpoint = new ListEdgeScripts();
202191

203-
ParameterValidator::validate($query, $endpoint->getQuery());
192+
$this->validator->query($query, $endpoint);
204193

205194
return $this->client->request(
206195
endpoint: $endpoint,
@@ -212,17 +201,15 @@ public function listEdgeScripts(array $query = []): BunnyClientResponseInterface
212201
* @throws ClientExceptionInterface
213202
* @throws Exception\BunnyClientResponseException
214203
* @throws Exception\JSONException
215-
* @throws Exception\InvalidTypeForKeyValueException
216-
* @throws Exception\InvalidTypeForListValueException
217-
* @throws Exception\ParameterIsRequiredException
204+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
218205
* @return BunnyClientResponseInterface
219206
* @param array<string,mixed> $body
220207
*/
221208
public function addEdgeScript(array $body): BunnyClientResponseInterface
222209
{
223210
$endpoint = new AddEdgeScript();
224211

225-
ParameterValidator::validate($body, $endpoint->getBody());
212+
$this->validator->body($body, $endpoint);
226213

227214
return $this->client->request(
228215
endpoint: $endpoint,
@@ -251,9 +238,7 @@ public function rotateDeploymentKey(int $id): BunnyClientResponseInterface
251238
* @throws ClientExceptionInterface
252239
* @throws Exception\BunnyClientResponseException
253240
* @throws Exception\JSONException
254-
* @throws Exception\InvalidTypeForKeyValueException
255-
* @throws Exception\InvalidTypeForListValueException
256-
* @throws Exception\ParameterIsRequiredException
241+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
257242
* @return BunnyClientResponseInterface
258243
* @param int $id
259244
* @param array<string,mixed> $body
@@ -264,7 +249,7 @@ public function addVariable(
264249
): BunnyClientResponseInterface {
265250
$endpoint = new AddVariable();
266251

267-
ParameterValidator::validate($body, $endpoint->getBody());
252+
$this->validator->body($body, $endpoint);
268253

269254
return $this->client->request(
270255
endpoint: $endpoint,
@@ -317,9 +302,7 @@ public function getVariable(
317302
* @throws ClientExceptionInterface
318303
* @throws Exception\BunnyClientResponseException
319304
* @throws Exception\JSONException
320-
* @throws Exception\InvalidTypeForKeyValueException
321-
* @throws Exception\InvalidTypeForListValueException
322-
* @throws Exception\ParameterIsRequiredException
305+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
323306
* @param int $variableId
324307
* @param array<string,mixed> $body
325308
* @return BunnyClientResponseInterface
@@ -332,7 +315,7 @@ public function updateVariable(
332315
): BunnyClientResponseInterface {
333316
$endpoint = new UpdateVariable();
334317

335-
ParameterValidator::validate($body, $endpoint->getBody());
318+
$this->validator->body($body, $endpoint);
336319

337320
return $this->client->request(
338321
endpoint: $endpoint,
@@ -345,9 +328,7 @@ public function updateVariable(
345328
* @throws ClientExceptionInterface
346329
* @throws Exception\BunnyClientResponseException
347330
* @throws Exception\JSONException
348-
* @throws Exception\InvalidTypeForKeyValueException
349-
* @throws Exception\InvalidTypeForListValueException
350-
* @throws Exception\ParameterIsRequiredException
331+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
351332
* @param array<string,mixed> $body
352333
* @return BunnyClientResponseInterface
353334
* @param int $id
@@ -358,7 +339,7 @@ public function upsertVariable(
358339
): BunnyClientResponseInterface {
359340
$endpoint = new UpsertVariable();
360341

361-
ParameterValidator::validate($body, $endpoint->getBody());
342+
$this->validator->body($body, $endpoint);
362343

363344
return $this->client->request(
364345
endpoint: $endpoint,
@@ -371,9 +352,7 @@ public function upsertVariable(
371352
* @throws ClientExceptionInterface
372353
* @throws Exception\BunnyClientResponseException
373354
* @throws Exception\JSONException
374-
* @throws Exception\InvalidTypeForKeyValueException
375-
* @throws Exception\InvalidTypeForListValueException
376-
* @throws Exception\ParameterIsRequiredException
355+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
377356
* @return BunnyClientResponseInterface
378357
* @param int $id
379358
* @param array<string,mixed> $body
@@ -384,7 +363,7 @@ public function addSecret(
384363
): BunnyClientResponseInterface {
385364
$endpoint = new AddSecret();
386365

387-
ParameterValidator::validate($body, $endpoint->getBody());
366+
$this->validator->body($body, $endpoint);
388367

389368
return $this->client->request(
390369
endpoint: $endpoint,
@@ -397,9 +376,7 @@ public function addSecret(
397376
* @throws ClientExceptionInterface
398377
* @throws Exception\BunnyClientResponseException
399378
* @throws Exception\JSONException
400-
* @throws Exception\InvalidTypeForKeyValueException
401-
* @throws Exception\InvalidTypeForListValueException
402-
* @throws Exception\ParameterIsRequiredException
379+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
403380
* @param int $id
404381
* @return BunnyClientResponseInterface
405382
*/
@@ -417,9 +394,7 @@ public function listSecrets(int $id): BunnyClientResponseInterface
417394
* @throws ClientExceptionInterface
418395
* @throws Exception\BunnyClientResponseException
419396
* @throws Exception\JSONException
420-
* @throws Exception\InvalidTypeForKeyValueException
421-
* @throws Exception\InvalidTypeForListValueException
422-
* @throws Exception\ParameterIsRequiredException
397+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
423398
* @param array<string,mixed> $body
424399
* @return BunnyClientResponseInterface
425400
* @param int $id
@@ -430,7 +405,7 @@ public function upsertSecret(
430405
): BunnyClientResponseInterface {
431406
$endpoint = new UpsertSecret();
432407

433-
ParameterValidator::validate($body, $endpoint->getBody());
408+
$this->validator->body($body, $endpoint);
434409

435410
return $this->client->request(
436411
endpoint: $endpoint,
@@ -463,9 +438,7 @@ public function deleteSecret(
463438
* @throws ClientExceptionInterface
464439
* @throws Exception\BunnyClientResponseException
465440
* @throws Exception\JSONException
466-
* @throws Exception\InvalidTypeForKeyValueException
467-
* @throws Exception\InvalidTypeForListValueException
468-
* @throws Exception\ParameterIsRequiredException
441+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
469442
* @param int $secretId
470443
* @param array<string,mixed> $body
471444
* @return BunnyClientResponseInterface
@@ -478,7 +451,7 @@ public function updateSecret(
478451
): BunnyClientResponseInterface {
479452
$endpoint = new UpdateSecret();
480453

481-
ParameterValidator::validate($body, $endpoint->getBody());
454+
$this->validator->body($body, $endpoint);
482455

483456
return $this->client->request(
484457
endpoint: $endpoint,
@@ -508,9 +481,7 @@ public function getActiveRelease(int $id): BunnyClientResponseInterface
508481
* @throws ClientExceptionInterface
509482
* @throws Exception\BunnyClientResponseException
510483
* @throws Exception\JSONException
511-
* @throws Exception\InvalidTypeForKeyValueException
512-
* @throws Exception\InvalidTypeForListValueException
513-
* @throws Exception\ParameterIsRequiredException
484+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
514485
* @param array<string,mixed> $query
515486
* @return BunnyClientResponseInterface
516487
* @param int $id
@@ -521,7 +492,7 @@ public function getReleases(
521492
): BunnyClientResponseInterface {
522493
$endpoint = new GetReleases();
523494

524-
ParameterValidator::validate($query, $endpoint->getQuery());
495+
$this->validator->query($query, $endpoint);
525496

526497
return $this->client->request(
527498
endpoint: $endpoint,
@@ -534,9 +505,7 @@ public function getReleases(
534505
* @throws ClientExceptionInterface
535506
* @throws Exception\BunnyClientResponseException
536507
* @throws Exception\JSONException
537-
* @throws Exception\InvalidTypeForKeyValueException
538-
* @throws Exception\InvalidTypeForListValueException
539-
* @throws Exception\ParameterIsRequiredException
508+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
540509
* @param array<string,mixed> $body
541510
* @return BunnyClientResponseInterface
542511
* @param int $id
@@ -547,7 +516,7 @@ public function publishRelease(
547516
): BunnyClientResponseInterface {
548517
$endpoint = new PublishRelease();
549518

550-
ParameterValidator::validate($body, $endpoint->getBody());
519+
$this->validator->body($body, $endpoint);
551520

552521
return $this->client->request(
553522
endpoint: $endpoint,
@@ -560,9 +529,7 @@ public function publishRelease(
560529
* @throws ClientExceptionInterface
561530
* @throws Exception\BunnyClientResponseException
562531
* @throws Exception\JSONException
563-
* @throws Exception\InvalidTypeForKeyValueException
564-
* @throws Exception\InvalidTypeForListValueException
565-
* @throws Exception\ParameterIsRequiredException
532+
* @throws Exception\Validation\BunnyValidatorExceptionInterface
566533
* @param string $uuid
567534
* @param array<string,mixed> $body
568535
* @return BunnyClientResponseInterface
@@ -575,7 +542,7 @@ public function publishReleaseByUuid(
575542
): BunnyClientResponseInterface {
576543
$endpoint = new PublishReleaseByPathParameter();
577544

578-
ParameterValidator::validate($body, $endpoint->getBody());
545+
$this->validator->body($body, $endpoint);
579546

580547
return $this->client->request(
581548
endpoint: $endpoint,

0 commit comments

Comments
 (0)