Passed
Push — master ( 8ecccf...82dcd6 )
by Jhao
02:33
created

OrderRequest::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
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
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 9
ccs 0
cts 9
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\DataAware;
8
use Appwilio\RussianPostSDK\Dispatching\Entities\Address;
9
use Appwilio\RussianPostSDK\Dispatching\Contracts\Arrayable;
10
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Order;
11
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\OrderItem;
12
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Recipient;
13
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\CustomsDeclaration;
14
15
final class OrderRequest implements Arrayable
16
{
17
    use DataAware;
18
19
    private const RUSSIAN_POSTAL_CODE = '~\d{6}~';
20
21
    /** @var Address */
22
    private $address;
23
24
    /** @var OrderItem[] */
25
    private $items = [];
26
27
    /** @var CustomsDeclaration|null */
28
    private $declaration;
29
30
    /** @var Recipient */
31
    private $recipient;
32
33
    public static function create(
34
        string $id,
35
        string $category,
36
        string $type,
37
        int $weight,
38
        Address $address,
39
        Recipient $recipient
40
    ) {
41
        return new self(...\func_get_args());
42
    }
43
44
    public static function fromOrder(Order $order)
0 ignored issues
show
Unused Code introduced by
The parameter $order is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

44
    public static function fromOrder(/** @scrutinizer ignore-unused */ Order $order)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        
47
    }
48
49
    public function __construct(
50
        string $number,
51
        string $mailType,
52
        string $mailCategory,
53
        int $weight,
54
        Address $address,
55
        Recipient $recipient
56
    ) {
57
        $this->data = [
58
            'mass'          => $weight,
59
            'order-num'     => $number,
60
            'mail-type'     => $mailType,
61
            'mail-category' => $mailCategory,
62
        ];
63
64
        $this->address = $address;
65
        $this->recipient = $recipient;
66
    }
67
68
    public function dimensions(int $height, int $width, int $length)
69
    {
70
        $this->data['dimension'] = \compact('height', 'width', 'length');
71
72
        return $this;
73
    }
74
75
    public function fragile(bool $value = true)
76
    {
77
        $this->data['fragile'] = $value;
78
79
        return $this;
80
    }
81
82
    public function viaCourier(bool $value = true)
83
    {
84
        $this->data['courier'] = $value;
85
86
        return $this;
87
    }
88
89
    public function addItem(OrderItem $item): void
90
    {
91
        $this->items[] = $item;
92
    }
93
94
    public function withCustomsDeclaration(CustomsDeclaration $declaration)
95
    {
96
        $this->declaration = $declaration;
97
98
        return $this;
99
    }
100
101
    public function withInshurance(int $value)
102
    {
103
        $this->data['insr-value'] = $value;
104
105
        return $this;
106
    }
107
108
    public function withInventory(bool $value)
109
    {
110
        $this->data['inventory'] = $value;
111
112
        return $this;
113
    }
114
115
    public function withDeclaredValue(int $value)
116
    {
117
        $this->data['payment'] = $value;
118
119
        return $this;
120
    }
121
122
    public function withCompletenessChecking(bool $value = true)
123
    {
124
        $this->data['completeness-checking'] = $value;
125
126
        return $this;
127
    }
128
129
    public function withVsd(bool $value = true)
130
    {
131
        $this->data['vsd'] = $value;
132
133
        return $this;
134
    }
135
136
    public function withElectronicNotice(bool $value = true)
137
    {
138
        $this->data['with-electronic-notice'] = $value;
139
140
        return $this;
141
    }
142
143
    public function withSimpleNotice(bool $value = true)
144
    {
145
        $this->data['with-simple-notice'] = $value;
146
147
        return $this;
148
    }
149
150
    public function withRegisteredNotice(bool $value = true)
151
    {
152
        $this->data['with-order-of-notice'] = $value;
153
154
        return $this;
155
    }
156
157
    public function withoutMailRank(bool $value = true)
158
    {
159
        $this->data['wo-mail-rank'] = $value;
160
161
        return $this;
162
    }
163
164
    public function toArray(): array
165
    {
166
        $this->data['custom-declaration'] = $this->declaration ? $this->declaration->toArray() : null;
167
168
        if (\count($this->items) > 0) {
169
            $this->data['goods'] = ['items'];
170
171
            foreach ($this->items as $item) {
172
                $this->data['goods']['items'][] = $item->toArray();
173
            }
174
        }
175
176
        $this->data = \array_merge($this->data, $this->recipient);
0 ignored issues
show
Bug introduced by
$this->recipient of type Appwilio\RussianPostSDK\...rders\Entites\Recipient is incompatible with the type array|null expected by parameter $array2 of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
        $this->data = \array_merge($this->data, /** @scrutinizer ignore-type */ $this->recipient);
Loading history...
177
        $this->data = \array_merge($this->data, \iterator_to_array($this->convertAddress($this->address)));
178
179
        return [$this->data];
180
    }
181
182
    private function convertAddress(Address $address): \Generator
183
    {
184
        foreach ($address->toArray() as $key => $value) {
185
            if ($key === 'index' && !\preg_match(self::RUSSIAN_POSTAL_CODE, $value)) {
186
                yield 'str-index-to' => $value;
187
            } else if ($key === 'mail-direct') {
188
                yield $key => $value;
189
            } else {
190
                yield "{$key}-to" => $value;
191
            }
192
        }
193
    }
194
}
195