Passed
Push — master ( ff7c30...ced429 )
by Jhao
02:29
created

Orders.php$0 ➔ delete()   A

Complexity

Conditions 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 9.6666
cc 2
crap 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Orders.php$0 ➔ toArray() 0 3 1
A Orders.php$0 ➔ __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\ArrayOf;
8
use Appwilio\RussianPostSDK\Dispatching\Http\ApiClient;
9
use Appwilio\RussianPostSDK\Dispatching\Exceptions\BadRequest;
10
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Order;
11
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Exceptions\NotFound;
12
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Requests\OrderRequest;
13
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Exceptions\OrderException;
14
15
final class Orders
16
{
17
    /** @var ApiClient */
18
    private $client;
19
20
    public function __construct(ApiClient $client)
21
    {
22
        $this->client = $client;
23
    }
24
25
    /**
26
     * Создание заказа.
27
     *
28
     * @see https://otpravka.pochta.ru/specification#/orders-creating_order
29
     *
30
     * @param  OrderRequest  $request
31
     *
32
     * @throws OrderException
33
     *
34
     * @return string номер созданного заказа
35
     */
36
    public function create(OrderRequest $request): string
37
    {
38
        $response = $this->client->put('/1.0/user/backlog', $request);
39
40
        if (isset($response['result-ids'])) {
41
            return (string) $response['result-ids'][0];
42
        }
43
44
        if (isset($response['errors'])) {
45
            throw new OrderException($response['errors'][0]['error-codes']);
46
        }
47
48
        return $response;
49
    }
50
51
    /**
52
     * Получение заказа по внутрипочтовому идентификатору.
53
     *
54
     * @see https://otpravka.pochta.ru/specification#/orders-search_order_byid
55
     *
56
     * @param  string  $id  внутрипочтовый идентификатор
57
     *
58
     * @throws NotFound
59
     *
60
     * @return Order
61
     */
62
    public function getById(string $id)
63
    {
64
        try {
65
            return $this->client->get("/1.0/backlog/{$id}", null, Order::class);
66
        } catch (BadRequest $e) {
67
            throw new NotFound($id);
68
        }
69
    }
70
71
    /**
72
     * Поиск заказов по идентификатору в системе отправителя.
73
     *
74
     * @see https://otpravka.pochta.ru/specification#/orders-search_order
75
     *
76
     * @param  string  $number  идентификатор в системе отправителя
77
     *
78
     * @return iterable|Order[]|null
79
     */
80
    public function findByShopNumber(string $number): ?iterable
81
    {
82
        $response = $this->client->get(
83
            '/1.0/backlog/search?'.\http_build_query(['query' => $number]), null, new ArrayOf(Order::class)
84
        );
85
86
        return empty($response) ? null : $response;
87
    }
88
89
    /**
90
     * Редактирование заказа.
91
     *
92
     * @see https://otpravka.pochta.ru/specification#/orders-editing_order
93
     *
94
     * @param  string        $id       внутрипочтовый идентификатор
95
     * @param  OrderRequest  $request
96
     *
97
     * @throws NotFound
98
     *
99
     * @return Order
100
     */
101
    public function update(string $id, OrderRequest $request)
102
    {
103
        try {
104
            return $this->client->put("/1.0/backlog/{$id}", $request);
105
        } catch (BadRequest $e) {
106
            throw new NotFound($id);
107
        }
108
    }
109
110
    public function delete(array $ids)
111
    {
112
        $response = $this->client->delete('/1.0/user/backlog', new class($ids) implements Arrayable {
0 ignored issues
show
Bug introduced by
The type Appwilio\RussianPostSDK\...points\Orders\Arrayable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
113
            private $ids;
114
115
            public function __construct(array $ids)
116
            {
117
                $this->ids = $ids;
118
            }
119
120
            public function toArray(): array
121
            {
122
                return $this->ids;
123
            }
124
        });
125
126
        if (isset($response['errors'])) {
127
            throw new OrderException($response['errors'][0]);
128
        }
129
    }
130
131
    public function renew(array $ids)
132
    {
133
        return $this->client->post('/1.0/user/backlog', new class($ids) implements Arrayable {
134
            private $ids;
135
136
            public function __construct(array $ids)
137
            {
138
                $this->ids = $ids;
139
            }
140
141
            public function toArray(): array
142
            {
143
                return $this->ids;
144
            }
145
        });
146
    }
147
}
148