PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / WidgetOptionsBuilder.php

WidgetOptionsBuilder.php in Packeta 2.0.9, at src/Packetery/Module/WidgetOptionsBuilder.php

226 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class WidgetOptionsBuilder
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module;
11
12 use Packetery\Core\Entity;
13 use Packetery\Core\Entity\Order;
14 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
15 use Packetery\Module\Options\FlagManager\FeatureFlagProvider;
16
17 /**
18 * Class WidgetOptionsBuilder
19 *
20 * @package Packetery
21 */
22 class WidgetOptionsBuilder {
23
24 /**
25 * Internal pickup points config.
26 *
27 * @var PacketaPickupPointsConfig
28 */
29 private $pickupPointsConfig;
30
31 /**
32 * Feature flag.
33 *
34 * @var FeatureFlagProvider
35 */
36 private $featureFlagProvider;
37
38 /**
39 * WidgetOptionsBuilder constructor.
40 *
41 * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
42 * @param FeatureFlagProvider $featureFlagProvider Feature flag.
43 */
44 public function __construct(
45 PacketaPickupPointsConfig $pickupPointsConfig,
46 FeatureFlagProvider $featureFlagProvider
47 ) {
48 $this->pickupPointsConfig = $pickupPointsConfig;
49 $this->featureFlagProvider = $featureFlagProvider;
50 }
51
52 /**
53 * Gets widget vendors param.
54 *
55 * @param string $carrierId Carrier id.
56 * @param string $country Country.
57 * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings.
58 *
59 * @return array
60 */
61 private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array {
62 if ( is_numeric( $carrierId ) ) {
63 return [
64 [
65 'carrierId' => $carrierId,
66 'selected' => true,
67 ],
68 ];
69 }
70
71 if ( ! isset( $vendorGroups ) || count( $vendorGroups ) === 0 ) {
72 if ( $this->pickupPointsConfig->isCompoundCarrierId( $carrierId ) ) {
73 $vendorGroups = $this->pickupPointsConfig->getCompoundCarrierVendorGroups( $carrierId );
74 } else {
75 $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers();
76 if ( isset( $vendorCarriers[ $carrierId ] ) ) {
77 $vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ];
78 }
79 }
80 }
81
82 $vendorsParam = [];
83 foreach ( $vendorGroups as $code ) {
84 $groupSettings = [
85 'selected' => true,
86 'country' => $country,
87 ];
88 if ( $code !== Entity\Carrier::VENDOR_GROUP_ZPOINT ) {
89 $groupSettings['group'] = $code;
90 }
91 $vendorsParam[] = $groupSettings;
92 }
93
94 return $vendorsParam;
95 }
96
97 /**
98 * Gets widget carriers param.
99 *
100 * @param bool $isPickupPoints Is context pickup point related.
101 * @param string $carrierId Carrier id.
102 *
103 * @return string|null
104 */
105 private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string {
106 if ( $isPickupPoints ) {
107 return ( is_numeric( $carrierId ) ? $carrierId : Entity\Carrier::INTERNAL_PICKUP_POINTS_ID );
108 }
109
110 return null;
111 }
112
113 /**
114 * Gets carrier configuration for widgets in frontend.
115 *
116 * @param Entity\Carrier $carrier Carrier configuration.
117 * @param string $optionId Option id.
118 *
119 * @return array
120 */
121 public function getCarrierForCheckout( Entity\Carrier $carrier, string $optionId ): array {
122 $carrierConfigForWidget = [
123 'id' => $carrier->getId(),
124 'is_pickup_points' => (int) $carrier->hasPickupPoints(),
125 ];
126
127 $carrierOption = get_option( $optionId );
128 if ( $carrier->hasPickupPoints() ) {
129 if ( $this->featureFlagProvider->isSplitActive() ) {
130 $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam(
131 $carrier->getId(),
132 $carrier->getCountry(),
133 ( ( ( $carrierOption !== null && $carrierOption !== false ) && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null )
134 );
135 } else {
136 $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() );
137 }
138 }
139
140 if ( ! $carrier->hasPickupPoints() ) {
141 $addressValidation = 'none';
142 if ( ( $carrierOption !== null && $carrierOption !== false ) && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) {
143 $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation );
144 }
145
146 $carrierConfigForWidget['address_validation'] = $addressValidation;
147 }
148
149 return $carrierConfigForWidget;
150 }
151
152 /**
153 * Creates pickup point widget options in backend.
154 *
155 * @param Order $order Order.
156 *
157 * @return array
158 */
159 public function createPickupPointForAdmin( Order $order ): array {
160 $widgetOptions = [
161 'country' => $order->getShippingCountry(),
162 'language' => substr( get_user_locale(), 0, 2 ),
163 'appIdentity' => Plugin::getAppIdentity(),
164 'weight' => $order->getFinalWeight(),
165 ];
166
167 if ( $this->featureFlagProvider->isSplitActive() ) {
168 // In backend, we want all pickup points in that country for Packeta carrier.
169 if ( $order->getCarrier()->getId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
170 $widgetOptions['vendors'] = $this->getWidgetVendorsParam(
171 $order->getCarrier()->getId(),
172 $order->getShippingCountry(),
173 null
174 );
175 }
176 } else {
177 $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrier()->getId() );
178 }
179
180 if ( $order->containsAdultContent() === true ) {
181 $widgetOptions += [ 'livePickupPoint' => true ];
182 }
183
184 return $widgetOptions;
185 }
186
187 /**
188 * Creates address validation widget options in backend.
189 *
190 * @param Order $order Order.
191 *
192 * @return array
193 */
194 public function createAddressForAdmin( Order $order ): array {
195 /**
196 * Delivery address is always present in this case.
197 *
198 * @var Entity\Address $deliveryAddress
199 */
200 $deliveryAddress = $order->getDeliveryAddress();
201 $widgetOptions = [
202 'country' => $order->getShippingCountry(),
203 'language' => substr( get_user_locale(), 0, 2 ),
204 'layout' => 'hd',
205 'appIdentity' => Plugin::getAppIdentity(),
206 'street' => $deliveryAddress->getStreet(),
207 'city' => $deliveryAddress->getCity(),
208 'postcode' => $deliveryAddress->getZip(),
209 ];
210
211 if ( $deliveryAddress->getHouseNumber() !== null ) {
212 $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ];
213 }
214
215 if ( $deliveryAddress->getCounty() !== null ) {
216 $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ];
217 }
218
219 if ( is_numeric( $order->getCarrier()->getId() ) ) {
220 $widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ];
221 }
222
223 return $widgetOptions;
224 }
225 }
226