Completed
Push — master ( 37d52e...ca4941 )
by Jhao
11:04
created

OrderRequest::toArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 4
nc 4
nop 0
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;
0 ignored issues
show
Bug introduced by
The type Appwilio\RussianPostSDK\...tching\Entities\Address was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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\CustomsDeclaration;
13
14
final class OrderRequest implements Arrayable
15
{
16
    use DataAware;
17
18
    private const RUSSIA_POSTAL_CODE = '~\d{6}~';
19
20
    /** @var Address */
21
    private $address;
22
23
    /** @var OrderItem[] */
24
    private $items = [];
25
26
    /** @var CustomsDeclaration|null */
27
    private $declaration;
28
29
    public static function create(string $id, string $category, string $type, int $weight, Address $address)
30
    {
31
        return new self(...\func_get_args());
32
    }
33
34
    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

34
    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...
35
    {
36
        
37
    }
38
39
    public function __construct(string $id, string $category, string $type, int $weight, Address $address)
40
    {
41
        $this->data['order-num'] = $id;
42
        $this->data['mail-type'] = $type;
43
        $this->data['mail-category'] = $category;
44
        $this->data['mass'] = $weight;
45
46
        $this->address = $address;
47
    }
48
49
    public function dimensions(int $height, int $width, int $length)
50
    {
51
        $this->data['dimension'] = \compact('height', 'width', 'length');
52
53
        return $this;
54
    }
55
56
    public function fragile(bool $value = true)
57
    {
58
        $this->data['fragile'] = $value;
59
60
        return $this;
61
    }
62
63
    public function viaCourier(bool $value = true)
64
    {
65
        $this->data['courier'] = $value;
66
67
        return $this;
68
    }
69
70
    public function addItem(OrderItem $item): void
71
    {
72
        $this->items[] = $item;
73
    }
74
75
    public function withCustomsDeclaration(CustomsDeclaration $declaration)
76
    {
77
        $this->declaration = $declaration;
78
79
        return $this;
80
    }
81
82
    public function withInshurance(int $value)
83
    {
84
        $this->data['insr-value'] = $value;
85
86
        return $this;
87
    }
88
89
    public function withInventory(bool $value)
90
    {
91
        $this->data['inventory'] = $value;
92
93
        return $this;
94
    }
95
96
    public function withDeclaredValue(int $value)
97
    {
98
        $this->data['payment'] = $value;
99
100
        return $this;
101
    }
102
103
    public function withCompletenessChecking(bool $value = true)
104
    {
105
        $this->data['completeness-checking'] = $value;
106
107
        return $this;
108
    }
109
110
    public function toArray(): array
111
    {
112
        $this->data['custom-declaration'] = $this->declaration ? $this->declaration->toArray() : null;
113
114
        if (\count($this->items) > 0) {
115
            $this->data['goods'] = ['items'];
116
117
            foreach ($this->items as $item) {
118
                $this->data['goods']['items'][] = $item->toArray();
119
            }
120
        }
121
122
        $this->data['recipient-name'] = 'avg';
123
124
        $this->data = \array_merge($this->data, \iterator_to_array($this->convertAddress($this->address)));
125
126
        return [$this->data];
127
    }
128
129
    private function convertAddress(Address $address): \Generator
130
    {
131
        foreach ($address->toArray() as $key => $value) {
132
            if ($key === 'index' && ! \preg_match(self::RUSSIA_POSTAL_CODE, $value)) {
133
                yield 'str-index-to' => $value;
134
            } elseif ($key === 'mail-direct') {
135
                yield $key => $value;
136
            } else {
137
                yield "{$key}-to" => $value;
138
            }
139
        }
140
    }
141
}
142