Skip to content

Commit f5e41a8

Browse files
Refactor API paths and request handling for consistency
Replaced hardcoded API paths with `ApiPathEnum` to improve maintainability and added string replacement for dynamic paths. Standardized HTTP method handling by removing unnecessary `->value` usage and fixed minor parameter naming inconsistencies. Enhanced error handling by wrapping `GuzzleException` with `UnitException` for better debugging.
1 parent 073b546 commit f5e41a8

File tree

11 files changed

+109
-75
lines changed

11 files changed

+109
-75
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
],
2020
"scripts": {
2121
"test": [
22-
"@test:unit",
23-
"@test:phpstan"
22+
"@test:phpstan",
23+
"@test:unit"
2424
],
2525
"test:unit": "./vendor/bin/pest",
26-
"test:coverage": "./vendor/bin/pest --coverage --coverage-clover coverage.xml",
27-
"test:phpstan": "./vendor/bin/phpstan analyse -c phpstan.neon",
26+
"test:coverage": "XDEBUG_MODE=cover ./vendor/bin/pest --coverage --coverage-clover coverage.xml",
27+
"test:phpstan": "./vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=256M",
2828
"lint": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --rules=@PSR12 ."
2929
},
3030
"require": {

src/Abstract/AbstractApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function __construct(string $name, ?array $data = [])
100100
$this->parseFromArray($data);
101101
}
102102

103-
$this->setApiEndpoint(EndpointBuilder::create(ApiPathEnum::APPLICATIONS->value)->get() . $this->getName());
103+
$this->setApiEndpoint(EndpointBuilder::create(ApiPathEnum::APPLICATIONS->value)->get() . '/' . $this->getName());
104104
}
105105

