Skip to content

Commit c5e6105

Browse files
committed
Add stock endpoint
1 parent 887bbc6 commit c5e6105

File tree

9 files changed

+153
-3
lines changed

9 files changed

+153
-3
lines changed

src/Client/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Setono\PeakWMS\Client\Endpoint\ProductEndpointInterface;
2020
use Setono\PeakWMS\Client\Endpoint\SalesOrderEndpoint;
2121
use Setono\PeakWMS\Client\Endpoint\SalesOrderEndpointInterface;
22+
use Setono\PeakWMS\Client\Endpoint\StockEndpoint;
23+
use Setono\PeakWMS\Client\Endpoint\StockEndpointInterface;
2224
use Setono\PeakWMS\Client\Endpoint\WebhookEndpoint;
2325
use Setono\PeakWMS\Client\Endpoint\WebhookEndpointInterface;
2426
use Setono\PeakWMS\Exception\InternalServerErrorException;
@@ -39,6 +41,8 @@ final class Client implements ClientInterface, LoggerAwareInterface
3941

4042
private ?SalesOrderEndpointInterface $salesOrderEndpoint = null;
4143

44+
private ?StockEndpointInterface $stockEndpoint = null;
45+
4246
private ?WebhookEndpointInterface $webhookEndpoint = null;
4347

4448
private ?HttpClientInterface $httpClient = null;
@@ -164,6 +168,16 @@ public function salesOrder(): SalesOrderEndpointInterface
164168
return $this->salesOrderEndpoint;
165169
}
166170

171+
public function stock(): StockEndpointInterface
172+
{
173+
if (null === $this->stockEndpoint) {
174+
$this->stockEndpoint = new StockEndpoint($this, $this->getMapperBuilder(), 'stock');
175+
$this->stockEndpoint->setLogger($this->logger);
176+
}
177+
178+
return $this->stockEndpoint;
179+
}
180+
167181
public function webhook(): WebhookEndpointInterface
168182
{
169183
if (null === $this->webhookEndpoint) {

src/Client/ClientInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Http\Message\ResponseInterface;
1010
use Setono\PeakWMS\Client\Endpoint\ProductEndpointInterface;
1111
use Setono\PeakWMS\Client\Endpoint\SalesOrderEndpointInterface;
12+
use Setono\PeakWMS\Client\Endpoint\StockEndpointInterface;
1213
use Setono\PeakWMS\Client\Endpoint\WebhookEndpointInterface;
1314
use Setono\PeakWMS\Exception\InternalServerErrorException;
1415
use Setono\PeakWMS\Exception\NotFoundException;
@@ -60,5 +61,7 @@ public function product(): ProductEndpointInterface;
6061

6162
public function salesOrder(): SalesOrderEndpointInterface;
6263

64+
public function stock(): StockEndpointInterface;
65+
6366
public function webhook(): WebhookEndpointInterface;
6467
}

src/Client/Endpoint/Endpoint.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
/**
1717
* @template T of AbstractDataTransferObject
18-
*
1918
* @implements EndpointInterface<T>
2019
*/
2120
abstract class Endpoint implements EndpointInterface, LoggerAwareInterface
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\PeakWMS\Client\Endpoint;
6+
7+
use Setono\PeakWMS\DataTransferObject\PaginatedCollection;
8+
use Setono\PeakWMS\DataTransferObject\Stock\Stock;
9+
use Setono\PeakWMS\Request\Query\KeySetPageQuery;
10+
11+
/**
12+
* @extends Endpoint<Stock>
13+
*/
14+
final class StockEndpoint extends Endpoint implements StockEndpointInterface
15+
{
16+
/**
17+
* @return PaginatedCollection<Stock>
18+
*/
19+
public function getPage(KeySetPageQuery $query = null): PaginatedCollection
20+
{
21+
$query ??= KeySetPageQuery::create();
22+
23+
/** @var class-string<PaginatedCollection<Stock>> $signature */
24+
$signature = sprintf('%s<%s>', PaginatedCollection::class, self::getDataClass());
25+
26+
return $this
27+
->mapperBuilder
28+
->mapper()
29+
->map(
30+
$signature,
31+
$this->createSource(
32+
$this->client->get(sprintf('%s/keySet', $this->endpoint), $query),
33+
)->map(['data' => 'items']),
34+
);
35+
}
36+
37+
/**
38+
* @return \Generator<Stock>
39+
*/
40+
public function iterate(KeySetPageQuery $query = null): \Generator
41+
{
42+
$query ??= KeySetPageQuery::create();
43+
44+
do {
45+
$collection = $this->getPage($query);
46+
47+
$lastId = null;
48+
49+
foreach ($collection as $item) {
50+
yield $item;
51+
$lastId = $item->id;
52+
}
53+
54+
if (null !== $lastId) {
55+
$query->setLastId($lastId);
56+
}
57+
} while (!$collection->empty());
58+
}
59+
60+
protected static function getDataClass(): string
61+
{
62+
return Stock::class;
63+
}
64+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\PeakWMS\Client\Endpoint;
6+
7+
use Setono\PeakWMS\DataTransferObject\PaginatedCollection;
8+
use Setono\PeakWMS\DataTransferObject\Stock\Stock;
9+
use Setono\PeakWMS\Request\Query\KeySetPageQuery;
10+
11+
/**
12+
* @extends EndpointInterface<Stock>
13+
*/
14+
interface StockEndpointInterface extends EndpointInterface
15+
{
16+
/**
17+
* @return PaginatedCollection<Stock>
18+
*/
19+
public function getPage(KeySetPageQuery $query = null): PaginatedCollection;
20+
21+
/**
22+
* @return iterable<Stock>
23+
*/
24+
public function iterate(KeySetPageQuery $query = null): iterable;
25+
}

src/DataTransferObject/Collection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* @template T of AbstractDataTransferObject
9-
*
109
* @implements \IteratorAggregate<int, T>
1110
* @implements \ArrayAccess<int, T>
1211
*/

src/DataTransferObject/PaginatedCollection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* @template T of AbstractDataTransferObject
9-
*
109
* @extends Collection<T>
1110
*/
1211
final class PaginatedCollection extends Collection
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\PeakWMS\DataTransferObject\Stock;
6+
7+
use Setono\PeakWMS\DataTransferObject\AbstractDataTransferObject;
8+
9+
final class Stock extends AbstractDataTransferObject
10+
{
11+
public function __construct(
12+
public readonly ?int $id = null,
13+
public readonly ?string $productId = null,
14+
public readonly ?string $variantId = null,
15+
public readonly ?string $itemNumber = null,
16+
public readonly ?int $quantity = null,
17+
public readonly ?int $reservedQuantity = null,
18+
) {
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\PeakWMS\Request\Query;
6+
7+
final class KeySetPageQuery extends Query
8+
{
9+
/**
10+
* @param int|null $lastId The last id from the previous page
11+
* @param int $pageSize The maximum page size is 250
12+
*/
13+
public static function create(int $lastId = null, int $pageSize = 250): self
14+
{
15+
return new self(array_filter([
16+
'LastId' => $lastId,
17+
'PageSize' => $pageSize,
18+
]));
19+
}
20+
21+
public function setLastId(int $lastId): self
22+
{
23+
$this->parameters['LastId'] = $lastId;
24+
25+
return $this;
26+
}
27+
}

0 commit comments

Comments
 (0)