PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
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 / includes / adapter / shipment-options-from-order-adapter.php

shipment-options-from-order-adapter.php in PostNL for WooCommerce 4.0.0, at includes/adapter/shipment-options-from-order-adapter.php

148 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
4 use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractShipmentOptionsAdapter;
5
6 class WCPN_ShipmentOptionsFromOrderAdapter extends AbstractShipmentOptionsAdapter
7 {
8 const DEFAULT_INSURANCE = 0;
9
10 /**
11 * WCPN_ShipmentOptionsFromOrderAdapter constructor.
12 *
13 * @param AbstractDeliveryOptionsAdapter|null $originAdapter
14 * @param array $inputData
15 */
16 public function __construct(?AbstractDeliveryOptionsAdapter $originAdapter, array $inputData)
17 {
18 $shipmentOptionsAdapter = $originAdapter ? $originAdapter->getShipmentOptions() : null;
19 $options = $inputData['shipment_options'] ?? $inputData;
20
21 $this->signature = $this->isSignatureFromOptions($options, $shipmentOptionsAdapter);
22 $this->only_recipient = $this->isOnlyRecipientFromOptions($options, $shipmentOptionsAdapter);
23 $this->return = $this->isReturnShipmentFromOptions($options, $shipmentOptionsAdapter);
24 $this->age_check = $this->isAgeCheckFromOptions($options, $shipmentOptionsAdapter);
25 $this->insurance = $this->isInsuranceFromOptions($options, $shipmentOptionsAdapter);
26 $this->label_description = $this->getLabelDescriptionFromOptions($options, $shipmentOptionsAdapter);
27 }
28
29 /**
30 * @param array $options
31 * @param AbstractShipmentOptionsAdapter|null $shipmentOptionsAdapter
32 *
33 * @return bool|null
34 */
35 private function isSignatureFromOptions(array $options, ?AbstractShipmentOptionsAdapter $shipmentOptionsAdapter): ?bool
36 {
37 if (key_exists('signature', $options)) {
38 return (bool) $options['signature'];
39 }
40
41 if ($shipmentOptionsAdapter) {
42 return $shipmentOptionsAdapter->hasSignature();
43 }
44
45 return null;
46 }
47
48 /**
49 * @param array $options
50 * @param AbstractShipmentOptionsAdapter|null $shipmentOptionsAdapter
51 *
52 * @return bool|null
53 */
54 private function isOnlyRecipientFromOptions(array $options, ?AbstractShipmentOptionsAdapter $shipmentOptionsAdapter): ?bool
55 {
56 if (key_exists('only_recipient', $options)) {
57 return (bool) $options['only_recipient'];
58 }
59 if ($shipmentOptionsAdapter) {
60 return $shipmentOptionsAdapter->hasOnlyRecipient();
61 }
62
63 return null;
64 }
65
66 /**
67 * @param array $options
68 * @param AbstractShipmentOptionsAdapter|null $shipmentOptionsAdapter
69 *
70 * @return bool|null
71 */
72 private function isReturnShipmentFromOptions(array $options, ?AbstractShipmentOptionsAdapter $shipmentOptionsAdapter): ?bool
73 {
74 if (key_exists('return_shipment', $options)) {
75 return (bool) $options['return_shipment'];
76 }
77
78 if ($shipmentOptionsAdapter) {
79 return $shipmentOptionsAdapter->isReturn();
80 }
81
82 return null;
83 }
84
85 /**
86 * @param array $options
87 * @param AbstractShipmentOptionsAdapter|null $shipmentOptionsAdapter
88 *
89 * @return bool|null
90 */
91 private function isAgeCheckFromOptions(array $options, ?AbstractShipmentOptionsAdapter $shipmentOptionsAdapter): ?bool
92 {
93 if (key_exists('age_check', $options)) {
94 return (bool) $options['age_check'];
95 }
96
97 if ($shipmentOptionsAdapter) {
98 return $shipmentOptionsAdapter->hasAgeCheck();
99 }
100
101 return null;
102 }
103
104 /**
105 * @param array $options
106 * @param AbstractShipmentOptionsAdapter|null $shipmentOptionsAdapter
107 *
108 * @return int|null
109 */
110 private function isInsuranceFromOptions(array $options, ?AbstractShipmentOptionsAdapter $shipmentOptionsAdapter): ?int
111 {
112 if (key_exists('insured', $options)) {
113 if ($options['insured']) {
114 return (int) $options['insured_amount'];
115 }
116
117 return self::DEFAULT_INSURANCE;
118 }
119
120 if ($shipmentOptionsAdapter) {
121 return $shipmentOptionsAdapter->getInsurance();
122 }
123
124 return null;
125 }
126
127 /**
128 * @param array $options
129 * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractShipmentOptionsAdapter|null $shipmentOptionsAdapter
130 *
131 * @return string|null
132 */
133 private function getLabelDescriptionFromOptions(
134 array $options,
135 ?AbstractShipmentOptionsAdapter $shipmentOptionsAdapter
136 ): ?string {
137 if (key_exists('label_description', $options)) {
138 return $options['label_description'];
139 }
140
141 if ($shipmentOptionsAdapter) {
142 return $shipmentOptionsAdapter->getLabelDescription();
143 }
144
145 return null;
146 }
147 }
148