Skip to content

Commit

Permalink
Implement 'remove' method and refactor 'upload' for 'Uploadable' objects
Browse files Browse the repository at this point in the history
This commit implements a 'remove' method for 'Uploadable' objects and refactors the existing 'upload' method in 'AbstractApplication' and 'Listener' classes. It also creates a new 'getApiEndpoint' method to efficiently retrieve API endpoint paths. 'root
  • Loading branch information
Pavlusha311245 committed Mar 22, 2024
1 parent 020a222 commit 9feab89
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
14 changes: 11 additions & 3 deletions src/Abstract/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,16 @@ public function toJson(): string|false
public function upload(UnitRequest $request): void
{
$request->setMethod('PUT')
->send("/config/applications/{$this->getName()}", true, [
'json' => array_filter($this->toArray(), fn ($item) => !empty($item))
]);
->send($this->getApiEndpoint(), true, ['json' => array_filter($this->toArray(), fn ($item) => !empty($item))]);
}

public function remove(UnitRequest $request): void
{
$request->setMethod('DELETE')->send($this->getApiEndpoint());
}

public function getApiEndpoint(): string
{
return "/config/applications/{$this->getName()}";
}
}
8 changes: 4 additions & 4 deletions src/Config/Application/PhpApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ final public function parseFromArray(array $data): void
{
parent::parseFromArray($data);

if (!is_string($data['root'])) {
throw new \InvalidArgumentException('root must be a string');
}

if (array_key_exists('root', $data)) {
if (!is_string($data['root'])) {
throw new \InvalidArgumentException('root must be a string');
}

$this->setRoot($data['root']);
}

Expand Down
18 changes: 14 additions & 4 deletions src/Config/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,19 @@ public function toUnitArray()

#[\Override] public function upload(UnitRequest $request): void
{
$request->setMethod('PUT')
->send("/config/listeners/{$this->getListener()}", true, [
'body' => $this->toJson()
]);
$request->setMethod('PUT')->send($this->getApiEndpoint(), true, ['json' => $this->toArray()]);
}

/**
* @throws UnitException
*/
public function remove(UnitRequest $request): void
{
$request->setMethod('DELETE')->send($this->getApiEndpoint());
}

private function getApiEndpoint(): string
{
return "/config/listeners/{$this->getListener()}";
}
}
14 changes: 14 additions & 0 deletions src/Contracts/Uploadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,19 @@

interface Uploadable
{
/**
* Uploads a file using the specified UnitRequest object.
*
* @param UnitRequest $request The UnitRequest object containing the file to be uploaded.
* @return mixed Returns the result of the upload process.
*/
public function upload(UnitRequest $request);

/**
* Removes the specified unit.
*
* @param UnitRequest $request The request object containing the details of the unit to be removed.
* @return mixed The result of the removal operation.
*/
public function remove(UnitRequest $request);
}
17 changes: 17 additions & 0 deletions src/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,25 @@ public function removeConfig(): bool
return true;
}

/**
* Uploads the specified Uploadable object.
*
* @param Uploadable $object
*/
public function upload(Uploadable $object): void
{
$object->upload($this->request);
}

/**
* Removes an Uploadable object.
*
* @param Uploadable $object The Uploadable object to be removed.
*
* @return void
*/
public function remove(Uploadable $object): void
{
$object->remove($this->request);
}
}

0 comments on commit 9feab89

Please sign in to comment.