Passed
Push — master ( 263c14...2fa329 )
by Jhao
02:31
created

Batches::findOrderByTrackingNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
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
     *
79
     * @return Order[]
80
     */
81
    public function getOrders(string $name)
82
    {
83
        return $this->client->get("/1.0/batch/{$name}/shipment", null, new ArrayOf(Order::class));
84
    }
85
86
    public function getOrderById(string $id): Order
87
    {
88
        try {
89
            return $this->client->get("/1.0/shipment/{$id}", null, Order::class);
90
        } catch (BadRequest $e) {
91
            throw new OrderNotFound($id, $e);
92
        }
93
    }
94
95
    /**
96
     * @param  string  $barcode
97
     * @return Order|null
98
     */
99
    public function findOrderByTrackingNumber(string $barcode): ?Order
100
    {
101
        return $this->client->get('/1.0/shipment/search', GenericRequest::create(['query' => $barcode]), Order::class);
102
    }
103
}
104