| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class EntityRepository |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Carrier; |
| 11 |
|
| 12 |
use Packetery\Core\Entity; |
| 13 |
use Packetery\Core\Entity\Carrier; |
| 14 |
use Packetery\Module\EntityFactory; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class EntityRepository |
| 18 |
* |
| 19 |
* @package Packetery |
| 20 |
*/ |
| 21 |
class EntityRepository { |
| 22 |
|
| 23 |
/** |
| 24 |
* @var array<int, Entity\Carrier|null> |
| 25 |
*/ |
| 26 |
private static $carrierDataCache = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Carrier repository. |
| 30 |
* |
| 31 |
* @var Repository |
| 32 |
*/ |
| 33 |
private $repository; |
| 34 |
|
| 35 |
/** |
| 36 |
* Carrier Entity Factory. |
| 37 |
* |
| 38 |
* @var EntityFactory\Carrier |
| 39 |
*/ |
| 40 |
private $carrierEntityFactory; |
| 41 |
|
| 42 |
/** |
| 43 |
* Internal pickup points config. |
| 44 |
* |
| 45 |
* @var PacketaPickupPointsConfig |
| 46 |
*/ |
| 47 |
private $pickupPointsConfig; |
| 48 |
|
| 49 |
/** |
| 50 |
* Car delivery config. |
| 51 |
* |
| 52 |
* @var CarDeliveryConfig |
| 53 |
*/ |
| 54 |
private $carDeliveryConfig; |
| 55 |
|
| 56 |
/** |
| 57 |
* Carrier options factory. |
| 58 |
* |
| 59 |
* @var CarrierOptionsFactory |
| 60 |
*/ |
| 61 |
private $carrierOptionsFactory; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var CarrierActivityBridge |
| 65 |
*/ |
| 66 |
private $carrierActivityBridge; |
| 67 |
|
| 68 |
public function __construct( |
| 69 |
Repository $repository, |
| 70 |
EntityFactory\Carrier $carrierEntityFactory, |
| 71 |
PacketaPickupPointsConfig $pickupPointsConfig, |
| 72 |
CarDeliveryConfig $carDeliveryConfig, |
| 73 |
CarrierOptionsFactory $carrierOptionsFactory, |
| 74 |
CarrierActivityBridge $carrierActivityBridge |
| 75 |
) { |
| 76 |
$this->repository = $repository; |
| 77 |
$this->carrierEntityFactory = $carrierEntityFactory; |
| 78 |
$this->pickupPointsConfig = $pickupPointsConfig; |
| 79 |
$this->carDeliveryConfig = $carDeliveryConfig; |
| 80 |
$this->carrierOptionsFactory = $carrierOptionsFactory; |
| 81 |
$this->carrierActivityBridge = $carrierActivityBridge; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Gets Carrier value object by id. |
| 86 |
* |
| 87 |
* @param int $carrierId Carrier id. |
| 88 |
* |
| 89 |
* @return Entity\Carrier|null |
| 90 |
*/ |
| 91 |
private function getById( int $carrierId ): ?Entity\Carrier { |
| 92 |
$result = $this->repository->getById( $carrierId ); |
| 93 |
if ( $result === null ) { |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
return $this->carrierEntityFactory->fromDbResult( $result ); |
| 98 |
} |
| 99 |
|
| 100 |
private function getByIdCached( int $carrierId ): ?Entity\Carrier { |
| 101 |
if ( ! isset( self::$carrierDataCache[ $carrierId ] ) ) { |
| 102 |
self::$carrierDataCache[ $carrierId ] = $this->getById( $carrierId ); |
| 103 |
} |
| 104 |
|
| 105 |
return self::$carrierDataCache[ $carrierId ]; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets feed carrier or Packeta carrier by id. |
| 110 |
* |
| 111 |
* @param string $carrierId Extended branch service id. |
| 112 |
* @param bool $useCache |
| 113 |
* |
| 114 |
* @return Entity\Carrier|null |
| 115 |
*/ |
| 116 |
public function getAnyById( string $carrierId, bool $useCache = false ): ?Entity\Carrier { |
| 117 |
$nonFeedCarriers = $this->pickupPointsConfig->getCompoundAndVendorCarriers(); |
| 118 |
|
| 119 |
foreach ( $nonFeedCarriers as $nonFeedCarrier ) { |
| 120 |
if ( $nonFeedCarrier->getId() === $carrierId ) { |
| 121 |
return $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrier ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if ( ! is_numeric( $carrierId ) ) { |
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
if ( $useCache === true ) { |
| 130 |
return $this->getByIdCached( (int) $carrierId ); |
| 131 |
} |
| 132 |
|
| 133 |
return $this->getById( (int) $carrierId ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Gets all active carriers for a country. |
| 138 |
* |
| 139 |
* @param string $country ISO code. |
| 140 |
* |
| 141 |
* @return Entity\Carrier[] |
| 142 |
*/ |
| 143 |
public function getByCountry( string $country, bool $includeUnavailable ): array { |
| 144 |
$entities = []; |
| 145 |
$countryCarriers = $this->repository->getByCountry( $country, $includeUnavailable ); |
| 146 |
|
| 147 |
foreach ( $countryCarriers as $carrierData ) { |
| 148 |
$entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData ); |
| 149 |
} |
| 150 |
|
| 151 |
return $entities; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Gets all active carriers. |
| 156 |
* |
| 157 |
* @return Entity\Carrier[] |
| 158 |
*/ |
| 159 |
public function getActiveCarriers(): array { |
| 160 |
$entities = []; |
| 161 |
$activeCarriers = $this->repository->getActiveCarriers(); |
| 162 |
|
| 163 |
foreach ( $activeCarriers as $carrierData ) { |
| 164 |
$entities[ $carrierData['id'] ] = $this->carrierEntityFactory->fromDbResult( $carrierData ); |
| 165 |
} |
| 166 |
|
| 167 |
return $entities; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Gets all active carriers for a country including internal pickup point carriers. |
| 172 |
* |
| 173 |
* @param string $country ISO code. |
| 174 |
* @param bool $includeUnavailable Include unavailable carriers. |
| 175 |
* |
| 176 |
* @return Carrier[] |
| 177 |
*/ |
| 178 |
public function getByCountryIncludingNonFeed( string $country, bool $includeUnavailable ): array { |
| 179 |
$nonFeedCarriers = []; |
| 180 |
$nonFeedCarriersArrays = $this->pickupPointsConfig->getNonFeedCarriersByCountry( $country ); |
| 181 |
foreach ( $nonFeedCarriersArrays as $nonFeedCarrierData ) { |
| 182 |
$nonFeedCarriers[] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrierData ); |
| 183 |
} |
| 184 |
$feedCarriers = $this->getByCountry( $country, $includeUnavailable ); |
| 185 |
|
| 186 |
return array_merge( $nonFeedCarriers, $feedCarriers ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get all carriers. |
| 191 |
* |
| 192 |
* @return Entity\Carrier[] |
| 193 |
*/ |
| 194 |
public function getAllCarriersIncludingNonFeed(): array { |
| 195 |
$feedCarriers = $this->getActiveCarriers(); |
| 196 |
$nonFeedCarriers = $this->getNonFeedCarriers(); |
| 197 |
|
| 198 |
return array_merge( $feedCarriers, $nonFeedCarriers ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Gets zpoint carriers as object. |
| 203 |
* |
| 204 |
* @return Entity\Carrier[] |
| 205 |
*/ |
| 206 |
public function getNonFeedCarriers(): array { |
| 207 |
$carriers = []; |
| 208 |
$nonFeedCarriers = $this->pickupPointsConfig->getCompoundAndVendorCarriers(); |
| 209 |
|
| 210 |
foreach ( $nonFeedCarriers as $nonFeedCarrier ) { |
| 211 |
$carriers[ $nonFeedCarrier->getId() ] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrier ); |
| 212 |
} |
| 213 |
|
| 214 |
return $carriers; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Gets all active carriers for checkbox list |
| 219 |
* |
| 220 |
* @return array |
| 221 |
*/ |
| 222 |
public function getAllActiveCarriersList(): array { |
| 223 |
$activeCarriers = []; |
| 224 |
$carriers = $this->getAllCarriersIncludingNonFeed(); |
| 225 |
foreach ( $carriers as $carrier ) { |
| 226 |
$carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() ); |
| 227 |
if ( $carrierOptions->isActive() ) { |
| 228 |
$activeCarriers[] = [ |
| 229 |
'option_id' => $carrierOptions->getOptionId(), |
| 230 |
'label' => $carrierOptions->getName(), |
| 231 |
]; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
return $activeCarriers; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Validates carrier for country. |
| 240 |
* |
| 241 |
* @param string $carrierId Carrier id. |
| 242 |
* @param string $customerCountry Customer country. |
| 243 |
* |
| 244 |
* @return bool |
| 245 |
*/ |
| 246 |
public function isValidForCountry( string $carrierId, string $customerCountry ): bool { |
| 247 |
// There is no separate validation for vendor carriers yet. |
| 248 |
if ( ! is_numeric( $carrierId ) ) { |
| 249 |
$compoundCarriers = $this->pickupPointsConfig->getCompoundCarriers(); |
| 250 |
|
| 251 |
return ( isset( $compoundCarriers[ $customerCountry ] ) ); |
| 252 |
} |
| 253 |
|
| 254 |
$carrier = $this->getById( (int) $carrierId ); |
| 255 |
if ( $carrier === null || $carrier->isDeleted() || ! $carrier->isAvailable() || $customerCountry !== $carrier->getCountry() ) { |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
$carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() ); |
| 260 |
|
| 261 |
return $this->carrierActivityBridge->isActive( $carrier, $carrierOptions ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Checks if carrier is home delivery carrier. |
| 266 |
* |
| 267 |
* @param string $carrierId Carrier ID. |
| 268 |
* |
| 269 |
* @return bool |
| 270 |
*/ |
| 271 |
public function isHomeDeliveryCarrier( string $carrierId ): bool { |
| 272 |
if ( $this->pickupPointsConfig->isInternalPickupPointCarrier( $carrierId ) ) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
return ( $this->repository->hasPickupPoints( (int) $carrierId ) || $this->carDeliveryConfig->isCarDeliveryCarrier( $carrierId ) ) === false; |
| 277 |
} |
| 278 |
} |
| 279 |
|