| @@ -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,15 +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; | |
| 10 | +use Packetery\Module\Exception\EmptyVendorsException; | |
| 11 | +use Packetery\Module\Framework\WpAdapter; | |
| 16 | 12 | |
| 17 | -/** | |
| 18 | - * Class WidgetOptionsBuilder | |
| 19 | - * | |
| 20 | - * @package Packetery | |
| 21 | - */ | |
| 22 | 13 | class WidgetOptionsBuilder { |
| 23 | 14 | |
| 24 | 15 | /** |
| 25 | 16 | * Internal pickup points config. |
| @@ -28,26 +19,18 @@ | ||
| 28 | 19 | */ |
| 29 | 20 | private $pickupPointsConfig; |
| 30 | 21 | |
| 31 | 22 | /** |
| 32 | - * Feature flag. | |
| 33 | - * | |
| 34 | - * @var FeatureFlagManager | |
| 23 | + * @var WpAdapter | |
| 35 | 24 | */ |
| 36 | - private $featureFlag; | |
| 25 | + private $wpAdapter; | |
| 37 | 26 | |
| 38 | - /** | |
| 39 | - * WidgetOptionsBuilder constructor. | |
| 40 | - * | |
| 41 | - * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config. | |
| 42 | - * @param FeatureFlagManager $featureFlag Feature flag. | |
| 43 | - */ | |
| 44 | 27 | public function __construct( |
| 45 | 28 | PacketaPickupPointsConfig $pickupPointsConfig, |
| 46 | - FeatureFlagManager $featureFlag | |
| 29 | + WpAdapter $wpAdapter | |
| 47 | 30 | ) { |
| 48 | 31 | $this->pickupPointsConfig = $pickupPointsConfig; |
| 49 | - $this->featureFlag = $featureFlag; | |
| 32 | + $this->wpAdapter = $wpAdapter; | |
| 50 | 33 | } |
| 51 | 34 | |
| 52 | 35 | /** |
| 53 | 36 | * Gets widget vendors param. |
| @@ -53,13 +36,14 @@ | ||
| 53 | 36 | * Gets widget vendors param. |
| 54 | 37 | * |
| 55 | 38 | * @param string $carrierId Carrier id. |
| 56 | 39 | * @param string $country Country. |
| 57 | - * @param array|null $vendorGroups Vendor groups. | |
| 40 | + * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings. | |
| 58 | 41 | * |
| 59 | - * @return array|null | |
| 42 | + * @return array | |
| 43 | + * @throws EmptyVendorsException | |
| 60 | 44 | */ |
| 61 | - private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): ?array { | |
| 45 | + private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array { | |
| 62 | 46 | if ( is_numeric( $carrierId ) ) { |
| 63 | 47 | return [ |
| 64 | 48 | [ |
| 65 | 49 | 'carrierId' => $carrierId, |
| @@ -67,17 +51,13 @@ | ||
| 67 | 51 | ], |
| 68 | 52 | ]; |
| 69 | 53 | } |
| 70 | 54 | |
| 71 | - $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers(); | |
| 72 | - if ( ! empty( $vendorCarriers[ $carrierId ] ) ) { | |
| 73 | - $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.' ); | |
| 74 | 58 | } |
| 75 | 59 | |
| 76 | - if ( empty( $vendorGroups ) ) { | |
| 77 | - return null; | |
| 78 | - } | |
| 79 | - | |
| 80 | 60 | $vendorsParam = []; |
| 81 | 61 | foreach ( $vendorGroups as $code ) { |
| 82 | 62 | $groupSettings = [ |
| 83 | 63 | 'selected' => true, |
| @@ -82,9 +62,9 @@ | ||
| 82 | 62 | $groupSettings = [ |
| 83 | 63 | 'selected' => true, |
| 84 | 64 | 'country' => $country, |
| 85 | 65 | ]; |
| 86 | - if ( Entity\Carrier::VENDOR_GROUP_ZPOINT !== $code ) { | |
| 66 | + if ( $code !== Entity\Carrier::VENDOR_GROUP_ZPOINT ) { | |
| 87 | 67 | $groupSettings['group'] = $code; |
| 88 | 68 | } |
| 89 | 69 | $vendorsParam[] = $groupSettings; |
| 90 | 70 | } |
| @@ -92,56 +72,33 @@ | ||
| 92 | 72 | return $vendorsParam; |
| 93 | 73 | } |
| 94 | 74 | |
| 95 | 75 | /** |
| 96 | - * Gets widget carriers param. | |
| 97 | - * | |
| 98 | - * @param bool $isPickupPoints Is context pickup point related. | |
| 99 | - * @param string $carrierId Carrier id. | |
| 100 | - * | |
| 101 | - * @return string|null | |
| 102 | - */ | |
| 103 | - private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string { | |
| 104 | - if ( $isPickupPoints ) { | |
| 105 | - return ( is_numeric( $carrierId ) ? $carrierId : Carrier\Repository::INTERNAL_PICKUP_POINTS_ID ); | |
| 106 | - } | |
| 107 | - | |
| 108 | - return null; | |
| 109 | - } | |
| 110 | - | |
| 111 | - /** | |
| 112 | 76 | * Gets carrier configuration for widgets in frontend. |
| 113 | 77 | * |
| 114 | 78 | * @param Entity\Carrier $carrier Carrier configuration. |
| 115 | - * @param float|null $defaultPrice Default price. | |
| 116 | 79 | * @param string $optionId Option id. |
| 117 | 80 | * |
| 118 | 81 | * @return array |
| 119 | 82 | */ |
| 120 | - public function getCarrierForCheckout( Entity\Carrier $carrier, ?float $defaultPrice, string $optionId ): array { | |
| 83 | + public function getCarrierForCheckout( Entity\Carrier $carrier, string $optionId ): array { | |
| 121 | 84 | $carrierConfigForWidget = [ |
| 122 | 85 | 'id' => $carrier->getId(), |
| 123 | 86 | 'is_pickup_points' => (int) $carrier->hasPickupPoints(), |
| 124 | - 'defaultPrice' => $defaultPrice, | |
| 125 | - 'defaultCurrency' => get_woocommerce_currency(), | |
| 126 | 87 | ]; |
| 127 | 88 | |
| 128 | - $carrierOption = get_option( $optionId ); | |
| 89 | + $carrierOption = $this->wpAdapter->getOption( $optionId ); | |
| 129 | 90 | if ( $carrier->hasPickupPoints() ) { |
| 130 | - if ( $this->featureFlag->isSplitActive() ) { | |
| 131 | - $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam( | |
| 132 | - $carrier->getId(), | |
| 133 | - $carrier->getCountry(), | |
| 134 | - ( ( $carrierOption && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null ) | |
| 135 | - ); | |
| 136 | - } else { | |
| 137 | - $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() ); | |
| 138 | - } | |
| 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 | + ); | |
| 139 | 96 | } |
| 140 | 97 | |
| 141 | 98 | if ( ! $carrier->hasPickupPoints() ) { |
| 142 | 99 | $addressValidation = 'none'; |
| 143 | - 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 ) ) { | |
| 144 | 101 | $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation ); |
| 145 | 102 | } |
| 146 | 103 | |
| 147 | 104 | $carrierConfigForWidget['address_validation'] = $addressValidation; |
| @@ -154,32 +111,28 @@ | ||
| 154 | 111 | * Creates pickup point widget options in backend. |
| 155 | 112 | * |
| 156 | 113 | * @param Order $order Order. |
| 157 | 114 | * |
| 158 | - * @return array|null | |
| 115 | + * @return array | |
| 159 | 116 | */ |
| 160 | 117 | public function createPickupPointForAdmin( Order $order ): array { |
| 161 | 118 | $widgetOptions = [ |
| 162 | 119 | 'country' => $order->getShippingCountry(), |
| 163 | - 'language' => substr( get_user_locale(), 0, 2 ), | |
| 120 | + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 164 | 121 | 'appIdentity' => Plugin::getAppIdentity(), |
| 165 | 122 | 'weight' => $order->getFinalWeight(), |
| 166 | 123 | ]; |
| 167 | 124 | |
| 168 | - if ( $this->featureFlag->isSplitActive() ) { | |
| 169 | - // In backend, we want all pickup points in that country for packeta carrier. | |
| 170 | - if ( $order->getCarrierId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) { | |
| 171 | - $widgetOptions['vendors'] = $this->getWidgetVendorsParam( | |
| 172 | - $order->getCarrierId(), | |
| 173 | - $order->getShippingCountry(), | |
| 174 | - null | |
| 175 | - ); | |
| 176 | - } | |
| 177 | - } else { | |
| 178 | - $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrierId() ); | |
| 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 | + ); | |
| 179 | 132 | } |
| 180 | 133 | |
| 181 | - if ( $order->containsAdultContent() ) { | |
| 134 | + if ( $order->containsAdultContent() === true ) { | |
| 182 | 135 | $widgetOptions += [ 'livePickupPoint' => true ]; |
| 183 | 136 | } |
| 184 | 137 | |
| 185 | 138 | return $widgetOptions; |
| @@ -200,9 +153,9 @@ | ||
| 200 | 153 | */ |
| 201 | 154 | $deliveryAddress = $order->getDeliveryAddress(); |
| 202 | 155 | $widgetOptions = [ |
| 203 | 156 | 'country' => $order->getShippingCountry(), |
| 204 | - 'language' => substr( get_user_locale(), 0, 2 ), | |
| 157 | + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 205 | 158 | 'layout' => 'hd', |
| 206 | 159 | 'appIdentity' => Plugin::getAppIdentity(), |
| 207 | 160 | 'street' => $deliveryAddress->getStreet(), |
| 208 | 161 | 'city' => $deliveryAddress->getCity(), |
| @@ -208,21 +161,19 @@ | ||
| 208 | 161 | 'city' => $deliveryAddress->getCity(), |
| 209 | 162 | 'postcode' => $deliveryAddress->getZip(), |
| 210 | 163 | ]; |
| 211 | 164 | |
| 212 | - if ( $deliveryAddress->getHouseNumber() ) { | |
| 165 | + if ( $deliveryAddress->getHouseNumber() !== null ) { | |
| 213 | 166 | $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ]; |
| 214 | 167 | } |
| 215 | 168 | |
| 216 | - if ( $deliveryAddress->getCounty() ) { | |
| 169 | + if ( $deliveryAddress->getCounty() !== null ) { | |
| 217 | 170 | $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ]; |
| 218 | 171 | } |
| 219 | 172 | |
| 220 | - // TODO: Redo in carrier refactor. | |
| 221 | - if ( $order->getCarrier() && is_numeric( $order->getCarrier()->getId() ) ) { | |
| 173 | + if ( is_numeric( $order->getCarrier()->getId() ) ) { | |
| 222 | 174 | $widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ]; |
| 223 | 175 | } |
| 224 | 176 | |
| 225 | 177 | return $widgetOptions; |
| 226 | 178 | } |
| 227 | - | |
| 228 | 179 | } |