| @@ -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,15 @@ | ||
| 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 | -use Packetery\Module\Framework\WpAdapter; | |
| 15 | +use Packetery\Module\Options\FlagManager\FeatureFlagProvider; | |
| 12 | 16 | |
| 17 | +/** | |
| 18 | + * Class WidgetOptionsBuilder | |
| 19 | + * | |
| 20 | + * @package Packetery | |
| 21 | + */ | |
| 13 | 22 | class WidgetOptionsBuilder { |
| 14 | 23 | |
| 15 | 24 | /** |
| 16 | 25 | * Internal pickup points config. |
| @@ -19,18 +28,26 @@ | ||
| 19 | 28 | */ |
| 20 | 29 | private $pickupPointsConfig; |
| 21 | 30 | |
| 22 | 31 | /** |
| 23 | - * @var WpAdapter | |
| 32 | + * Feature flag. | |
| 33 | + * | |
| 34 | + * @var FeatureFlagProvider | |
| 24 | 35 | */ |
| 25 | - private $wpAdapter; | |
| 36 | + private $featureFlagProvider; | |
| 26 | 37 | |
| 38 | + /** | |
| 39 | + * WidgetOptionsBuilder constructor. | |
| 40 | + * | |
| 41 | + * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config. | |
| 42 | + * @param FeatureFlagProvider $featureFlagProvider Feature flag. | |
| 43 | + */ | |
| 27 | 44 | public function __construct( |
| 28 | 45 | PacketaPickupPointsConfig $pickupPointsConfig, |
| 29 | - WpAdapter $wpAdapter | |
| 46 | + FeatureFlagProvider $featureFlagProvider | |
| 30 | 47 | ) { |
| 31 | - $this->pickupPointsConfig = $pickupPointsConfig; | |
| 32 | - $this->wpAdapter = $wpAdapter; | |
| 48 | + $this->pickupPointsConfig = $pickupPointsConfig; | |
| 49 | + $this->featureFlagProvider = $featureFlagProvider; | |
| 33 | 50 | } |
| 34 | 51 | |
| 35 | 52 | /** |
| 36 | 53 | * Gets widget vendors param. |
| @@ -39,9 +56,8 @@ | ||
| 39 | 56 | * @param string $country Country. |
| 40 | 57 | * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings. |
| 41 | 58 | * |
| 42 | 59 | * @return array |
| 43 | - * @throws EmptyVendorsException | |
| 44 | 60 | */ |
| 45 | 61 | private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array { |
| 46 | 62 | if ( is_numeric( $carrierId ) ) { |
| 47 | 63 | return [ |
| @@ -51,11 +67,17 @@ | ||
| 51 | 67 | ], |
| 52 | 68 | ]; |
| 53 | 69 | } |
| 54 | 70 | |
| 55 | - $vendorGroups = $this->pickupPointsConfig->getFinalVendorGroups( $vendorGroups, $carrierId ); | |
| 56 | - if ( $vendorGroups === null ) { | |
| 57 | - throw new EmptyVendorsException( 'Empty vendor groups for internal carrier.' ); | |
| 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 | + } | |
| 58 | 80 | } |
| 59 | 81 | |
| 60 | 82 | $vendorsParam = []; |
| 61 | 83 | foreach ( $vendorGroups as $code ) { |
| @@ -72,8 +94,24 @@ | ||
| 72 | 94 | return $vendorsParam; |
| 73 | 95 | } |
| 74 | 96 | |
| 75 | 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 | + /** | |
| 76 | 114 | * Gets carrier configuration for widgets in frontend. |
| 77 | 115 | * |
| 78 | 116 | * @param Entity\Carrier $carrier Carrier configuration. |
| 79 | 117 | * @param string $optionId Option id. |
| @@ -85,15 +123,19 @@ | ||
| 85 | 123 | 'id' => $carrier->getId(), |
| 86 | 124 | 'is_pickup_points' => (int) $carrier->hasPickupPoints(), |
| 87 | 125 | ]; |
| 88 | 126 | |
| 89 | - $carrierOption = $this->wpAdapter->getOption( $optionId ); | |
| 127 | + $carrierOption = get_option( $optionId ); | |
| 90 | 128 | 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 | - ); | |
| 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 | + } | |
| 96 | 138 | } |
| 97 | 139 | |
| 98 | 140 | if ( ! $carrier->hasPickupPoints() ) { |
| 99 | 141 | $addressValidation = 'none'; |
| @@ -116,20 +158,24 @@ | ||
| 116 | 158 | */ |
| 117 | 159 | public function createPickupPointForAdmin( Order $order ): array { |
| 118 | 160 | $widgetOptions = [ |
| 119 | 161 | 'country' => $order->getShippingCountry(), |
| 120 | - 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 162 | + 'language' => substr( get_user_locale(), 0, 2 ), | |
| 121 | 163 | 'appIdentity' => Plugin::getAppIdentity(), |
| 122 | 164 | 'weight' => $order->getFinalWeight(), |
| 123 | 165 | ]; |
| 124 | 166 | |
| 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 | - ); | |
| 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() ); | |
| 132 | 178 | } |
| 133 | 179 | |
| 134 | 180 | if ( $order->containsAdultContent() === true ) { |
| 135 | 181 | $widgetOptions += [ 'livePickupPoint' => true ]; |
| @@ -153,9 +199,9 @@ | ||
| 153 | 199 | */ |
| 154 | 200 | $deliveryAddress = $order->getDeliveryAddress(); |
| 155 | 201 | $widgetOptions = [ |
| 156 | 202 | 'country' => $order->getShippingCountry(), |
| 157 | - 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 203 | + 'language' => substr( get_user_locale(), 0, 2 ), | |
| 158 | 204 | 'layout' => 'hd', |
| 159 | 205 | 'appIdentity' => Plugin::getAppIdentity(), |
| 160 | 206 | 'street' => $deliveryAddress->getStreet(), |
| 161 | 207 | 'city' => $deliveryAddress->getCity(), |