| 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\Module\EntityFactory; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class EntityRepository |
| 17 |
* |
| 18 |
* @package Packetery |
| 19 |
*/ |
| 20 |
class EntityRepository { |
| 21 |
|
| 22 |
/** |
| 23 |
* Carrier repository. |
| 24 |
* |
| 25 |
* @var Repository |
| 26 |
*/ |
| 27 |
private $repository; |
| 28 |
|
| 29 |
/** |
| 30 |
* Carrier Entity Factory. |
| 31 |
* |
| 32 |
* @var EntityFactory\Carrier |
| 33 |
*/ |
| 34 |
private $carrierEntityFactory; |
| 35 |
|
| 36 |
/** |
| 37 |
* Internal pickup points config. |
| 38 |
* |
| 39 |
* @var PacketaPickupPointsConfig |
| 40 |
*/ |
| 41 |
private $pickupPointsConfig; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
* |
| 46 |
* @param Repository $repository Carrier repository. |
| 47 |
* @param EntityFactory\Carrier $carrierEntityFactory Carrier Entity Factory. |
| 48 |
* @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config. |
| 49 |
*/ |
| 50 |
public function __construct( |
| 51 |
Repository $repository, |
| 52 |
EntityFactory\Carrier $carrierEntityFactory, |
| 53 |
PacketaPickupPointsConfig $pickupPointsConfig |
| 54 |
) { |
| 55 |
$this->repository = $repository; |
| 56 |
$this->carrierEntityFactory = $carrierEntityFactory; |
| 57 |
$this->pickupPointsConfig = $pickupPointsConfig; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Gets Carrier value object by id. |
| 62 |
* |
| 63 |
* @param int $carrierId Carrier id. |
| 64 |
* |
| 65 |
* @return Entity\Carrier|null |
| 66 |
*/ |
| 67 |
public function getById( int $carrierId ): ?Entity\Carrier { |
| 68 |
$result = $this->repository->getById( $carrierId ); |
| 69 |
if ( null === $result ) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
return $this->carrierEntityFactory->fromDbResult( $result ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Gets feed carrier or packeta carrier by id. |
| 78 |
* |
| 79 |
* @param string $carrierId Extended branch service id. |
| 80 |
* |
| 81 |
* @return Entity\Carrier|null |
| 82 |
*/ |
| 83 |
public function getAnyById( string $carrierId ): ?Entity\Carrier { |
| 84 |
$nonFeedCarriers = $this->pickupPointsConfig->getCompoundAndVendorCarriers(); |
| 85 |
|
| 86 |
foreach ( $nonFeedCarriers as $nonFeedCarrier ) { |
| 87 |
if ( $nonFeedCarrier->getId() === $carrierId ) { |
| 88 |
return $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrier ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if ( ! is_numeric( $carrierId ) ) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
return $this->getById( (int) $carrierId ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Gets all active carriers for a country. |
| 101 |
* |
| 102 |
* @param string $country ISO code. |
| 103 |
* |
| 104 |
* @return Entity\Carrier[] |
| 105 |
*/ |
| 106 |
public function getByCountry( string $country ): array { |
| 107 |
$entities = []; |
| 108 |
$countryCarriers = $this->repository->getByCountry( $country ); |
| 109 |
|
| 110 |
foreach ( $countryCarriers as $carrierData ) { |
| 111 |
$entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData ); |
| 112 |
} |
| 113 |
|
| 114 |
return $entities; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Gets all active carriers. |
| 120 |
* |
| 121 |
* @return Entity\Carrier[] |
| 122 |
*/ |
| 123 |
public function getActiveCarriers(): array { |
| 124 |
$entities = []; |
| 125 |
$activeCarriers = $this->repository->getActiveCarriers(); |
| 126 |
|
| 127 |
foreach ( $activeCarriers as $carrierData ) { |
| 128 |
$entities[ $carrierData['id'] ] = $this->carrierEntityFactory->fromDbResult( $carrierData ); |
| 129 |
} |
| 130 |
|
| 131 |
return $entities; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Gets all active carriers for a country including internal pickup point carriers. |
| 136 |
* |
| 137 |
* @param string $country ISO code. |
| 138 |
* |
| 139 |
* @return Entity\Carrier[] |
| 140 |
*/ |
| 141 |
public function getByCountryIncludingNonFeed( string $country ): array { |
| 142 |
$nonFeedCarriers = []; |
| 143 |
$nonFeedCarriersArrays = $this->pickupPointsConfig->getNonFeedCarriersByCountry( $country ); |
| 144 |
foreach ( $nonFeedCarriersArrays as $nonFeedCarrierData ) { |
| 145 |
$nonFeedCarriers[] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrierData ); |
| 146 |
} |
| 147 |
$feedCarriers = $this->getByCountry( $country ); |
| 148 |
|
| 149 |
return array_merge( $nonFeedCarriers, $feedCarriers ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get all carriers. |
| 154 |
* |
| 155 |
* @return Entity\Carrier[] |
| 156 |
*/ |
| 157 |
public function getAllCarriersIncludingNonFeed(): array { |
| 158 |
$feedCarriers = $this->getActiveCarriers(); |
| 159 |
$nonFeedCarriers = $this->getNonFeedCarriers(); |
| 160 |
|
| 161 |
return array_merge( $feedCarriers, $nonFeedCarriers ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Gets zpoint carriers as object. |
| 166 |
* |
| 167 |
* @return Entity\Carrier[] |
| 168 |
*/ |
| 169 |
public function getNonFeedCarriers(): array { |
| 170 |
$carriers = []; |
| 171 |
$nonFeedCarriers = $this->pickupPointsConfig->getCompoundAndVendorCarriers(); |
| 172 |
|
| 173 |
foreach ( $nonFeedCarriers as $nonFeedCarrier ) { |
| 174 |
$carriers[ $nonFeedCarrier->getId() ] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrier ); |
| 175 |
} |
| 176 |
|
| 177 |
return $carriers; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Gets all active carriers for checkbox list |
| 182 |
* |
| 183 |
* @return array |
| 184 |
*/ |
| 185 |
public function getAllActiveCarriersList(): array { |
| 186 |
$activeCarriers = []; |
| 187 |
$carriers = $this->getAllCarriersIncludingNonFeed(); |
| 188 |
foreach ( $carriers as $carrier ) { |
| 189 |
$carrierOptions = Options::createByCarrierId( $carrier->getId() ); |
| 190 |
if ( $carrierOptions->isActive() ) { |
| 191 |
$activeCarriers[] = [ |
| 192 |
'option_id' => $carrierOptions->getOptionId(), |
| 193 |
'label' => $carrierOptions->getName(), |
| 194 |
]; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return $activeCarriers; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Validates carrier for country. |
| 203 |
* |
| 204 |
* @param string|null $carrierId Null for internal pickup points. |
| 205 |
* @param string $customerCountry Customer country. |
| 206 |
* |
| 207 |
* @return bool |
| 208 |
*/ |
| 209 |
public function isValidForCountry( ?string $carrierId, string $customerCountry ): bool { |
| 210 |
if ( null === $carrierId ) { |
| 211 |
$compoundCarriers = $this->pickupPointsConfig->getCompoundCarriers(); |
| 212 |
|
| 213 |
return ( ! empty( $compoundCarriers[ $customerCountry ] ) ); |
| 214 |
} |
| 215 |
|
| 216 |
$carrier = $this->getById( (int) $carrierId ); |
| 217 |
if ( null === $carrier || $carrier->isDeleted() || $customerCountry !== $carrier->getCountry() ) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
return Options::createByCarrierId( $carrier->getId() )->isActive(); |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|