Passed
Push — master ( 8428c1...16b454 )
by Jhao
02:06
created

Orders   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 49
ccs 0
cts 5
cp 0
rs 10
c 1
b 0
f 0
eloc 12
wmc 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 9 2
A __construct() 0 3 1
A hp$0 ➔ toArray() 0 3 1
A hp$0 ➔ delete() 0 17 1
delete() 0 17 ?
A hp$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\Dispatching\Http\ApiClient;
8
use Appwilio\RussianPostSDK\Dispatching\Contracts\Arrayable;
9
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Requests\OrderRequest;
10
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Exceptions\OrderException;
11
12
final class Orders
13
{
14
    /** @var ApiClient */
15
    private $client;
16
17
    public function __construct(ApiClient $client)
18
    {
19
        $this->client = $client;
20
    }
21
22
    /**
23
     * Создание заказа.
24
     *
25
     * @link https://otpravka.pochta.ru/specification#/orders-creating_order
26
     *
27
     * @param  OrderRequest  $request
28
     *
29
     * @throws OrderException
30
     *
31
     * @return string номер созданного заказа
32
     */
33
    public function create(OrderRequest $request): string
34
    {
35
        $response = $this->client->put('/1.0/user/backlog', $request);
36
37
        if (isset($response['result-ids'])) {
38
            return (string) $response['result-ids'][0];
39
        }
40
41
        return $response['errors'] ?? $response;
42
    }
43
44
    public function delete(array $ids)
45
    {
46
        $response = $this->client->delete('/1.0/user/backlog', new class($ids) implements Arrayable {
47
            private $ids;
48
49
            public function __construct(array $ids)
50
            {
51
                $this->ids = $ids;
52
            }
53
54
            public function toArray(): array
55
            {
56
                return $this->ids;
57
            }
58
        });
59
60
        return $response['result-ids'] ?? $response['errors'] ?? $response;
61
    }
62
}
63