PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
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
← All changes | src/Packetery/Module/WidgetOptionsBuilder.php +39 -140 1.5.22.3.2 View file →
@@ -1,10 +1,5 @@
1 1 <?php
2 -/**
3 - * Class WidgetOptionsBuilder
4 - *
5 - * @package Packetery
6 - */
7 2
8 3 declare( strict_types=1 );
9 4
10 5 namespace Packetery\Module;
@@ -11,17 +6,11 @@
11 6
12 7 use Packetery\Core\Entity;
13 8 use Packetery\Core\Entity\Order;
14 9 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
15 -use Packetery\Module\Options\FeatureFlagManager;
16 -use Packetery\Module\Order\Repository;
17 -use WC_Order;
10 +use Packetery\Module\Exception\EmptyVendorsException;
11 +use Packetery\Module\Framework\WpAdapter;
18 12
19 -/**
20 - * Class WidgetOptionsBuilder
21 - *
22 - * @package Packetery
23 - */
24 13 class WidgetOptionsBuilder {
25 14
26 15 /**
27 16 * Internal pickup points config.
@@ -30,46 +19,18 @@
30 19 */
31 20 private $pickupPointsConfig;
32 21
33 22 /**
34 - * RateCalculator.
35 - *
36 - * @var RateCalculator
23 + * @var WpAdapter
37 24 */
38 - private $rateCalculator;
25 + private $wpAdapter;
39 26
40 - /**
41 - * Order repository.
42 - *
43 - * @var Repository
44 - */
45 - private $orderRepository;
46 -
47 - /**
48 - * Feature flag.
49 - *
50 - * @var FeatureFlagManager
51 - */
52 - private $featureFlag;
53 -
54 - /**
55 - * WidgetOptionsBuilder constructor.
56 - *
57 - * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
58 - * @param RateCalculator $rateCalculator RateCalculator.
59 - * @param Repository $orderRepository Order repository.
60 - * @param FeatureFlagManager $featureFlag Feature flag.
61 - */
62 27 public function __construct(
63 28 PacketaPickupPointsConfig $pickupPointsConfig,
64 - RateCalculator $rateCalculator,
65 - Repository $orderRepository,
66 - FeatureFlagManager $featureFlag
29 + WpAdapter $wpAdapter
67 30 ) {
68 31 $this->pickupPointsConfig = $pickupPointsConfig;
69 - $this->rateCalculator = $rateCalculator;
70 - $this->orderRepository = $orderRepository;
71 - $this->featureFlag = $featureFlag;
32 + $this->wpAdapter = $wpAdapter;
72 33 }
73 34
74 35 /**
75 36 * Gets widget vendors param.
@@ -75,13 +36,14 @@
75 36 * Gets widget vendors param.
76 37 *
77 38 * @param string $carrierId Carrier id.
78 39 * @param string $country Country.
79 - * @param array|null $vendorGroups Vendor groups.
40 + * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings.
80 41 *
81 - * @return array|null
42 + * @return array
43 + * @throws EmptyVendorsException
82 44 */
83 - private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): ?array {
45 + private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array {
84 46 if ( is_numeric( $carrierId ) ) {
85 47 return [
86 48 [
87 49 'carrierId' => $carrierId,
@@ -89,17 +51,13 @@
89 51 ],
90 52 ];
91 53 }
92 54
93 - $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers();
94 - if ( ! empty( $vendorCarriers[ $carrierId ] ) ) {
95 - $vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ];
55 + $vendorGroups = $this->pickupPointsConfig->getFinalVendorGroups( $vendorGroups, $carrierId );
56 + if ( $vendorGroups === null ) {
57 + throw new EmptyVendorsException( 'Empty vendor groups for internal carrier.' );
96 58 }
97 59
98 - if ( empty( $vendorGroups ) ) {
99 - return null;
100 - }
101 -
102 60 $vendorsParam = [];
103 61 foreach ( $vendorGroups as $code ) {
104 62 $groupSettings = [
105 63 'selected' => true,
@@ -104,9 +62,9 @@
104 62 $groupSettings = [
105 63 'selected' => true,
106 64 'country' => $country,
107 65 ];
108 - if ( Entity\Carrier::VENDOR_GROUP_ZPOINT !== $code ) {
66 + if ( $code !== Entity\Carrier::VENDOR_GROUP_ZPOINT ) {
109 67 $groupSettings['group'] = $code;
110 68 }
111 69 $vendorsParam[] = $groupSettings;
112 70 }
@@ -114,56 +72,33 @@
114 72 return $vendorsParam;
115 73 }
116 74
117 75 /**
118 - * Gets widget carriers param.
119 - *
120 - * @param bool $isPickupPoints Is context pickup point related.
121 - * @param string $carrierId Carrier id.
122 - *
123 - * @return string|null
124 - */
125 - private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string {
126 - if ( $isPickupPoints ) {
127 - return ( is_numeric( $carrierId ) ? $carrierId : Carrier\Repository::INTERNAL_PICKUP_POINTS_ID );
128 - }
129 -
130 - return null;
131 - }
132 -
133 - /**
134 76 * Gets carrier configuration for widgets in frontend.
135 77 *
136 78 * @param Entity\Carrier $carrier Carrier configuration.
137 - * @param float|null $defaultPrice Default price.
138 79 * @param string $optionId Option id.
139 80 *
140 81 * @return array
141 82 */
142 - public function getCarrierForCheckout( Entity\Carrier $carrier, ?float $defaultPrice, string $optionId ): array {
83 + public function getCarrierForCheckout( Entity\Carrier $carrier, string $optionId ): array {
143 84 $carrierConfigForWidget = [
144 85 'id' => $carrier->getId(),
145 86 'is_pickup_points' => (int) $carrier->hasPickupPoints(),
146 - 'defaultPrice' => $defaultPrice,
147 - 'defaultCurrency' => get_woocommerce_currency(),
148 87 ];
149 88
150 - $carrierOption = get_option( $optionId );
89 + $carrierOption = $this->wpAdapter->getOption( $optionId );
151 90 if ( $carrier->hasPickupPoints() ) {
152 - if ( $this->featureFlag->isSplitActive() ) {
153 - $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam(
154 - (string) $carrier->getId(),
155 - $carrier->getCountry(),
156 - ( ( $carrierOption && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null )
157 - );
158 - } else {
159 - $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() );
160 - }
91 + $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam(
92 + $carrier->getId(),
93 + $carrier->getCountry(),
94 + ( ( ( $carrierOption !== null && $carrierOption !== false ) && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null )
95 + );
161 96 }
162 97
163 98 if ( ! $carrier->hasPickupPoints() ) {
164 99 $addressValidation = 'none';
165 - if ( $carrierOption && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) {
100 + if ( ( $carrierOption !== null && $carrierOption !== false ) && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) {
166 101 $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation );
167 102 }
168 103
169 104 $carrierConfigForWidget['address_validation'] = $addressValidation;
@@ -176,62 +111,28 @@
176 111 * Creates pickup point widget options in backend.
177 112 *
178 113 * @param Order $order Order.
179 114 *
180 - * @return array|null
115 + * @return array
181 116 */
182 117 public function createPickupPointForAdmin( Order $order ): array {
183 - if ( $order->isExternalCarrier() ) {
184 - $carrierId = $order->getCarrierId();
185 - } else {
186 - $carrierId = $this->pickupPointsConfig->getCompoundCarrierIdByCountry( $order->getShippingCountry() );
187 - }
188 -
189 - $defaultPrice = null;
190 - if ( null !== $carrierId ) {
191 - /**
192 - * Cannot be null due to the conditions in the caller method.
193 - *
194 - * @var WC_Order $wcOrder
195 - */
196 - $wcOrder = $this->orderRepository->getWcOrderById( (int) $order->getNumber() );
197 - $defaultPrice = $this->rateCalculator->getShippingRateCost(
198 - Carrier\Options::createByCarrierId( $carrierId ),
199 - $wcOrder->get_total() - $wcOrder->get_shipping_total(),
200 - $order->getWeight(),
201 - $this->rateCalculator->isFreeShippingCouponApplied( $wcOrder )
202 - );
203 - }
204 -
205 118 $widgetOptions = [
206 - 'country' => $order->getShippingCountry(),
207 - 'language' => substr( get_user_locale(), 0, 2 ),
208 - 'appIdentity' => Plugin::getAppIdentity(),
209 - 'weight' => $order->getFinalWeight(),
210 - 'defaultPrice' => $defaultPrice,
211 - 'defaultCurrency' => $order->getCurrency(),
119 + 'country' => $order->getShippingCountry(),
120 + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ),
121 + 'appIdentity' => Plugin::getAppIdentity(),
122 + 'weight' => $order->getFinalWeight(),
212 123 ];
213 124
214 - // TODO: update later when carrier will not be nullable.
215 - $orderCarrier = $order->getCarrier();
216 - if ( $orderCarrier ) {
217 - $widgetOptions['defaultCurrency'] = $orderCarrier->getCurrency();
125 + // In backend, we want all pickup points in that country for Packeta carrier.
126 + if ( $order->getCarrier()->getId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
127 + $widgetOptions['vendors'] = $this->getWidgetVendorsParam(
128 + $order->getCarrier()->getId(),
129 + $order->getShippingCountry(),
130 + null
131 + );
218 132 }
219 133
220 - if ( $this->featureFlag->isSplitActive() ) {
221 - // In backend, we want all pickup points in that country for packeta carrier.
222 - if ( $order->getCarrierId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
223 - $widgetOptions['vendors'] = $this->getWidgetVendorsParam(
224 - $order->getCarrierId(),
225 - $order->getShippingCountry(),
226 - null
227 - );
228 - }
229 - } else {
230 - $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrierId() );
231 - }
232 -
233 - if ( $order->containsAdultContent() ) {
134 + if ( $order->containsAdultContent() === true ) {
234 135 $widgetOptions += [ 'livePickupPoint' => true ];
235 136 }
236 137
237 138 return $widgetOptions;
@@ -252,9 +153,9 @@
252 153 */
253 154 $deliveryAddress = $order->getDeliveryAddress();
254 155 $widgetOptions = [
255 156 'country' => $order->getShippingCountry(),
256 - 'language' => substr( get_user_locale(), 0, 2 ),
157 + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ),
257 158 'layout' => 'hd',
258 159 'appIdentity' => Plugin::getAppIdentity(),
259 160 'street' => $deliveryAddress->getStreet(),
260 161 'city' => $deliveryAddress->getCity(),
@@ -260,21 +161,19 @@
260 161 'city' => $deliveryAddress->getCity(),
261 162 'postcode' => $deliveryAddress->getZip(),
262 163 ];
263 164
264 - if ( $deliveryAddress->getHouseNumber() ) {
165 + if ( $deliveryAddress->getHouseNumber() !== null ) {
265 166 $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ];
266 167 }
267 168
268 - if ( $deliveryAddress->getCounty() ) {
169 + if ( $deliveryAddress->getCounty() !== null ) {
269 170 $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ];
270 171 }
271 172
272 - // TODO: Redo in carrier refactor.
273 - if ( $order->getCarrier() && is_numeric( $order->getCarrier()->getId() ) ) {
173 + if ( is_numeric( $order->getCarrier()->getId() ) ) {
274 174 $widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ];
275 175 }
276 176
277 177 return $widgetOptions;
278 178 }
279 -
280 179 }