PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/Carrier/EntityRepository.php +75 -22 1.6.42.1 View file →
@@ -9,8 +9,9 @@
9 9
10 10 namespace Packetery\Module\Carrier;
11 11
12 12 use Packetery\Core\Entity;
13 +use Packetery\Core\Entity\Carrier;
13 14 use Packetery\Module\EntityFactory;
14 15
15 16 /**
16 17 * Class EntityRepository
@@ -19,8 +20,13 @@
19 20 */
20 21 class EntityRepository {
21 22
22 23 /**
24 + * @var array<int, Entity\Carrier|null>
25 + */
26 + private static $carrierDataCache = [];
27 +
28 + /**
23 29 * Carrier repository.
24 30 *
25 31 * @var Repository
26 32 */
@@ -40,22 +46,40 @@
40 46 */
41 47 private $pickupPointsConfig;
42 48
43 49 /**
44 - * Constructor.
50 + * Car delivery config.
45 51 *
46 - * @param Repository $repository Carrier repository.
47 - * @param EntityFactory\Carrier $carrierEntityFactory Carrier Entity Factory.
48 - * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
52 + * @var CarDeliveryConfig
49 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 +
50 68 public function __construct(
51 69 Repository $repository,
52 70 EntityFactory\Carrier $carrierEntityFactory,
53 - PacketaPickupPointsConfig $pickupPointsConfig
71 + PacketaPickupPointsConfig $pickupPointsConfig,
72 + CarDeliveryConfig $carDeliveryConfig,
73 + CarrierOptionsFactory $carrierOptionsFactory,
74 + CarrierActivityBridge $carrierActivityBridge
54 75 ) {
55 - $this->repository = $repository;
56 - $this->carrierEntityFactory = $carrierEntityFactory;
57 - $this->pickupPointsConfig = $pickupPointsConfig;
76 + $this->repository = $repository;
77 + $this->carrierEntityFactory = $carrierEntityFactory;
78 + $this->pickupPointsConfig = $pickupPointsConfig;
79 + $this->carDeliveryConfig = $carDeliveryConfig;
80 + $this->carrierOptionsFactory = $carrierOptionsFactory;
81 + $this->carrierActivityBridge = $carrierActivityBridge;
58 82 }
59 83
60 84 /**
61 85 * Gets Carrier value object by id.
@@ -63,11 +87,11 @@
63 87 * @param int $carrierId Carrier id.
64 88 *
65 89 * @return Entity\Carrier|null
66 90 */
67 - public function getById( int $carrierId ): ?Entity\Carrier {
91 + private function getById( int $carrierId ): ?Entity\Carrier {
68 92 $result = $this->repository->getById( $carrierId );
69 - if ( null === $result ) {
93 + if ( $result === null ) {
70 94 return null;
71 95 }
72 96
73 97 return $this->carrierEntityFactory->fromDbResult( $result );
@@ -72,16 +96,25 @@
72 96
73 97 return $this->carrierEntityFactory->fromDbResult( $result );
74 98 }
75 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 +
76 108 /**
77 - * Gets feed carrier or packeta carrier by id.
109 + * Gets feed carrier or Packeta carrier by id.
78 110 *
79 111 * @param string $carrierId Extended branch service id.
112 + * @param bool $useCache
80 113 *
81 114 * @return Entity\Carrier|null
82 115 */
83 - public function getAnyById( string $carrierId ): ?Entity\Carrier {
116 + public function getAnyById( string $carrierId, bool $useCache = false ): ?Entity\Carrier {
84 117 $nonFeedCarriers = $this->pickupPointsConfig->getCompoundAndVendorCarriers();
85 118
86 119 foreach ( $nonFeedCarriers as $nonFeedCarrier ) {
87 120 if ( $nonFeedCarrier->getId() === $carrierId ) {
@@ -92,8 +125,12 @@
92 125 if ( ! is_numeric( $carrierId ) ) {
93 126 return null;
94 127 }
95 128
129 + if ( $useCache === true ) {
130 + return $this->getByIdCached( (int) $carrierId );
131 + }
132 +
96 133 return $this->getById( (int) $carrierId );
97 134 }
98 135
99 136 /**
@@ -102,11 +139,11 @@
102 139 * @param string $country ISO code.
103 140 *
104 141 * @return Entity\Carrier[]
105 142 */
106 - public function getByCountry( string $country ): array {
143 + public function getByCountry( string $country, bool $includeUnavailable ): array {
107 144 $entities = [];
108 - $countryCarriers = $this->repository->getByCountry( $country );
145 + $countryCarriers = $this->repository->getByCountry( $country, $includeUnavailable );
109 146
110 147 foreach ( $countryCarriers as $carrierData ) {
111 148 $entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
112 149 }
@@ -113,9 +150,8 @@
113 150
114 151 return $entities;
115 152 }
116 153
117 -
118 154 /**
119 155 * Gets all active carriers.
120 156 *
121 157 * @return Entity\Carrier[]
@@ -134,18 +170,19 @@
134 170 /**
135 171 * Gets all active carriers for a country including internal pickup point carriers.
136 172 *
137 173 * @param string $country ISO code.
174 + * @param bool $includeUnavailable Include unavailable carriers.
138 175 *
139 - * @return Entity\Carrier[]
176 + * @return Carrier[]
140 177 */
141 - public function getByCountryIncludingNonFeed( string $country ): array {
178 + public function getByCountryIncludingNonFeed( string $country, bool $includeUnavailable ): array {
142 179 $nonFeedCarriers = [];
143 180 $nonFeedCarriersArrays = $this->pickupPointsConfig->getNonFeedCarriersByCountry( $country );
144 181 foreach ( $nonFeedCarriersArrays as $nonFeedCarrierData ) {
145 182 $nonFeedCarriers[] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrierData );
146 183 }
147 - $feedCarriers = $this->getByCountry( $country );
184 + $feedCarriers = $this->getByCountry( $country, $includeUnavailable );
148 185
149 186 return array_merge( $nonFeedCarriers, $feedCarriers );
150 187 }
151 188
@@ -185,9 +222,9 @@
185 222 public function getAllActiveCarriersList(): array {
186 223 $activeCarriers = [];
187 224 $carriers = $this->getAllCarriersIncludingNonFeed();
188 225 foreach ( $carriers as $carrier ) {
189 - $carrierOptions = Options::createByCarrierId( $carrier->getId() );
226 + $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );
190 227 if ( $carrierOptions->isActive() ) {
191 228 $activeCarriers[] = [
192 229 'option_id' => $carrierOptions->getOptionId(),
193 230 'label' => $carrierOptions->getName(),
@@ -210,16 +247,32 @@
210 247 // There is no separate validation for vendor carriers yet.
211 248 if ( ! is_numeric( $carrierId ) ) {
212 249 $compoundCarriers = $this->pickupPointsConfig->getCompoundCarriers();
213 250
214 - return ( ! empty( $compoundCarriers[ $customerCountry ] ) );
251 + return ( isset( $compoundCarriers[ $customerCountry ] ) );
215 252 }
216 253
217 254 $carrier = $this->getById( (int) $carrierId );
218 - if ( null === $carrier || $carrier->isDeleted() || $customerCountry !== $carrier->getCountry() ) {
255 + if ( $carrier === null || $carrier->isDeleted() || ! $carrier->isAvailable() || $customerCountry !== $carrier->getCountry() ) {
219 256 return false;
220 257 }
221 258
222 - return Options::createByCarrierId( $carrier->getId() )->isActive();
259 + $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );
260 +
261 + return $this->carrierActivityBridge->isActive( $carrier, $carrierOptions );
223 262 }
224 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 + }
225 278 }