Skip to content

Commit 51ac72a

Browse files
Implement Uploadable interface in Config class
The Config class now implements the Uploadable interface, adding `upload` and `remove` methods associated with it. The `uploadConfig` and `removeConfig` methods from UnitInterface have been removed, taking away the responsibility of handling uploads from the Unit class. Additionally, the type hint for `getSettings` has been relaxed to optionally return null to increase flexibility.
1 parent fad4cf9 commit 51ac72a

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

src/Config.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use UnitPhpSdk\Config\{AccessLog, Application, Listener, Route, Settings, Upstream, Upstream\Server};
88
use UnitPhpSdk\Exceptions\{FileNotFoundException, UnitException};
99
use UnitPhpSdk\Contracts\ConfigInterface;
10+
use UnitPhpSdk\Contracts\Uploadable;
1011
use UnitPhpSdk\Http\UnitRequest;
1112
use UnitPhpSdk\Enums\HttpMethodsEnum;
1213

@@ -15,7 +16,7 @@
1516
*
1617
* @implements ConfigInterface
1718
*/
18-
class Config implements ConfigInterface
19+
class Config implements ConfigInterface, Uploadable
1920
{
2021
/**
2122
* Listeners that accept requests
@@ -47,9 +48,9 @@ class Config implements ConfigInterface
4748
private array $upstreams = [];
4849

4950
/**
50-
* @var Settings
51+
* @var Settings|null
5152
*/
52-
private Settings $settings;
53+
private ?Settings $settings = null;
5354

5455
private ?UnitRequest $unitRequest;
5556

@@ -614,9 +615,9 @@ private function mapConfigObjectToArray(array $data): array
614615
}
615616

616617
/**
617-
* @return Settings
618+
* @return Settings|null
618619
*/
619-
public function getSettings(): Settings
620+
public function getSettings(): ?Settings
620621
{
621622
return $this->settings;
622623
}
@@ -637,4 +638,38 @@ public function setSettings(Settings $settings): void
637638
{
638639
return json_encode($this->toArray());
639640
}
641+
642+
/**
643+
* @inheritDoc
644+
*/
645+
#[Override] public function upload(UnitRequest $request)
646+
{
647+
try {
648+
$request
649+
->setMethod(HttpMethodsEnum::PUT)
650+
->send('/config', requestOptions: [
651+
'json' => $this->toArray()
652+
]);
653+
654+
return true;
655+
} catch (UnitException) {
656+
return false;
657+
}
658+
}
659+
660+
/**
661+
* @inheritDoc
662+
*/
663+
#[Override] public function remove(UnitRequest $request)
664+
{
665+
try {
666+
$request
667+
->setMethod(HttpMethodsEnum::DELETE)
668+
->send('/config');
669+
670+
return true;
671+
} catch (UnitException) {
672+
return false;
673+
}
674+
}
640675
}

src/Contracts/ConfigInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ public function getAccessLog(): ?AccessLog;
151151
*/
152152
public function removeAccessLog(): bool;
153153

154+
154155
/**
155-
* @return Settings
156+
* @return Settings|null
156157
*/
157-
public function getSettings(): Settings;
158+
public function getSettings(): ?Settings;
158159

159160
/**
160161
* @param Settings $settings

src/Contracts/UnitInterface.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ public function uploadCertificate(string $path, string $certificateName): bool;
6161
*/
6262
public function getConfig(): ConfigInterface;
6363

64-
/**
65-
* @param Config $config
66-
* @return bool
67-
*/
68-
public function uploadConfig(Config $config): bool;
69-
7064
/**
7165
* Upload full file config
7266
*
@@ -84,13 +78,6 @@ public function uploadConfigFromFile(string $path): bool;
8478
*/
8579
public function removeCertificate(string $certificateName): bool;
8680

87-
/**
88-
* Remove all data from config
89-
*
90-
* @return bool
91-
*/
92-
public function removeConfig(): bool;
93-
9481
/**
9582
* Retrieve the list of JavaScript modules.
9683
*

src/Unit.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,6 @@ public function getCertificate(string $certificateName): ?CertificateInterface
225225
return $this->certificates[$certificateName] ?? null;
226226
}
227227

228-
public function uploadConfig(Config $config): bool
229-
{
230-
return true;
231-
}
232-
233228
/**
234229
* @inheritDoc
235230
* @throws UnitException
@@ -262,22 +257,6 @@ public function uploadConfigFromFile(string $path): bool
262257
return true;
263258
}
264259

265-
/**
266-
* @inheritDoc
267-
*/
268-
public function removeConfig(): bool
269-
{
270-
try {
271-
$this->request
272-
->setMethod(HttpMethodsEnum::DELETE->value)
273-
->send('/config');
274-
} catch (UnitException) {
275-
return false;
276-
}
277-
278-
return true;
279-
}
280-
281260
/**
282261
* Uploads the specified Uploadable object.
283262
*

0 commit comments

Comments
 (0)