Skip to content

Commit

Permalink
Add put method to client to be able to update sales orders
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Aug 21, 2024
1 parent 97b01d7 commit 89918ea
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ public function post(string $uri, array|object $body): ResponseInterface
return $this->request($request);
}

public function put(string $uri, array|object $body): ResponseInterface
{
$url = sprintf('%s/%s', $this->getBaseUri(), ltrim($uri, '/'));

$request = $this->getRequestFactory()
->createRequest('PUT', $url)
->withBody(
$this->getStreamFactory()
->createStream(json_encode($body, \JSON_THROW_ON_ERROR)),
)
;

return $this->request($request);
}

public function delete(string $uri, int $id): ResponseInterface
{
$url = sprintf('%s/%s/%d', $this->getBaseUri(), ltrim($uri, '/'), $id);
Expand Down
2 changes: 2 additions & 0 deletions src/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function get(string $uri, Query|array $query = []): ResponseInterface;

public function post(string $uri, array|object $body): ResponseInterface;

public function put(string $uri, array|object $body): ResponseInterface;

public function delete(string $uri, int $id): ResponseInterface;

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Client/Endpoint/SalesOrderEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ final class SalesOrderEndpoint extends Endpoint implements SalesOrderEndpointInt

use DeletableEndpointTrait;

/**
* @use UpdatableEndpointTrait<SalesOrder>
*/
use UpdatableEndpointTrait;

protected static function getDataClass(): string
{
return SalesOrder::class;
Expand Down
3 changes: 2 additions & 1 deletion src/Client/Endpoint/SalesOrderEndpointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
/**
* @extends EndpointInterface<SalesOrder>
* @extends CreatableEndpointInterface<SalesOrder>
* @extends UpdatableEndpointInterface<SalesOrder>
*/
interface SalesOrderEndpointInterface extends EndpointInterface, CreatableEndpointInterface, DeletableEndpointInterface
interface SalesOrderEndpointInterface extends EndpointInterface, CreatableEndpointInterface, DeletableEndpointInterface, UpdatableEndpointInterface
{
}
18 changes: 18 additions & 0 deletions src/Client/Endpoint/UpdatableEndpointInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Client\Endpoint;

use Setono\PeakWMS\DataTransferObject\AbstractDataTransferObject;

/**
* @template T of AbstractDataTransferObject
*/
interface UpdatableEndpointInterface
{
/**
* @param mixed $id This is (most likely) the host id
*/
public function update(mixed $id, AbstractDataTransferObject $data): void;
}
23 changes: 23 additions & 0 deletions src/Client/Endpoint/UpdatableEndpointTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Client\Endpoint;

use Setono\PeakWMS\DataTransferObject\AbstractDataTransferObject;

/**
* @mixin Endpoint
*
* @template T of AbstractDataTransferObject
*/
trait UpdatableEndpointTrait
{
/**
* @param T|AbstractDataTransferObject $data
*/
public function update(mixed $id, AbstractDataTransferObject $data): void
{
$this->client->put(sprintf('%s/%s', $this->endpoint, (string) $id), $data);
}
}

0 comments on commit 89918ea

Please sign in to comment.