Passed
Push — master ( d743e2...20969e )
by Jhao
01:54
created

Orders::getByPostalId()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders;
6
7
use Appwilio\RussianPostSDK\Dispatching\Http\ApiClient;
8
use Appwilio\RussianPostSDK\Dispatching\Contracts\Arrayable;
9
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Order;
10
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Requests\OrderRequest;
11
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Exceptions\OrderException;
12
13
final class Orders
14
{
15
    /** @var ApiClient */
16
    private $client;
17
18
    public function __construct(ApiClient $client)
19
    {
20
        $this->client = $client;
21
    }
22
23
    /**
24
     * Создание заказа.
25
     *
26
     * @link https://otpravka.pochta.ru/specification#/orders-creating_order
27
     *
28
     * @param  OrderRequest  $request
29
     *
30
     * @throws OrderException
31
     *
32
     * @return string номер созданного заказа
33
     */
34
    public function create(OrderRequest $request): string
35
    {
36
        $response = $this->client->put('/1.0/user/backlog', $request);
37
38
        if (isset($response['result-ids'])) {
39
            return (string) $response['result-ids'][0];
40
        }
41
42
        if (isset($response['errors'])) {
43
            throw new OrderException($response['errors'][0]['error-codes']);
44
        }
45
46
        return $response;
47
    }
48
49
    public function getByPostalId(string $id): Order
50
    {
51
        return $this->client->get("/1.0/backlog/{$id}", null, Order::class);
52
    }
53
54
    public function findByShopId(string $id)
55
    {
56
        return $this->client->get('backlog/search', new class($id) implements Arrayable {
57
            private $query;
58
59
            public function __construct(string $query)
60
            {
61
                $this->query = $query;
62
            }
63
64
            public function toArray(): array
65
            {
66
                return ['query' => $this->query];
67
            }
68
        }, Order::class);
69
    }
70
71
    public function findByTrackingNumber(string $number)
72
    {
73
        return $this->client->get('/1.0/shipment/search', new class($number) implements Arrayable {
74
            private $query;
75
76
            public function __construct(string $query)
77
            {
78
                $this->query = $query;
79
            }
80
81
            public function toArray(): array
82
            {
83
                return ['query' => $this->query];
84
            }
85
        }, Order::class);
86
    }
87
88
    public function read(string $id)
89
    {
90
        return $this->client->get("/1.0/user/backlog/{$id}");
91
    }
92
93
    public function update(string $id, OrderRequest $request)
94
    {
95
        return $this->client->put("/1.0/user/backlog/{$id}", $request);
96
    }
97
98
    public function delete(array $ids)
99
    {
100
        $response = $this->client->delete('/1.0/user/backlog', new class($ids) implements Arrayable {
101
            private $ids;
102
103
            public function __construct(array $ids)
104
            {
105
                $this->ids = $ids;
106
            }
107
108
            public function toArray(): array
109
            {
110
                return $this->ids;
111
            }
112
        });
113
114
        if (isset($response['errors'])) {
115
            throw new OrderException($response['errors'][0]);
116
        }
117
    }
118
119
    public function renew(array $ids)
120
    {
121
        return $this->client->post('/1.0/user/backlog', new class($ids) implements Arrayable {
122
            private $ids;
123
124
            public function __construct(array $ids)
125
            {
126
                $this->ids = $ids;
127
            }
128
129
            public function toArray(): array
130
            {
131
                return $this->ids;
132
            }
133
        });
134
    }
135
}
136