Skip to content

Commit

Permalink
Add product endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 17, 2024
1 parent 93b6811 commit e96dc4f
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Setono\PeakWMS\Client\Endpoint\ProductEndpoint;
use Setono\PeakWMS\Client\Endpoint\ProductEndpointInterface;
use Setono\PeakWMS\Client\Endpoint\SalesOrderEndpoint;
use Setono\PeakWMS\Client\Endpoint\SalesOrderEndpointInterface;
use Setono\PeakWMS\Client\Endpoint\WebhookEndpoint;
Expand All @@ -33,6 +35,8 @@ final class Client implements ClientInterface, LoggerAwareInterface

private ?ResponseInterface $lastResponse = null;

private ?ProductEndpointInterface $productEndpoint = null;

private ?SalesOrderEndpointInterface $salesOrderEndpoint = null;

private ?WebhookEndpointInterface $webhookEndpoint = null;
Expand Down Expand Up @@ -124,6 +128,16 @@ public function ping(): void
$this->get('ping');
}

public function product(): ProductEndpointInterface
{
if (null === $this->productEndpoint) {
$this->productEndpoint = new ProductEndpoint($this, $this->getMapperBuilder(), 'product');
$this->productEndpoint->setLogger($this->logger);
}

return $this->productEndpoint;
}

public function salesOrder(): SalesOrderEndpointInterface
{
if (null === $this->salesOrderEndpoint) {
Expand Down
3 changes: 3 additions & 0 deletions src/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Setono\PeakWMS\Client\Endpoint\ProductEndpointInterface;
use Setono\PeakWMS\Client\Endpoint\SalesOrderEndpointInterface;
use Setono\PeakWMS\Client\Endpoint\WebhookEndpointInterface;
use Setono\PeakWMS\Exception\InternalServerErrorException;
Expand Down Expand Up @@ -53,6 +54,8 @@ public function delete(string $uri, int $id): ResponseInterface;
*/
public function ping(): void;

public function product(): ProductEndpointInterface;

public function salesOrder(): SalesOrderEndpointInterface;

public function webhook(): WebhookEndpointInterface;
Expand Down
42 changes: 42 additions & 0 deletions src/Client/Endpoint/ProductEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Client\Endpoint;

use Setono\PeakWMS\DataTransferObject\Collection;
use Setono\PeakWMS\DataTransferObject\Product\Product;

/**
* @extends Endpoint<Product>
*/
final class ProductEndpoint extends Endpoint implements ProductEndpointInterface
{
/**
* @use CreatableEndpointTrait<Product>
*/
use CreatableEndpointTrait;

use DeletableEndpointTrait;

public function getByProductId(string $productId): Collection
{
/** @var class-string<Collection<Product>> $signature */
$signature = sprintf('%s<%s>', Collection::class, self::getDataClass());

return $this
->mapperBuilder
->mapper()
->map(
$signature,
$this->createSource(
$this->client->get(sprintf('%s/%s', $this->endpoint, $productId)),
),
);
}

protected static function getDataClass(): string
{
return Product::class;
}
}
20 changes: 20 additions & 0 deletions src/Client/Endpoint/ProductEndpointInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Client\Endpoint;

use Setono\PeakWMS\DataTransferObject\Collection;
use Setono\PeakWMS\DataTransferObject\Product\Product;

/**
* @extends EndpointInterface<Product>
* @extends CreatableEndpointInterface<Product>
*/
interface ProductEndpointInterface extends EndpointInterface, CreatableEndpointInterface, DeletableEndpointInterface
{
/**
* @return Collection<Product>
*/
public function getByProductId(string $productId): Collection;
}
42 changes: 42 additions & 0 deletions src/DataTransferObject/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\DataTransferObject;

/**
* @template T of AbstractDataTransferObject
*
* @implements \IteratorAggregate<int, T>
*/
final class Collection implements \IteratorAggregate, \Countable
{
/** @var list<T> */
public array $items;

/**
* @param list<T> $items
*/
public function __construct(array $items = [])
{
$this->items = $items;
}

public function empty(): bool
{
return [] === $this->items;
}

/**
* @return \ArrayIterator<int<0, max>, T>
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->items);
}

public function count(): int
{
return count($this->items);
}
}
75 changes: 75 additions & 0 deletions src/DataTransferObject/Product/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\DataTransferObject\Product;

use Setono\PeakWMS\DataTransferObject\AbstractDataTransferObject;

final class Product extends AbstractDataTransferObject
{
public function __construct(
public ?string $createdAt = null,
public ?string $updatedAt = null,
public ?int $id = null,
public ?string $productId = null,
public ?string $variantId = null,
public ?string $itemNumber = null,
public ?string $description = null,
public ?string $comment = null,
public ?string $countryOfOrigin = null,
public ?string $quantityInUnits = null,
public ?string $units = null,
/**
* @var list<array{code: string}>|null
*/
public ?array $customsTariffs = null,
public ?string $imagePath = null,
public ?string $ean = null,
public ?int $state = null,
public ?int $pickType = null,
public ?int $cycleCountThreshold = null,
public ?int $cycleCountInterval = null,
public ?int $weight = null,
public ?int $netWeight = null,
public ?int $volume = null,
public ?int $length = null,
public ?int $width = null,
public ?int $height = null,
public ?float $averageCostPrice = null,
public ?float $costPricePiece = null,
public ?float $salesPricePerPiece = null,
public ?bool $bestBeforeDateControlled = null,
public ?bool $lotNumberControlled = null,
public ?bool $lotRequiredForOrder = null,
public ?bool $includeInPoForecast = null,
public ?string $variantMaster = null,
public ?int $minStockThreshold = null,
public ?int $maxStockThreshold = null,
public ?string $brand = null,
public ?int $availableToSell = null,
public ?int $availableInStock = null,
public ?int $orderedByCustomers = null,
public ?bool $production = null,
public ?bool $autoAssignLot = null,
/**
* @var list<array{name: string, vendorName: string}>|null
*/
public ?array $vendors = null,
/**
* @var list<string>|null
*/
public ?array $allowedAreas = null,
public ?string $dangerousGoodsClass = null,
public ?string $size = null,
public ?string $composition = null,
public ?string $collection = null,
public ?string $sizeRange = null,
public ?string $styleNumber = null,
public ?string $styleDescription = null,
public ?string $colorNumber = null,
public ?string $colorDescription = null,
public ?string $productGroup = null,
) {
}
}

0 comments on commit e96dc4f

Please sign in to comment.