106106
/**

src/Config.php

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,12 @@ public function uploadListener(Listener $listener): bool
256256
{
257257
try {
258258
$this->unitRequest->setMethod(HttpMethodsEnum::PUT)
259-
->send("/config/listeners/{$listener->getListener()}", requestOptions: [
260-
'json' => $listener->toArray()
261-
]);
259+
->send(
260+
uri: "/config/listeners/{$listener->getListener()}",
261+
requestOptions: [
262+
'json' => $listener->toArray()
263+
]
264+
);
262265
} catch (UnitException $e) {
263266
print_r($e->getMessage());
264267
return false;
@@ -270,7 +273,7 @@ public function uploadListener(Listener $listener): bool
270273
/**
271274
* @throws UnitException
272275
*/
273-
public function uploadListenerFromFile(string $path, string $listener): bool
276+
public function uploadListenerFromFile(string $path, string $listenerName): bool
274277
{
275278
$fileContent = file_get_contents($path);
276279

@@ -284,7 +287,7 @@ public function uploadListenerFromFile(string $path, string $listener): bool
284287

285288
try {
286289
$this->unitRequest->setMethod(HttpMethodsEnum::PUT)
287-
->send("/config/listeners/$listener", requestOptions: [
290+
->send(ApiPathEnum::LISTENER->getPath($listenerName), requestOptions: [
288291
'body' => $fileContent
289292
]);
290293
} catch (UnitException) {
@@ -301,10 +304,10 @@ public function uploadListenerFromFile(string $path, string $listener): bool
301304
*/
302305
public function removeListener(Listener $listener): bool
303306
{
304-
$listenerId = $listener->getListener();
307+
$listenerName = $listener->getListener();
305308
$this->unitRequest
306-
->setMethod(HttpMethodsEnum::DELETE->value)
307-
->send("/config/listeners/$listenerId");
309+
->setMethod(HttpMethodsEnum::DELETE)
310+
->send(ApiPathEnum::LISTENER->getPath($listenerName));
308311

309312
return true;
310313
}
@@ -347,12 +350,12 @@ public function uploadApplication(AbstractApplication $application, string $name
347350
throw new UnitException('Application name not specified');
348351
}
349352

350-
$appName = empty($application->getName()) ? $name : $application->getName();
353+
$applicationName = empty($application->getName()) ? $name : $application->getName();
351354

352355
try {
353356
$this->unitRequest
354-
->setMethod(HttpMethodsEnum::PUT->value)
355-
->send("/config/applications/$appName", requestOptions: [
357+
->setMethod(HttpMethodsEnum::PUT)
358+
->send(ApiPathEnum::APPLICATION->getPath($applicationName), requestOptions: [
356359
'json' => $application->toJson()
357360
]);
358361
} catch (UnitException) {
@@ -366,7 +369,7 @@ public function uploadApplication(AbstractApplication $application, string $name
366369
* @inheritdoc
367370
* @throws UnitException
368371
*/
369-
public function uploadApplicationFromFile(string $path, string $name): bool
372+
public function uploadApplicationFromFile(string $path, string $applicationName): bool
370373
{
371374
// TODO: add validation if json contains application name
372375
$fileContent = file_get_contents($path);
@@ -381,8 +384,8 @@ public function uploadApplicationFromFile(string $path, string $name): bool
381384

382385
try {
383386
$this->unitRequest
384-
->setMethod(HttpMethodsEnum::PUT->value)
385-
->send("/config/applications/$name", requestOptions: [
387+
->setMethod(HttpMethodsEnum::PUT)
388+
->send(ApiPathEnum::APPLICATION->getPath($applicationName), requestOptions: [
386389
'body' => $fileContent
387390
]);
388391
} catch (UnitException) {
@@ -402,8 +405,8 @@ public function removeApplication(AbstractApplication|string $application): bool
402405
{
403406
$applicationName = is_string($application) ? $application : $application->getName();
404407
$this->unitRequest
405-
->setMethod(HttpMethodsEnum::DELETE->value)
406-
->send("/config/applications/$applicationName");
408+
->setMethod(HttpMethodsEnum::DELETE)
409+
->send(ApiPathEnum::APPLICATION->getPath($applicationName));
407410

408411
return true;
409412
}
@@ -492,8 +495,8 @@ public function uploadUpstream(Upstream $upstream, string $name = ''): bool
492495

493496
try {
494497
$this->unitRequest
495-
->setMethod(HttpMethodsEnum::PUT->value)
496-
->send("/config/upstreams/$upstreamName", requestOptions: [
498+
->setMethod(HttpMethodsEnum::PUT)
499+
->send(ApiPathEnum::UPSTREAM->getPath($upstreamName), requestOptions: [
497500
'json' => $upstream->toArray()
498501
]);
499502
} catch (UnitException) {
@@ -521,8 +524,8 @@ public function uploadUpstreamsFromFile(string $path): bool
521524

522525
try {
523526
$this->unitRequest
524-
->setMethod(HttpMethodsEnum::PUT->value)
525-
->send('/config/upstreams', requestOptions: [
527+
->setMethod(HttpMethodsEnum::PUT)
528+
->send(ApiPathEnum::UPSTREAMS, requestOptions: [
526529
'body' => $fileContent
527530
]);
528531
} catch (UnitException $exception) {
@@ -546,7 +549,7 @@ public function setAccessLog($path, $format = null): bool
546549

547550
try {
548551
$this->unitRequest->setMethod(HttpMethodsEnum::PUT)
549-
->send('/config/access_log', requestOptions: [
552+
->send(ApiPathEnum::ACCESS_LOG, requestOptions: [
550553
'json' => json_encode($data)
551554
]);
552555
} catch (UnitException) {
@@ -576,8 +579,8 @@ public function removeAccessLog(): bool
576579
{
577580
try {
578581
$this->unitRequest
579-
->setMethod(HttpMethodsEnum::DELETE->value)
580-
->send('/config/access_log');
582+
->setMethod(HttpMethodsEnum::DELETE)
583+
->send(ApiPathEnum::ACCESS_LOG->value);
581584
} catch (UnitException) {
582585
return false;
583586
}
@@ -623,20 +626,20 @@ public function reset(): bool
623626
{
624627
try {
625628
$this->unitRequest->setMethod(HttpMethodsEnum::DELETE)
626-
->send('/config');
629+
->send(ApiPathEnum::CONFIG->value);
627630

628631
$this->unitRequest->setMethod(HttpMethodsEnum::POST)
629632
->send(
630-
uri: '/config',
633+
uri: ApiPathEnum::CONFIG->value,
631634
requestOptions: [
632-
'json' => json_encode([
633-
'listeners' => (object)[],
634-
'routes' => (object)[],
635-
'applications' => (object)[],
636-
'upstreams' => (object)[],
637-
'settings' => (object)[]
638-
])
639-
]
635+
'json' => json_encode([
636+
'listeners' => (object)[],
637+
'routes' => (object)[],
638+
'applications' => (object)[],
639+
'upstreams' => (object)[],
640+
'settings' => (object)[]
641+
])
642+
]
640643
);
641644
} catch (UnitException) {
642645
return false;

src/Contracts/ConfigInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public function uploadListener(Listener $listener): bool;
3434
* Upload configuration from file
3535
*
3636
* @param string $path
37-
* @param string $listener
37+
* @param string $listenerName
3838
* @return bool
3939
*/
40-
public function uploadListenerFromFile(string $path, string $listener): bool;
40+
public function uploadListenerFromFile(string $path, string $listenerName): bool;
4141

4242
/**
4343
* @param Listener $listener
@@ -93,10 +93,10 @@ public function uploadApplication(AbstractApplication $application, string $name
9393
* Upload application to Nginx Unit from file
9494
*
9595
* @param string $path
96-
* @param string $name
96+
* @param string $applicationName
9797
* @return mixed
9898
*/
99-
public function uploadApplicationFromFile(string $path, string $name): bool;
99+
public function uploadApplicationFromFile(string $path, string $applicationName): bool;
100100

101101
/**
102102
* @param AbstractApplication|string $application

src/Contracts/UnitInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function getCertificate(string $certificateName): ?CertificateInterface;
4949
*
5050
* @param string $path
5151
* @param string $certificateName
52-
* @return bool
5352
* @throws UnitException
5453
*/
5554
public function uploadCertificate(string $path, string $certificateName);

src/Enums/ApiPathEnum.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
enum ApiPathEnum: string
66
{
7-
case UNIT = '/unit';
7+
case UNIT = '/';
88
case CONFIG = '/config';
99
case ROUTES = '/config/routes';
1010
case APPLICATIONS = '/config/applications';
@@ -17,6 +17,19 @@ enum ApiPathEnum: string
1717
case CERTIFICATES = '/certificates';
1818
case CERTIFICATE = '/certificates/{certificate}';
1919
case STATUS = '/status';
20-
2120
case ACCESS_LOG = '/config/access_log';
21+
case JS_MODULES = '/js_modules';
22+
case APPLICATION_RESET = '/config/applications/{application}/reset';
23+
24+
public function getPath(string $name): string
25+
{
26+
return match ($this) {
27+
self::LISTENER => str_replace('{listener}', $name, $this->value),
28+
self::ROUTE => str_replace('{route}', $name, $this->value),
29+
self::APPLICATION, self::APPLICATION_RESET => str_replace('{application}', $name, $this->value),
30+
self::UPSTREAM => str_replace('{upstream}', $name, $this->value),
31+
self::CERTIFICATE => str_replace('{certificate}', $name, $this->value),
32+
default => $this->value
33+
};
34+
}
2235
}

src/Http/UnitHttpClient.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace UnitPhpSdk\Http;
44

55
use GuzzleHttp\Client;
6-
use GuzzleHttp\ClientInterface;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\ResponseInterface;
79

810
class UnitHttpClient
911
{
@@ -27,9 +29,12 @@ public function __construct(
2729
]);
2830
}
2931

30-
public function request(string $method, string $uri, array $options = [])
32+
/**
33+
* @throws GuzzleException
34+
*/
35+
public function request(string $method, string $uri, array $options = []): ResponseInterface
3136
{
32-
37+
return $this->client->request($method, $uri, $options);
3338
}
3439

3540
// /**

src/Http/UnitRequest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class UnitRequest
2525
*/
2626
private readonly string $address;
2727

28-
private ClientInterface $client;
29-
3028
/**
3129
* Constructor
3230
*
@@ -35,7 +33,8 @@ class UnitRequest
3533
*/
3634
public function __construct(
3735
string $address,
38-
private readonly ?string $socket = null
36+
private readonly ?string $socket = null,
37+
private ?ClientInterface $client = null
3938
) {
4039
$this->client = $client ?? new Client([
4140
'base_uri' => $address,
@@ -83,12 +82,15 @@ public function setMethod(string|HttpMethodsEnum $method): self
8382
* @param bool $associative
8483
* @param array $requestOptions
8584
* @return mixed
86-
* @throws GuzzleException
8785
* @throws UnitException
8886
*/
89-
public function send($uri, bool $associative = true, array $requestOptions = [])
87+
public function send($uri, bool $associative = true, array $requestOptions = []): mixed
9088
{
91-
$response = $this->client->request($this->method, $this->address . $uri, $requestOptions);
89+
try {
90+
$response = $this->client->request($this->method, $this->address . $uri, $requestOptions);
91+
} catch (GuzzleException $e) {
92+
throw new UnitException($e->getMessage());
93+
}
9294

9395
$rawData = json_decode($response->getBody()->getContents(), $associative);
9496

src/Traits/CanUpload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function upload(UnitRequest $request)
5050
$data = $this->removeEmptyArrays($this->toArray());
5151

5252
try {
53-
$request->setMethod(HttpMethodsEnum::PUT->value)->send(
53+
$request->setMethod(HttpMethodsEnum::PUT)->send(
5454
$this->getApiEndpoint(),
5555
true,
5656
['json' => $data]
@@ -68,7 +68,7 @@ public function upload(UnitRequest $request)
6868
public function remove(UnitRequest $request)
6969
{
7070
try {
71-
$request->setMethod(HttpMethodsEnum::DELETE->value)->send($this->getApiEndpoint());
71+
$request->setMethod(HttpMethodsEnum::DELETE)->send($this->getApiEndpoint());
7272
} catch (UnitException $e) {
7373
throw new UnitException($e->getMessage());
7474
}

0 commit comments

Comments
 (0)