Orders.php$1 ➔ renew()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 4
cp 0
rs 9.8333
cc 1
crap 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Orders.php$1 ➔ toArray() 0 3 1
A Orders.php$1 ➔ __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders;
6
7
use Appwilio\RussianPostSDK\Core\Arrayable;
8
use Appwilio\RussianPostSDK\Core\ArrayOf;
9
use Appwilio\RussianPostSDK\Dispatching\Http\ApiClient;
10
use Appwilio\RussianPostSDK\Dispatching\Exceptions\BadRequest;
11
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Order;
12
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Requests\OrderRequest;
13
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Exceptions\OrderNotFound;
14
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Exceptions\OrderException;
15
16
final class Orders
17
{
18
    /** @var ApiClient */
19
    private $client;
20
21
    public function __construct(ApiClient $client)
22
    {
23
        $this->client = $client;
24
    }
25
26
    /**
27
     * Создание заказа.
28
     *
29
     * @see https://otpravka.pochta.ru/specification#/orders-creating_order
30
     *
31
     * @param  OrderRequest  $request
32
     *
33
     * @throws OrderException
34
     *
35
     * @return string номер созданного заказа
36
     */
37
    public function create(OrderRequest $request): string
38
    {
39
        $response = $this->client->put('/1.0/user/backlog', $request);
40
41
        if (isset($response['result-ids'])) {
42
            return (string) $response['result-ids'][0];
43
        }
44
45
        if (isset($response['errors'])) {
46
            throw new OrderException($response['errors'][0]['error-codes']);
47
        }
48
49
        return $response;
50
    }
51
52
    /**
53
     * Получение заказа по внутрипочтовому идентификатору.
54
     *
55
     * @see https://otpravka.pochta.ru/specification#/orders-search_order_byid
56
     *
57
     * @param  string  $id  внутрипочтовый идентификатор
58
     *
59
     * @throws OrderNotFound
60
     *
61
     * @return Order
62
     */
63
    public function getById(string $id)
64
    {
65
        try {
66
            return $this->client->get("/1.0/backlog/{$id}", null, Order::class);
67
        } catch (BadRequest $e) {
68
            throw new OrderNotFound($id, $e);
69
        }
70
    }
71
72
    /**
73
     * Поиск заказов по идентификатору в системе отправителя.
74
     *
75
     * @see https://otpravka.pochta.ru/specification#/orders-search_order
76
     *
77
     * @param  string  $number  идентификатор в системе отправителя
78
     *
79
     * @return iterable|Order[]|null
80
     */
81
    public function findByShopNumber(string $number): ?iterable
82
    {
83
        $response = $this->client->get(
84
            '/1.0/backlog/search?'.\http_build_query(['query' => $number]), null, new ArrayOf(Order::class)
85
        );
86
87
        return empty($response) ? null : $response;
88
    }
89
90
    /**
91
     * Редактирование заказа.
92
     *
93
     * @see https://otpravka.pochta.ru/specification#/orders-editing_order
94
     *
95
     * @param  string        $id       внутрипочтовый идентификатор
96
     * @param  OrderRequest  $request
97
     *
98
     * @throws OrderNotFound
99
     *
100
     * @return Order
101
     */
102
    public function update(string $id, OrderRequest $request)
103
    {
104
        try {
105
            return $this->client->put("/1.0/backlog/{$id}", $request);
106
        } catch (BadRequest $e) {
107
            throw new OrderNotFound($id, $e);
108
        }
109
    }
110
111
    public function delete(array $ids)
112
    {
113
        $response = $this->client->delete('/1.0/user/backlog', new class($ids) implements Arrayable {
114
            private $ids;
115
116
            public function __construct(array $ids)
117
            {
118
                $this->ids = $ids;
119
            }
120
121
            public function toArray(): array
122
            {
123
                return $this->ids;
124
            }
125
        });
126
127
        if (isset($response['errors'])) {
128
            throw new OrderException($response['errors'][0]);
129
        }
130
    }
131
132
    public function renew(array $ids)
133
    {
134
        return $this->client->post('/1.0/user/backlog', new class($ids) implements Arrayable {
135
            private $ids;
136
137
            public function __construct(array $ids)
138
            {
139
                $this->ids = $ids;
140
            }
141
142
            public function toArray(): array
143
            {
144
                return $this->ids;
145
            }
146
        });
147
    }
148
}
149