Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
Add paging order search request
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 27, 2020
1 parent bf83698 commit bb15770
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 21 deletions.
45 changes: 25 additions & 20 deletions examples/120_OrdersSearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@
/** @var \YDeliverySDK\Client $client */
$client = $builder->build();

$request = new OrdersSearchRequest();
$request->senderIds[] = $_SERVER['YANDEX_SHOP_ID'];
$request = new OrdersSearchRequest([
(int) $_SERVER['YANDEX_SHOP_ID'],
]);
$request->term = '+79266056128';
$request->size = 2;

$logger->addFile('orders-search-request.json');
$logger->addFile('orders-search-response.json');

$response = $client->sendOrdersSearchRequest($request);
$orders = $client->searchOrders($request);

if ($response->hasErrors()) {
if ($orders->hasErrors()) {
// Обрабатываем ошибки
foreach ($response->getMessages() as $message) {
foreach ($orders->getMessages() as $message) {
if ($message->getErrorCode() !== '') {
// Это ошибка
echo "{$message->getErrorCode()}: {$message->getMessage()}\n";
Expand All @@ -61,24 +62,28 @@
return;
}

foreach ($response as $order) {
echo "{$order->id}\t{$order->comment}\n";
}
foreach ($orders as $order) {
echo "{$order->id}\t{$order->status}\t{$order->comment}\n";

if (\strpos($order->comment, 'тестовый заказ') === false) {
return;
}
if (\strpos($order->comment, 'тестовый заказ') === false) {
continue;
}

$response = $client->makeDeleteOrderRequest($order->id);
if ($order->status === 'CANCELLED') {
continue;
}

if ($response->hasErrors()) {
// Обрабатываем ошибки
foreach ($response->getMessages() as $message) {
if ($message->getErrorCode() !== '') {
// Это ошибка
echo "{$message->getErrorCode()}: {$message->getMessage()}\n";
$response = $client->makeDeleteOrderRequest($order->id);

if ($response->hasErrors()) {
// Обрабатываем ошибки
foreach ($response->getMessages() as $message) {
if ($message->getErrorCode() !== '') {
// Это ошибка
echo "{$message->getErrorCode()}: {$message->getMessage()}\n";
}
}
}

return;
return;
}
}
7 changes: 6 additions & 1 deletion src/Requests/OrdersSearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,19 @@ final class OrdersSearchRequest implements JsonRequest, ParamRequest
* @param array<int> $partnerIds
* @param array<string> $statuses
*/
public function __construct(array $senderIds = [], array $orderIds = [], array $partnerIds = [], array $statuses = [])
public function __construct(array $senderIds, array $orderIds = [], array $partnerIds = [], array $statuses = [])
{
$this->senderIds = new ArrayProperty($senderIds);
$this->orderIds = new ArrayProperty($orderIds);
$this->partnerIds = new ArrayProperty($partnerIds);
$this->statuses = new ArrayProperty($statuses);
}

public function addPage(): void
{
++$this->page;
}

public function getParams(): array
{
return \array_filter([
Expand Down
59 changes: 59 additions & 0 deletions src/Requests/Shortcuts.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
namespace YDeliverySDK\Requests;

use CommonSDK\Contracts;
use IteratorAggregate;
use function Pipeline\map;
use YDeliverySDK\Client;
use YDeliverySDK\Responses;

/**
Expand All @@ -40,6 +43,7 @@
* @method Responses\DeliveryOptionsResponse|Responses\Types\DeliveryOption[] sendDeliveryOptionsRequest(DeliveryOptionsRequest $request)
* @method Responses\OrderResponse sendCreateOrderRequest(CreateOrderRequest $request)
* @method Responses\SubmitOrderResponse|Responses\Types\SubmittedOrder[] sendSubmitOrderRequest(SubmitOrderRequest $request)
* @method Responses\OrdersSearchResponse|Responses\Types\Order[] sendOrdersSearchRequest(OrdersSearchRequest $request)
* @method Contracts\Response sendDeleteOrderRequest(DeleteOrderRequest $request)
*/
trait Shortcuts
Expand Down Expand Up @@ -75,4 +79,59 @@ public function makeDeleteOrderRequest(int $orderId)

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

/**
* @return iterable|Responses\Types\Order[]
*/
public function searchOrders(OrdersSearchRequest $request)
{
return new class($this, $request) implements Contracts\Response, IteratorAggregate {
private $client;
private $request;

/** @var Contracts\Response|null */
private $response;

public function __construct(Client $client, OrdersSearchRequest $request)
{
$this->client = $client;
$this->request = $request;
}

private function makeRequest(): void
{
$this->response = $this->client->sendOrdersSearchRequest($this->request);
$this->request->addPage();
}

private function getLastResponse(): Contracts\Response
{
if ($this->response === null) {
$this->makeRequest();
}

return $this->response;
}

public function hasErrors(): bool
{
return $this->getLastResponse()->hasErrors();
}

public function getMessages()
{
return $this->getLastResponse()->getMessages();
}

public function getIterator()
{
return map(function () {
do {
yield from $this->getLastResponse();
$this->makeRequest();
} while (\count($this->getLastResponse()) > 0);
});
}
};
}
}
18 changes: 18 additions & 0 deletions src/Responses/Types/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
namespace YDeliverySDK\Responses\Types;

use CommonSDK\Concerns\PropertyRead;
use DateTimeImmutable;
use JMS\Serializer\Annotation as JMS;

/**
* @property-read int $id
* @property-read string|null $externalId
* @property-read string|null $comment;
* @property-read DateTimeImmutable $created
* @property-read string $status
*/
final class Order
{
Expand All @@ -59,4 +63,18 @@ final class Order
* @var string
*/
private $comment;

/**
* @JMS\Type("DateTimeImmutable<'Y-m-d\TH:i:s.uO'>")
*
* @var DateTimeImmutable
*/
private $created;

/**
* @JMS\Type("string")
*
* @var string
*/
private $status;
}

0 comments on commit bb15770

Please sign in to comment.