PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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
packeta / src / Packetery / Module / Carrier / EntityRepository.php

EntityRepository.php in Packeta 2.0.9, at src/Packetery/Module/Carrier/EntityRepository.php

259 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Car delivery config.
45 *
46 * @var CarDeliveryConfig
47 */
48 private $carDeliveryConfig;
49
50 /**
51 * Carrier options factory.
52 *
53 * @var CarrierOptionsFactory
54 */
55 private $carrierOptionsFactory;
56
57 /**
58 * @var CarrierActivityBridge
59 */
60 private $carrierActivityBridge;
61
62 public function __construct(
63 Repository $repository,
64 EntityFactory\Carrier $carrierEntityFactory,
65 PacketaPickupPointsConfig $pickupPointsConfig,
66 CarDeliveryConfig $carDeliveryConfig,
67 CarrierOptionsFactory $carrierOptionsFactory,
68 CarrierActivityBridge $carrierActivityBridge
69 ) {
70 $this->repository = $repository;
71 $this->carrierEntityFactory = $carrierEntityFactory;
72 $this->pickupPointsConfig = $pickupPointsConfig;
73 $this->carDeliveryConfig = $carDeliveryConfig;
74 $this->carrierOptionsFactory = $carrierOptionsFactory;
75 $this->carrierActivityBridge = $carrierActivityBridge;
76 }
77
78 /**
79 * Gets Carrier value object by id.
80 *
81 * @param int $carrierId Carrier id.
82 *
83 * @return Entity\Carrier|null
84 */
85 public function getById( int $carrierId ): ?Entity\Carrier {
86 $result = $this->repository->getById( $carrierId );
87 if ( $result === null ) {
88 return null;
89 }
90
91 return $this->carrierEntityFactory->fromDbResult( $result );
92 }
93
94 /**
95 * Gets feed carrier or Packeta carrier by id.
96 *
97 * @param string $carrierId Extended branch service id.
98 *
99 * @return Entity\Carrier|null
100 */
101 public function getAnyById( string $carrierId ): ?Entity\Carrier {
102 $nonFeedCarriers = $this->pickupPointsConfig->getCompoundAndVendorCarriers();
103
104 foreach ( $nonFeedCarriers as $nonFeedCarrier ) {
105 if ( $nonFeedCarrier->getId() === $carrierId ) {
106 return $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrier );
107 }
108 }
109
110 if ( ! is_numeric( $carrierId ) ) {
111 return null;
112 }
113
114 return $this->getById( (int) $carrierId );
115 }
116
117 /**
118 * Gets all active carriers for a country.
119 *
120 * @param string $country ISO code.
121 *
122 * @return Entity\Carrier[]
123 */
124 public function getByCountry( string $country ): array {
125 $entities = [];
126 $countryCarriers = $this->repository->getByCountry( $country );
127
128 foreach ( $countryCarriers as $carrierData ) {
129 $entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
130 }
131
132 return $entities;
133 }
134
135 /**
136 * Gets all active carriers.
137 *
138 * @return Entity\Carrier[]
139 */
140 public function getActiveCarriers(): array {
141 $entities = [];
142 $activeCarriers = $this->repository->getActiveCarriers();
143
144 foreach ( $activeCarriers as $carrierData ) {
145 $entities[ $carrierData['id'] ] = $this->carrierEntityFactory->fromDbResult( $carrierData );
146 }
147
148 return $entities;
149 }
150
151 /**
152 * Gets all active carriers for a country including internal pickup point carriers.
153 *
154 * @param string $country ISO code.
155 *
156 * @return Entity\Carrier[]
157 */
158 public function getByCountryIncludingNonFeed( string $country ): array {
159 $nonFeedCarriers = [];
160 $nonFeedCarriersArrays = $this->pickupPointsConfig->getNonFeedCarriersByCountry( $country );
161 foreach ( $nonFeedCarriersArrays as $nonFeedCarrierData ) {
162 $nonFeedCarriers[] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrierData );
163 }
164 $feedCarriers = $this->getByCountry( $country );
165
166 return array_merge( $nonFeedCarriers, $feedCarriers );
167 }
168
169 /**
170 * Get all carriers.
171 *
172 * @return Entity\Carrier[]
173 */
174 public function getAllCarriersIncludingNonFeed(): array {
175 $feedCarriers = $this->getActiveCarriers();
176 $nonFeedCarriers = $this->getNonFeedCarriers();
177
178 return array_merge( $feedCarriers, $nonFeedCarriers );
179 }
180
181 /**
182 * Gets zpoint carriers as object.
183 *
184 * @return Entity\Carrier[]
185 */
186 public function getNonFeedCarriers(): array {
187 $carriers = [];
188 $nonFeedCarriers = $this->pickupPointsConfig->getCompoundAndVendorCarriers();
189
190 foreach ( $nonFeedCarriers as $nonFeedCarrier ) {
191 $carriers[ $nonFeedCarrier->getId() ] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrier );
192 }
193
194 return $carriers;
195 }
196
197 /**
198 * Gets all active carriers for checkbox list
199 *
200 * @return array
201 */
202 public function getAllActiveCarriersList(): array {
203 $activeCarriers = [];
204 $carriers = $this->getAllCarriersIncludingNonFeed();
205 foreach ( $carriers as $carrier ) {
206 $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );
207 if ( $this->carrierActivityBridge->isActive( $carrier->getId(), $carrierOptions ) ) {
208 $activeCarriers[] = [
209 'option_id' => $carrierOptions->getOptionId(),
210 'label' => $carrierOptions->getName(),
211 ];
212 }
213 }
214
215 return $activeCarriers;
216 }
217
218 /**
219 * Validates carrier for country.
220 *
221 * @param string $carrierId Carrier id.
222 * @param string $customerCountry Customer country.
223 *
224 * @return bool
225 */
226 public function isValidForCountry( string $carrierId, string $customerCountry ): bool {
227 // There is no separate validation for vendor carriers yet.
228 if ( ! is_numeric( $carrierId ) ) {
229 $compoundCarriers = $this->pickupPointsConfig->getCompoundCarriers();
230
231 return ( isset( $compoundCarriers[ $customerCountry ] ) );
232 }
233
234 $carrier = $this->getById( (int) $carrierId );
235 if ( $carrier === null || $carrier->isDeleted() || $customerCountry !== $carrier->getCountry() ) {
236 return false;
237 }
238
239 $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );
240
241 return $this->carrierActivityBridge->isActive( $carrier->getId(), $carrierOptions );
242 }
243
244 /**
245 * Checks if carrier is home delivery carrier.
246 *
247 * @param string $carrierId Carrier ID.
248 *
249 * @return bool
250 */
251 public function isHomeDeliveryCarrier( string $carrierId ): bool {
252 if ( $this->pickupPointsConfig->isInternalPickupPointCarrier( $carrierId ) ) {
253 return false;
254 }
255
256 return ( $this->repository->hasPickupPoints( (int) $carrierId ) || $this->carDeliveryConfig->isCarDeliveryCarrier( $carrierId ) ) === false;
257 }
258 }
259