PluginProbe
Packeta / 1.6.4
Packeta v1.6.4
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 1.6.4, at src/Packetery/Module/Carrier/EntityRepository.php

226 lines 5.5 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 * 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 $carrierId Carrier id.
205 * @param string $customerCountry Customer country.
206 *
207 * @return bool
208 */
209 public function isValidForCountry( string $carrierId, string $customerCountry ): bool {
210 // There is no separate validation for vendor carriers yet.
211 if ( ! is_numeric( $carrierId ) ) {
212 $compoundCarriers = $this->pickupPointsConfig->getCompoundCarriers();
213
214 return ( ! empty( $compoundCarriers[ $customerCountry ] ) );
215 }
216
217 $carrier = $this->getById( (int) $carrierId );
218 if ( null === $carrier || $carrier->isDeleted() || $customerCountry !== $carrier->getCountry() ) {
219 return false;
220 }
221
222 return Options::createByCarrierId( $carrier->getId() )->isActive();
223 }
224
225 }
226