PluginProbe
Packeta / 2.1
Packeta v2.1
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 +67 -19 trunk2.1 View file →
@@ -1,5 +1,10 @@
1 1 <?php
2 +/**
3 + * Class WidgetOptionsBuilder
4 + *
5 + * @package Packetery
6 + */
2 7
3 8 declare( strict_types=1 );
4 9
5 10 namespace Packetery\Module;
@@ -6,11 +11,16 @@
6 11
7 12 use Packetery\Core\Entity;
8 13 use Packetery\Core\Entity\Order;
9 14 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
10 -use Packetery\Module\Exception\EmptyVendorsException;
11 15 use Packetery\Module\Framework\WpAdapter;
16 +use Packetery\Module\Options\FlagManager\FeatureFlagProvider;
12 17
18 +/**
19 + * Class WidgetOptionsBuilder
20 + *
21 + * @package Packetery
22 + */
13 23 class WidgetOptionsBuilder {
14 24
15 25 /**
16 26 * Internal pickup points config.
@@ -19,8 +29,15 @@
19 29 */
20 30 private $pickupPointsConfig;
21 31
22 32 /**
33 + * Feature flag.
34 + *
35 + * @var FeatureFlagProvider
36 + */
37 + private $featureFlagProvider;
38 +
39 + /**
23 40 * @var WpAdapter
24 41 */
25 42 private $wpAdapter;
26 43
@@ -25,12 +42,14 @@
25 42 private $wpAdapter;
26 43
27 44 public function __construct(
28 45 PacketaPickupPointsConfig $pickupPointsConfig,
46 + FeatureFlagProvider $featureFlagProvider,
29 47 WpAdapter $wpAdapter
30 48 ) {
31 - $this->pickupPointsConfig = $pickupPointsConfig;
32 - $this->wpAdapter = $wpAdapter;
49 + $this->pickupPointsConfig = $pickupPointsConfig;
50 + $this->featureFlagProvider = $featureFlagProvider;
51 + $this->wpAdapter = $wpAdapter;
33 52 }
34 53
35 54 /**
36 55 * Gets widget vendors param.
@@ -39,9 +58,8 @@
39 58 * @param string $country Country.
40 59 * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings.
41 60 *
42 61 * @return array
43 - * @throws EmptyVendorsException
44 62 */
45 63 private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array {
46 64 if ( is_numeric( $carrierId ) ) {
47 65 return [
@@ -51,11 +69,17 @@
51 69 ],
52 70 ];
53 71 }
54 72
55 - $vendorGroups = $this->pickupPointsConfig->getFinalVendorGroups( $vendorGroups, $carrierId );
56 - if ( $vendorGroups === null ) {
57 - throw new EmptyVendorsException( 'Empty vendor groups for internal carrier.' );
73 + if ( ! isset( $vendorGroups ) || count( $vendorGroups ) === 0 ) {
74 + if ( $this->pickupPointsConfig->isCompoundCarrierId( $carrierId ) ) {
75 + $vendorGroups = $this->pickupPointsConfig->getCompoundCarrierVendorGroups( $carrierId );
76 + } else {
77 + $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers();
78 + if ( isset( $vendorCarriers[ $carrierId ] ) ) {
79 + $vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ];
80 + }
81 + }
58 82 }
59 83
60 84 $vendorsParam = [];
61 85 foreach ( $vendorGroups as $code ) {
@@ -72,8 +96,24 @@
72 96 return $vendorsParam;
73 97 }
74 98
75 99 /**
100 + * Gets widget carriers param.
101 + *
102 + * @param bool $isPickupPoints Is context pickup point related.
103 + * @param string $carrierId Carrier id.
104 + *
105 + * @return string|null
106 + */
107 + private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string {
108 + if ( $isPickupPoints ) {
109 + return ( is_numeric( $carrierId ) ? $carrierId : Entity\Carrier::INTERNAL_PICKUP_POINTS_ID );
110 + }
111 +
112 + return null;
113 + }
114 +
115 + /**
76 116 * Gets carrier configuration for widgets in frontend.
77 117 *
78 118 * @param Entity\Carrier $carrier Carrier configuration.
79 119 * @param string $optionId Option id.
@@ -87,13 +127,17 @@
87 127 ];
88 128
89 129 $carrierOption = $this->wpAdapter->getOption( $optionId );
90 130 if ( $carrier->hasPickupPoints() ) {
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 - );
131 + if ( $this->featureFlagProvider->isSplitActive() ) {
132 + $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam(
133 + $carrier->getId(),
134 + $carrier->getCountry(),
135 + ( ( ( $carrierOption !== null && $carrierOption !== false ) && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null )
136 + );
137 + } else {
138 + $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() );
139 + }
96 140 }
97 141
98 142 if ( ! $carrier->hasPickupPoints() ) {
99 143 $addressValidation = 'none';
@@ -121,15 +165,19 @@
121 165 'appIdentity' => Plugin::getAppIdentity(),
122 166 'weight' => $order->getFinalWeight(),
123 167 ];
124 168
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 - );
169 + if ( $this->featureFlagProvider->isSplitActive() ) {
170 + // In backend, we want all pickup points in that country for Packeta carrier.
171 + if ( $order->getCarrier()->getId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
172 + $widgetOptions['vendors'] = $this->getWidgetVendorsParam(
173 + $order->getCarrier()->getId(),
174 + $order->getShippingCountry(),
175 + null
176 + );
177 + }
178 + } else {
179 + $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrier()->getId() );
132 180 }
133 181
134 182 if ( $order->containsAdultContent() === true ) {
135 183 $widgetOptions += [ 'livePickupPoint' => true ];