Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 2a032d5

Browse files
Implement HTTP upload and remove methods
This commit adds `upload` and `remove` methods to the HTTP class and implements the Uploadable interface. Additionally, minor changes are made for better typing and syntax consistency across different classes. A badge for deploy workflow is added in the README.
1 parent 4c34510 commit 2a032d5

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<p align="center">
1010
<a href="https://packagist.org/packages/pavlusha311245/unit-php-sdk"><img src="https://img.shields.io/packagist/v/Pavlusha311245/unit-php-sdk?labelColor=%231e293b&color=%23702963&link=https%3A%2F%2Fpackagist.org%2Fpackages%2Fpavlusha311245%2Funit-php-sdk" alt="packagist link"></a>
1111
<a href="https://unit-sdk.pavlusha.me/"><img src="https://img.shields.io/website?url=https%3A%2F%2Funit-sdk.pavlusha.me%2F&label=documentation&link=https%3A%2F%2Funit-sdk.pavlusha.me%2F" alt="documentation link"></a>
12+
<img src="https://github.com/Pavlusha311245/nginx-unit-php-sdk/actions/workflows/deploy_codecov.yaml/badge.svg" alt="documentation link">
1213
<a href="https://codecov.io/gh/Pavlusha311245/nginx-unit-php-sdk" >
1314
<img src="https://codecov.io/gh/Pavlusha311245/nginx-unit-php-sdk/graph/badge.svg?token=FGTTDSJ7BX" alt="Codecov dabge"/>
1415
</a>

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function loadUpstreams(array $data): void
203203
}
204204

205205
$servers = array_map(
206-
fn($v, $k) => new Server($v, $k['weight'] ?? 1),
206+
fn ($v, $k) => new Server($v, $k['weight'] ?? 1),
207207
array_keys($upstreamData['servers']),
208208
array_values($upstreamData['servers'])
209209
);

src/Config/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function setJsModule(array|string $js_module): void
9191
$request->setMethod(HttpMethodsEnum::DELETE)->send($this->getEndpoint());
9292
}
9393

94-
private function getEndpoint()
94+
private function getEndpoint(): string
9595
{
9696
return '/config/settings';
9797
}

src/Config/Settings/Http.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use UnitPhpSdk\Contracts\Arrayable;
66
use UnitPhpSdk\Contracts\Jsonable;
7+
use UnitPhpSdk\Contracts\Uploadable;
8+
use UnitPhpSdk\Enums\HttpMethodsEnum;
9+
use UnitPhpSdk\Http\UnitRequest;
710

8-
class Http implements Arrayable, Jsonable
11+
class Http implements Arrayable, Jsonable, Uploadable
912
{
1013
/**
1114
*
@@ -199,4 +202,21 @@ public function toJson(int $options = 0): string
199202
{
200203
return json_encode($this->toArray(), $options);
201204
}
205+
206+
#[\Override] public function upload(UnitRequest $request)
207+
{
208+
$request->setMethod(HttpMethodsEnum::PUT)->send($this->getEndpoint(), true, [
209+
'json' => array_filter($this->toArray(), fn ($value) => $value !== null)
210+
]);
211+
}
212+
213+
#[\Override] public function remove(UnitRequest $request)
214+
{
215+
$request->setMethod(HttpMethodsEnum::DELETE)->send($this->getEndpoint());
216+
}
217+
218+
private function getEndpoint(): string
219+
{
220+
return '/config/settings/http';
221+
}
202222
}

src/Http/UnitRequest.php

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

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Exception\GuzzleException;
7+
use UnitPhpSdk\Enums\HttpMethodsEnum;
78
use UnitPhpSdk\Exceptions\UnitException;
89

910
/**
@@ -60,9 +61,9 @@ private function parseAddress(string $address): string
6061
*
6162
* @param mixed $method
6263
*/
63-
public function setMethod(string $method): self
64+
public function setMethod(string|HttpMethodsEnum $method): self
6465
{
65-
$this->method = mb_strtoupper($method);
66+
$this->method = is_string($method) ? mb_strtoupper($method) : $method->value;
6667

6768
return $this;
6869
}

src/Unit.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class Unit implements UnitInterface
5050
public function __construct(
5151
private readonly string $address,
5252
private readonly ?string $socket = null
53-
)
54-
{
53+
) {
5554
$this->request = new UnitRequest(
5655
address: $this->address,
5756
socket: $this->socket

0 commit comments

Comments
 (0)