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

Orders.php$0 ➔ delete()   A

Complexity

Conditions 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 17
ccs 0
cts 5
cp 0
crap 2
rs 9.7
c 0
b 0
f 0

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\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