Skip to content

Commit dc36c7a

Browse files
Refactor API endpoint building to use ApiPathEnum.
Replaced the EndpointBuilder utility with ApiPathEnum for constructing API endpoints. This change simplifies and standardizes endpoint generation, improving maintainability and reducing code complexity. Removed the unused EndpointBuilder class.
1 parent f5e41a8 commit dc36c7a

File tree

11 files changed

+22
-82
lines changed

11 files changed

+22
-82
lines changed

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(ApiPathEnum::APPLICATION->getPath($this->getName()));
104104
}
105105

106106
/**

src/Builders/EndpointBuilder.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __construct(object $data = null, UnitRequest $unitRequest = null
7575
$this->parseUnitObject($data);
7676
}
7777

78-
$this->setApiEndpoint(EndpointBuilder::create(ApiPathEnum::CONFIG->value)->get());
78+
$this->setApiEndpoint(ApiPathEnum::CONFIG->value);
7979
}
8080

8181
/**

src/Config/AccessLog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(
3232
$this->path = $data['path'] ?? null;
3333
$this->format = $data['format'] ?? null;
3434

35-
$this->setApiEndpoint(EndpointBuilder::create(ApiPathEnum::ACCESS_LOG->value)->get());
35+
$this->setApiEndpoint(ApiPathEnum::ACCESS_LOG->value);
3636
}
3737

3838
/**

src/Config/Listener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use UnitPhpSdk\Contracts\Arrayable;
1313
use UnitPhpSdk\Contracts\Jsonable;
1414
use UnitPhpSdk\Contracts\Uploadable;
15+
use UnitPhpSdk\Enums\ApiPathEnum;
1516
use UnitPhpSdk\Exceptions\UnitException;
1617
use UnitPhpSdk\Traits\CanUpload;
1718

@@ -43,7 +44,7 @@ public function __construct(
4344
$this->parsePort();
4445
$this->parsePass($pass);
4546

46-
$this->setApiEndpoint(EndpointBuilder::create($this)->get() . '/' . $this->getListener());
47+
$this->setApiEndpoint(ApiPathEnum::LISTENER->getPath($this->getListener()));
4748
}
4849

4950
/**

src/Config/Route.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use UnitPhpSdk\Config\Routes\RouteBlock;
99
use UnitPhpSdk\Contracts\RouteInterface;
1010
use UnitPhpSdk\Contracts\Uploadable;
11+
use UnitPhpSdk\Enums\ApiPathEnum;
1112
use UnitPhpSdk\Exceptions\UnitException;
1213
use UnitPhpSdk\Traits\CanUpload;
1314
use UnitPhpSdk\Traits\HasListeners;
@@ -37,9 +38,10 @@ class Route implements RouteInterface, Uploadable
3738
*/
3839
public function __construct(
3940
private readonly string $name,
40-
$data = [],
41+
$data = [],
4142
bool $single = false
42-
) {
43+
)
44+
{
4345
$this->routeBlocks = new SplObjectStorage();
4446
if (!empty($data)) {
4547
if ($single) {
@@ -51,7 +53,7 @@ public function __construct(
5153
}
5254
}
5355

54-
$this->setApiEndpoint(EndpointBuilder::create($this)->get() . '/' . $this->getName());
56+
$this->setApiEndpoint(ApiPathEnum::ROUTE->getPath($this->getName()));
5557
}
5658

5759
/**
@@ -103,7 +105,7 @@ public function getRouteBlocks(): array
103105
*/
104106
#[Override] public function toArray(): array
105107
{
106-
return array_map(fn (RouteBlock $routeBlock) => $routeBlock->toArray(), $this->getRouteBlocks());
108+
return array_map(fn(RouteBlock $routeBlock) => $routeBlock->toArray(), $this->getRouteBlocks());
107109
}
108110

109111
/**
@@ -112,6 +114,6 @@ public function getRouteBlocks(): array
112114
*/
113115
#[Override] public function toJson(int $options = 0): string
114116
{
115-
return json_encode(array_filter($this->toArray(), fn ($item) => !empty($item)));
117+
return json_encode(array_filter($this->toArray(), fn($item) => !empty($item)));
116118
}
117119
}

src/Config/Settings.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use UnitPhpSdk\Contracts\Arrayable;
1010
use UnitPhpSdk\Contracts\Jsonable;
1111
use UnitPhpSdk\Contracts\Uploadable;
12+
use UnitPhpSdk\Enums\ApiPathEnum;
1213
use UnitPhpSdk\Traits\CanUpload;
1314

1415
class Settings implements Uploadable, Arrayable, Jsonable
@@ -48,7 +49,7 @@ public function __construct(array $data = [])
4849
$this->parseTelemetry($data['telemetry']);
4950
}
5051

51-
$this->setApiEndpoint(EndpointBuilder::create($this)->get());
52+
$this->setApiEndpoint(ApiPathEnum::SETTINGS->value);
5253
}
5354

5455
/**
@@ -125,6 +126,6 @@ public function setJsModule(array|string $js_module): void
125126
*/
126127
#[\Override] public function toJson(int $options = 0): string
127128
{
128-
return json_encode(array_filter($this->toArray(), fn ($item) => !empty($item)), $options);
129+
return json_encode(array_filter($this->toArray(), fn($item) => !empty($item)), $options);
129130
}
130131
}

src/Config/Settings/Http.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use UnitPhpSdk\Builders\EndpointBuilder;
66
use UnitPhpSdk\Contracts\{Arrayable, Jsonable, Uploadable};
7+
use UnitPhpSdk\Enums\ApiPathEnum;
78
use UnitPhpSdk\Traits\CanUpload;
89

910
class Http implements Arrayable, Jsonable, Uploadable
@@ -97,7 +98,7 @@ private function parseData(array $data): void
9798
}
9899
}
99100

100-
$this->setApiEndpoint(EndpointBuilder::create($this)->get());
101+
$this->setApiEndpoint(ApiPathEnum::HTTP->value);
101102
}
102103

103104
/**

src/Config/Upstream.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use UnitPhpSdk\Contracts\Arrayable;
99
use UnitPhpSdk\Contracts\Uploadable;
1010
use UnitPhpSdk\Contracts\UpstreamInterface;
11+
use UnitPhpSdk\Enums\ApiPathEnum;
1112
use UnitPhpSdk\Exceptions\UnitException;
1213
use UnitPhpSdk\Traits\CanUpload;
1314
use UnitPhpSdk\Traits\HasListeners;
@@ -37,7 +38,7 @@ public function __construct(
3738
}
3839
}
3940

40-
$this->setApiEndpoint(EndpointBuilder::create($this)->get() . '/' . $this->getName());
41+
$this->setApiEndpoint(ApiPathEnum::UPSTREAM->getPath($this->getName()));
4142
}
4243

4344
/**

src/Enums/ApiPathEnum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ enum ApiPathEnum: string
2020
case ACCESS_LOG = '/config/access_log';
2121
case JS_MODULES = '/js_modules';
2222
case APPLICATION_RESET = '/config/applications/{application}/reset';
23+
case SETTINGS = '/config/settings';
24+
case HTTP = '/config/settings/http';
2325

2426
public function getPath(string $name): string
2527
{

src/Unit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function getConfig(): Contracts\ConfigInterface
9494
private function loadConfig(): void
9595
{
9696
$result = $this->request->send(ApiPathEnum::CONFIG->value, false);
97+
9798
$this->config = new Config($result, $this->request);
9899
}
99100

0 commit comments

Comments
 (0)