OrderRequest::withCustomsDeclaration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Requests;
6
7
use Appwilio\RussianPostSDK\Core\Arrayable;
8
use Appwilio\RussianPostSDK\Dispatching\Enum\MailType;
9
use Appwilio\RussianPostSDK\Dispatching\Entities\Address;
10
use Appwilio\RussianPostSDK\Dispatching\Enum\MailCategory;
11
use Appwilio\RussianPostSDK\Dispatching\Entities\CommonOrder;
12
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\EcomData;
13
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\OrderItem;
14
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Recipient;
15
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\FiscalData;
16
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\CustomsDeclaration;
17
18
final class OrderRequest implements Arrayable
19
{
20
    use CommonOrder;
21
22
    private const RUSSIAN_POSTAL_CODE = '~\d{6}~';
23
24
    /** @var Address */
25
    private $address;
26
27
    /** @var OrderItem[] */
28
    private $items = [];
29
30
    /** @var CustomsDeclaration|null */
31
    private $declaration;
32
33
    /** @var EcomData */
34
    private $ecomData;
35
36
    /** @var FiscalData */
37
    private $fiscalData;
38
39
    /** @var Recipient */
40
    private $recipient;
41
42
    public static function create(
43
        string $number,
44
        MailType $type,
45
        MailCategory $category,
46
        int $weight,
47
        Address $address,
48
        Recipient $recipient
49
    ) {
50
        return new self(...\func_get_args());
51
    }
52
53
//    public static function fromOrder(Order $order): self
54
//    {
55
//        [
56
//            'completeness-checking', 'compulsory-payment', 'comment', 'courier', 'delivery-with-cod', 'dimension',
57
//            'envelope-type', 'fragile', 'insr-value', 'inventory', 'mail-category', 'mail-type',
58
//            'no-return', 'easy-return', 'notice-payment-method', 'payment-method', 'sms-notice-recipient',
59
//        ];
60
//
61
//        $request = new self(
62
//            $order->getNumber(), MailType::BANDEROL(), MailCategory::ORDINARY(),
63
//            $order->getWeight(), $order->getAddress(), $order->getRecipient()
64
//        );
65
//
66
//        if ($order->hasCustomsDeclaration()) {
67
//            $request->withCustomsDeclaration($order->getCustomsDeclaration());
68
//        }
69
//
70
//        foreach ($order->getItems() as $item) {
71
//            $request->addItem($item);
72
//        }
73
//
74
//        return $request;
75
//    }
76
77
    public function __construct(
78
        string $number,
79
        MailType $mailType,
80
        MailCategory $mailCategory,
81
        int $weight,
82
        Address $address,
83
        Recipient $recipient
84
    ) {
85
        $this->data = [
86
            'mass'          => $weight,
87
            'order-num'     => $number,
88
            'mail-type'     => $mailType,
89
            'mail-category' => $mailCategory,
90
        ];
91
92
        $this->address = $address;
93
        $this->recipient = $recipient;
94
    }
95
96
    public function addItem(OrderItem $item): void
97
    {
98
        $this->items[] = $item;
99
    }
100
101
    public function withCustomsDeclaration(CustomsDeclaration $declaration)
102
    {
103
        $this->declaration = $declaration;
104
105
        return $this;
106
    }
107
108
    public function withEcomData(EcomData $data)
109
    {
110
        $this->ecomData = $data;
111
112
        return $this;
113
    }
114
115
    public function withFiscalData(FiscalData $data)
116
    {
117
        $this->fiscalData = $data;
118
119
        return $this;
120
    }
121
122
    public function withInshurance(int $value)
123
    {
124
        $this->data['insr-value'] = $value;
125
126
        return $this;
127
    }
128
129
    public function withDeclaredValue(int $value)
130
    {
131
        $this->data['payment'] = $value;
132
133
        return $this;
134
    }
135
136
    public function withoutMailRank(bool $value = true)
137
    {
138
        $this->data['wo-mail-rank'] = $value;
139
140
        return $this;
141
    }
142
143
    public function toArray(): array
144
    {
145
        $this->data['ecom-data'] = $this->ecomData ? $this->ecomData->toArray() : null;
146
        $this->data['fiscal-data'] = $this->fiscalData ? $this->fiscalData->toArray() : null;
147
        $this->data['custom-declaration'] = $this->declaration ? $this->declaration->toArray() : null;
148
149
        if (\count($this->items) > 0) {
150
            $this->data['goods'] = ['items'];
151
152
            foreach ($this->items as $item) {
153
                $this->data['goods']['items'][] = $item->toArray();
154
            }
155
        }
156
157
        $this->data = \array_merge(
158
            $this->data,
159
            $this->recipient->toArray(),
160
            \iterator_to_array($this->convertAddress($this->address))
161
        );
162
163
        return [$this->data];
164
    }
165
166
    private function convertAddress(Address $address): \Generator
167
    {
168
        foreach ($address->toArray() as $key => $value) {
169
            if ($key === 'index' && ! \preg_match(self::RUSSIAN_POSTAL_CODE, $value)) {
170
                yield 'str-index-to' => $value;
171
            } elseif ($key === 'mail-direct') {
172
                yield $key => $value;
173
            } else {
174
                yield "{$key}-to" => $value;
175
            }
176
        }
177
    }
178
}
179