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