Passed
Push — master ( 96300c...cedc1b )
by Jhao
02:30
created

OrderRequest::withoutMailRank()   A

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