PluginProbe
PostNL for WooCommerce / 4.4.1
PostNL for WooCommerce v4.4.1
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / vendor / myparcelnl / sdk / src / Adapter / ConsignmentAdapter.php

ConsignmentAdapter.php in PostNL for WooCommerce 4.4.1, at vendor/myparcelnl/sdk/src/Adapter/ConsignmentAdapter.php

236 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2 /**
3 * If you want to add improvements, please create a fork in our GitHub:
4 * https://github.com/myparcelnl
5 *
6 * @author Reindert Vetter <[email protected]>
7 * @copyright 2010-2020 MyParcel
8 * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
9 * @link https://github.com/myparcelnl/sdk
10 * @since File available since Release v2.0.0
11 */
12
13 namespace MyParcelNL\Sdk\src\Adapter;
14
15 use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
16 use MyParcelNL\Sdk\src\Support\Arr;
17
18 class ConsignmentAdapter
19 {
20 private $data;
21
22 /**
23 * @var AbstractConsignment
24 */
25 private $consignment;
26
27 /**
28 * ConsignmentDecode constructor.
29 *
30 * @param array $data
31 * @param \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment $consignment
32 *
33 * @throws \Exception
34 */
35 public function __construct(array $data, AbstractConsignment $consignment)
36 {
37 $this->data = $data;
38 $this->consignment = $consignment;
39
40 $this
41 ->baseOptions()
42 ->extraOptions()
43 ->recipient()
44 ->pickup();
45 }
46
47 /**
48 * @return AbstractConsignment
49 */
50 public function getConsignment()
51 {
52 return $this->consignment;
53 }
54
55 /**
56 * @return $this
57 */
58 private function baseOptions()
59 {
60 $recipient = $this->data['recipient'];
61 $options = $this->data['options'];
62
63 /** @noinspection PhpInternalEntityUsedInspection */
64 $this->consignment
65 ->setConsignmentId($this->data['id'])
66 ->setShopId($this->data['shop_id'])
67 ->setReferenceId($this->data['reference_identifier'])
68 ->setBarcode($this->data['barcode'])
69 ->setStatus($this->data['status'])
70 ->setCountry($recipient['cc'])
71 ->setPerson($recipient['person'])
72 ->setPostalCode($recipient['postal_code'])
73 ->setStreet($recipient['street'])
74 ->setCity($recipient['city'])
75 ->setEmail(isset($recipient['email']) ? $recipient['email'] : '')
76 ->setPhone(isset($recipient['phone']) ? $recipient['phone'] : '')
77 ->setPackageType($options['package_type'])
78 ->setLabelDescription(isset($options['label_description']) ? $options['label_description'] : '');
79
80 if (Arr::get($this->data, 'physical_properties.weight')) {
81 $this->consignment->setTotalWeight($this->data['physical_properties']['weight']);
82 }
83
84 return $this;
85 }
86
87 /**
88 * @return $this
89 * @throws \Exception
90 */
91 private function extraOptions()
92 {
93 $options = $this->data['options'];
94 $fields = [
95 'only_recipient' => false,
96 'large_format' => false,
97 'age_check' => false,
98 'signature' => false,
99 'return' => false,
100 'delivery_date' => null,
101 'delivery_type' => AbstractConsignment::DEFAULT_DELIVERY_TYPE,
102 ];
103 /** @noinspection PhpInternalEntityUsedInspection */
104 $this->clearFields($fields);
105
106 if (! empty($options['only_recipient'])) {
107 $this->consignment->setOnlyRecipient((bool) $options['only_recipient']);
108 }
109
110 if (! empty($options['large_format'])) {
111 $this->consignment->setLargeFormat((bool) $options['large_format']);
112 }
113
114 if (! empty($options['age_check'])) {
115 $this->consignment->setAgeCheck((bool) $options['age_check']);
116 }
117
118 if (! empty($options['signature'])) {
119 $this->consignment->setSignature((bool) $options['signature']);
120 }
121
122 if (! empty($options['return'])) {
123 $this->consignment->setReturn((bool) $options['return']);
124 }
125
126 if (! empty($options['delivery_date'])) {
127 $this->consignment->setDeliveryDate($options['delivery_date']);
128 }
129
130 if (key_exists('insurance', $options)) {
131 $insuranceAmount = $options['insurance']['amount'];
132 $this->consignment->setInsurance($insuranceAmount / 100);
133 }
134
135 if (isset($options['delivery_type'])) {
136 $this->consignment->setDeliveryType($options['delivery_type'], false);
137 }
138
139 return $this;
140 }
141
142 /**
143 * @return $this
144 */
145 private function recipient()
146 {
147 $fields = [
148 'company' => '',
149 'number' => null,
150 'number_suffix' => '',
151
152 ];
153 /** @noinspection PhpInternalEntityUsedInspection */
154 $this->clearFields($fields);
155
156 $methods = [
157 'Company' => 'company',
158 'Number' => 'number',
159 'NumberSuffix' => 'number_suffix',
160 ];
161 /** @noinspection PhpInternalEntityUsedInspection */
162 $this->setByMethods($this->data['recipient'], $methods);
163
164 return $this;
165 }
166
167 /**
168 * @return $this
169 */
170 private function pickup()
171 {
172 // Set pickup
173 if (key_exists('pickup', $this->data) && $this->data['pickup'] !== null) {
174
175 $methods = [
176 'PickupPostalCode' => 'postal_code',
177 'PickupStreet' => 'street',
178 'PickupCity' => 'city',
179 'PickupNumber' => 'number',
180 'PickupLocationName' => 'location_name',
181 'PickupLocationCode' => 'location_code',
182 'PickupNetworkId' => 'network_id',
183 ];
184 /** @noinspection PhpInternalEntityUsedInspection */
185 $this->setByMethods($this->data['pickup'], $methods);
186 } else {
187
188 $fields = [
189 'pickup_postal_code' => null,
190 'pickup_street' => null,
191 'pickup_city' => null,
192 'pickup_number' => null,
193 'pickup_location_name' => null,
194 'pickup_location_code' => '',
195 'retail_network_id' => '',
196
197 ];
198 /** @noinspection PhpInternalEntityUsedInspection */
199 $this->clearFields($fields);
200 }
201
202 return $this;
203 }
204
205 /**
206 * @param array $data
207 * @param array $methods
208 *
209 * @return $this
210 */
211 private function setByMethods($data, $methods)
212 {
213 foreach ($methods as $method => $value) {
214 if (! empty($data[$value])) {
215 $this->consignment->{'set' . $method}($data[$value]);
216 }
217 }
218
219 return $this;
220 }
221
222 /**
223 * @param $fields
224 *
225 * @return $this
226 */
227 private function clearFields($fields)
228 {
229 foreach ($fields as $field => $default) {
230 $this->consignment->{$field} = $default;
231 }
232
233 return $this;
234 }
235 }
236