| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Carrier; |
| 6 |
|
| 7 |
use Packetery\Core\Entity; |
| 8 |
use Packetery\Core\PickupPointProvider\BaseProvider; |
| 9 |
use Packetery\Core\PickupPointProvider\CompoundCarrierCollectionFactory; |
| 10 |
use Packetery\Core\PickupPointProvider\CompoundProvider; |
| 11 |
use Packetery\Core\PickupPointProvider\VendorCollectionFactory; |
| 12 |
use Packetery\Core\PickupPointProvider\VendorProvider; |
| 13 |
use Packetery\Module\Exception\InvalidCarrierException; |
| 14 |
|
| 15 |
class PacketaPickupPointsConfig { |
| 16 |
|
| 17 |
public const COMPOUND_CARRIER_PREFIX = 'zpoint'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var CompoundCarrierCollectionFactory |
| 21 |
*/ |
| 22 |
private $compoundCarrierFactory; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var VendorCollectionFactory |
| 26 |
*/ |
| 27 |
private $vendorCollectionFactory; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
CompoundCarrierCollectionFactory $compoundCarrierFactory, |
| 31 |
VendorCollectionFactory $vendorCollectionFactory |
| 32 |
) { |
| 33 |
$this->vendorCollectionFactory = $vendorCollectionFactory; |
| 34 |
$this->compoundCarrierFactory = $compoundCarrierFactory; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns internal pickup points configuration |
| 39 |
* |
| 40 |
* @return CompoundProvider[] |
| 41 |
*/ |
| 42 |
public function getCompoundCarriers(): array { |
| 43 |
$translatedNames = [ |
| 44 |
'zpointcz' => 'CZ ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ), |
| 45 |
'zpointsk' => 'SK ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ), |
| 46 |
'zpointhu' => 'HU ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ), |
| 47 |
'zpointro' => 'RO ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ), |
| 48 |
]; |
| 49 |
|
| 50 |
$indexedCollection = []; |
| 51 |
$compoundCarrierCollection = $this->compoundCarrierFactory->create(); |
| 52 |
foreach ( $compoundCarrierCollection as $compoundProvider ) { |
| 53 |
$carrierId = $compoundProvider->getId(); |
| 54 |
assert( isset( $translatedNames[ $carrierId ] ), 'Missing name for carrier id ' . $carrierId ); |
| 55 |
$compoundProvider->setTranslatedName( $translatedNames[ $carrierId ] ); |
| 56 |
// There is only one provider for each country. |
| 57 |
$indexedCollection[ $compoundProvider->getCountry() ] = $compoundProvider; |
| 58 |
} |
| 59 |
|
| 60 |
return $indexedCollection; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Gets vendor groups for compound carrier. |
| 65 |
* |
| 66 |
* @param string $carrierId Carrier id. |
| 67 |
* |
| 68 |
* @return string[] |
| 69 |
*/ |
| 70 |
public function getCompoundCarrierVendorGroups( string $carrierId ): array { |
| 71 |
$vendorGroups = []; |
| 72 |
$vendorCarriers = $this->getVendorCarriers(); |
| 73 |
$compoundCarrierCollection = $this->compoundCarrierFactory->create(); |
| 74 |
foreach ( $compoundCarrierCollection as $compoundProvider ) { |
| 75 |
if ( $compoundProvider->getId() === $carrierId ) { |
| 76 |
$vendorCodes = $compoundProvider->getVendorCodes(); |
| 77 |
foreach ( $vendorCodes as $vendorCode ) { |
| 78 |
$vendorGroups[] = $vendorCarriers[ $vendorCode ]->getGroup(); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $vendorGroups; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return VendorProvider[] |
| 88 |
*/ |
| 89 |
public function getVendorCarriers(): array { |
| 90 |
$translatedNames = [ |
| 91 |
'czzpoint' => 'CZ ' . __( 'Packeta Pick-up Point', 'packeta' ), |
| 92 |
'czzbox' => 'CZ ' . __( 'Packeta', 'packeta' ) . ' Z-BOX', |
| 93 |
'skzpoint' => 'SK ' . __( 'Packeta Pick-up Point', 'packeta' ), |
| 94 |
'skzbox' => 'SK ' . __( 'Packeta', 'packeta' ) . ' Z-BOX', |
| 95 |
'huzpoint' => 'HU ' . __( 'Packeta Pick-up Point', 'packeta' ), |
| 96 |
'huzbox' => 'HU ' . __( 'Packeta', 'packeta' ) . ' Z-BOX', |
| 97 |
'rozpoint' => 'RO ' . __( 'Packeta Pick-up Point', 'packeta' ), |
| 98 |
'rozbox' => 'RO ' . __( 'Packeta', 'packeta' ) . ' Z-BOX', |
| 99 |
]; |
| 100 |
|
| 101 |
$indexedCollection = []; |
| 102 |
$vendorCollection = $this->vendorCollectionFactory->create(); |
| 103 |
foreach ( $vendorCollection as $vendorProvider ) { |
| 104 |
$vendorId = $vendorProvider->getId(); |
| 105 |
assert( isset( $translatedNames[ $vendorId ] ), 'Missing name for vendor id ' . $vendorId ); |
| 106 |
$vendorProvider->setTranslatedName( $translatedNames[ $vendorId ] ); |
| 107 |
$indexedCollection[ $vendorId ] = $vendorProvider; |
| 108 |
} |
| 109 |
|
| 110 |
return $indexedCollection; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Gets all non-feed carriers settings. |
| 115 |
* |
| 116 |
* @return BaseProvider[] |
| 117 |
*/ |
| 118 |
public function getCompoundAndVendorCarriers(): array { |
| 119 |
return array_merge( $this->getCompoundCarriers(), $this->getVendorCarriers() ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Checks if id is compound carrier id. |
| 124 |
* |
| 125 |
* @param string $carrierId Carrier id. |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
public function isCompoundCarrierId( string $carrierId ): bool { |
| 130 |
return ( strpos( $carrierId, self::COMPOUND_CARRIER_PREFIX ) === 0 ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Checks if provided id is a vendor carrier id. |
| 135 |
* |
| 136 |
* @param string $carrierId Carrier id. |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public function isVendorCarrierId( string $carrierId ): bool { |
| 141 |
$vendorCarriers = $this->getVendorCarriers(); |
| 142 |
|
| 143 |
return isset( $vendorCarriers[ $carrierId ] ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* All internal pickup point carrier ids are strings, including old 'packeta' value. |
| 148 |
* |
| 149 |
* @param string $carrierId Carrier id. |
| 150 |
* |
| 151 |
* @return bool |
| 152 |
*/ |
| 153 |
public function isInternalPickupPointCarrier( string $carrierId ): bool { |
| 154 |
return ! is_numeric( $carrierId ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Gets non-feed carriers settings by country. |
| 159 |
* |
| 160 |
* @param string $country Country. |
| 161 |
* |
| 162 |
* @return array<int<0, max>, BaseProvider> |
| 163 |
*/ |
| 164 |
public function getNonFeedCarriersByCountry( string $country ): array { |
| 165 |
$filteredCarriers = []; |
| 166 |
$nonFeedCarriers = $this->getCompoundAndVendorCarriers(); |
| 167 |
|
| 168 |
foreach ( $nonFeedCarriers as $nonFeedCarrier ) { |
| 169 |
if ( $nonFeedCarrier->getCountry() === $country ) { |
| 170 |
$filteredCarriers[] = $nonFeedCarrier; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $filteredCarriers; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Gets internal countries. |
| 179 |
* |
| 180 |
* @return string[] |
| 181 |
*/ |
| 182 |
public function getInternalCountries(): array { |
| 183 |
return array_keys( $this->getCompoundCarriers() ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Changes previously used identifier 'packeta' to zpoint type id. |
| 188 |
* Returns one of ids from CompoundCarrierCollectionFactory, e.g. zpointcz. |
| 189 |
* |
| 190 |
* @param string $carrierId Carrier id. |
| 191 |
* @param string $country Lowercase country. |
| 192 |
* |
| 193 |
* @return string |
| 194 |
* @throws InvalidCarrierException InvalidCarrierException. |
| 195 |
*/ |
| 196 |
public function getFixedCarrierId( string $carrierId, string $country ): string { |
| 197 |
if ( $carrierId === Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) { |
| 198 |
$compoundCarriers = $this->getCompoundCarriers(); |
| 199 |
|
| 200 |
if ( ! isset( $compoundCarriers[ $country ] ) ) { |
| 201 |
throw new InvalidCarrierException( |
| 202 |
sprintf( |
| 203 |
// translators: %s is country code. |
| 204 |
__( 'Selected carrier does not deliver to country "%s".', 'packeta' ), |
| 205 |
$country |
| 206 |
) |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
return $compoundCarriers[ $country ]->getId(); |
| 211 |
} |
| 212 |
|
| 213 |
return $carrierId; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @param string[]|null $vendorGroups Value from carrier options. |
| 218 |
* @param string $carrierId |
| 219 |
* |
| 220 |
* @return string[]|null |
| 221 |
*/ |
| 222 |
public function getFinalVendorGroups( ?array $vendorGroups, string $carrierId ): ?array { |
| 223 |
if ( isset( $vendorGroups ) && count( $vendorGroups ) !== 0 ) { |
| 224 |
return $vendorGroups; |
| 225 |
} |
| 226 |
|
| 227 |
if ( $this->isCompoundCarrierId( $carrierId ) ) { |
| 228 |
$vendorGroups = $this->getCompoundCarrierVendorGroups( $carrierId ); |
| 229 |
} else { |
| 230 |
$vendorCarriers = $this->getVendorCarriers(); |
| 231 |
if ( isset( $vendorCarriers[ $carrierId ] ) ) { |
| 232 |
$vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ]; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return $vendorGroups; |
| 237 |
} |
| 238 |
} |
| 239 |
|