Passed
Push — master ( 3a1e08...f90194 )
by Jhao
02:17
created

Batches::findOrdersInAllBatches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of RussianPost SDK package.
5
 *
6
 * © Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda)
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Batches;
15
16
use Appwilio\RussianPostSDK\Core\ArrayOf;
17
use Appwilio\RussianPostSDK\Core\GenericRequest;
18
use Appwilio\RussianPostSDK\Dispatching\Http\ApiClient;
19
use Appwilio\RussianPostSDK\Dispatching\Exceptions\BadRequest;
20
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Order;
21
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Batches\Entites\Batch;
22
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Exceptions\OrderNotFound;
23
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Batches\Exceptions\BatchNotFound;
24
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Batches\Requests\FindBatchRequest;
25
26
final class Batches
27
{
28
    /** @var ApiClient */
29
    private $client;
30
31 1
    public function __construct(ApiClient $client)
32
    {
33 1
        $this->client = $client;
34 1
    }
35
36
    /**
37
     * Получение партии по имени (идентификатору).
38
     *
39
     * @see https://otpravka.pochta.ru/specification#/batches-find_batch
40
     *
41
     * @param  string  $name  имя (идентификатор) партии
42
     *
43
     * @throws OrderNotFound
44
     *
45
     * @return Batch
46
     */
47
    public function getByName(string $name): Batch
48
    {
49
        try {
50
            return $this->client->get("/1.0/batch/{$name}", null, Batch::class);
51
        } catch (BadRequest $e) {
52
            throw new BatchNotFound($name, $e);
53
        }
54
    }
55
56
    /**
57
     * Поиск партий.
58
     *
59
     * @see https://otpravka.pochta.ru/specification#/batches-search_all_batches
60
     *
61
     * @param  FindBatchRequest  $request
62
     *
63
     * @return iterable|Batch[]|null
64
     */
65
    public function find(FindBatchRequest $request): ?iterable
66
    {
67
        $response = $this->client->get('/1.0/batch', $request, new ArrayOf(Batch::class));
68
69
        return empty($response) ? null : $response;
70
    }
71
72
    /**
73
     * Запрос данных о заказах в партии.
74
     *
75
     * @see https://otpravka.pochta.ru/specification#/batches-get_info_about_orders_in_batch
76
     *
77
     * @param  string    $name
78
     * @param  int|null  $page
79
     * @param  int|null  $perPage
80
     * @param  string    $sortBy
81
     *
82
     * @return Order[]
83
     */
84
    public function getOrdersInBatch(string $name, ?int $page = null, ?int $perPage = null, string $sortBy = 'asc')
85
    {
86
        $query = GenericRequest::create([
87
            'page' => $page,
88
            'size' => $perPage,
89
            'sort' => $sortBy,
90
        ]);
91
92
        return $this->client->get("/1.0/batch/{$name}/shipment", $query, new ArrayOf(Order::class));
93
    }
94
95
    public function getOrderById(string $id): Order
96
    {
97
        try {
98
            return $this->client->get("/1.0/shipment/{$id}", null, Order::class);
99
        } catch (BadRequest $e) {
100
            throw new OrderNotFound($id, $e);
101
        }
102
    }
103
104
    /**
105
     * Поиск заказов по ШПИ или внутреннему номеру во всех партиях.
106
     *
107
     * @param  string  $query  ШПИ или внутренний номер заказа
108
     *
109
     * @see https://otpravka.pochta.ru/specification#/batches-find_orders_with_barcode
110
     *
111
     * @return Order[]|null
112
     */
113
    public function findOrdersInAllBatches(string $query): ?iterable
114
    {
115
        return $this->client->get(
116
            '/1.0/shipment/search',
117
            GenericRequest::create(['query' => $query]),
118
            new ArrayOf(Order::class)
119
        );
120
    }
121
}
122