PluginProbe
Packeta / 1.4
Packeta v1.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 / Repository.php

Repository.php in Packeta 1.4, at src/Packetery/Module/Carrier/Repository.php

453 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Packeta carrier repository
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 CarrierRepository
17 * TODO: cache - some queries may run more times during request.
18 *
19 * @package Packetery
20 */
21 class Repository {
22
23 public const INTERNAL_PICKUP_POINTS_ID = 'packeta';
24
25 /**
26 * WordPress wpdb object from global
27 *
28 * @var \wpdb
29 */
30 private $wpdb;
31
32 /**
33 * Carrier Entity Factory.
34 *
35 * @var EntityFactory\Carrier
36 */
37 private $carrierEntityFactory;
38
39 /**
40 * Repository constructor.
41 *
42 * @param \wpdb $wpdb wpdb.
43 * @param EntityFactory\Carrier $carrierEntityFactory Carrier Entity Factory.
44 */
45 public function __construct( \wpdb $wpdb, EntityFactory\Carrier $carrierEntityFactory ) {
46 $this->wpdb = $wpdb;
47 $this->carrierEntityFactory = $carrierEntityFactory;
48 }
49
50 /**
51 * Gets wpdb object from global variable with custom tables names set.
52 *
53 * @return \wpdb
54 */
55 private function get_wpdb(): \wpdb {
56 return $this->wpdb;
57 }
58
59 /**
60 * Create table to store carriers.
61 *
62 * @return bool
63 */
64 public function createTable(): bool {
65 $wpdb = $this->get_wpdb();
66 return $wpdb->query(
67 'CREATE TABLE IF NOT EXISTS `' . $wpdb->packetery_carrier . '` (
68 `id` int NOT NULL,
69 `name` varchar(255) NOT NULL,
70 `is_pickup_points` boolean NOT NULL,
71 `has_carrier_direct_label` boolean NOT NULL,
72 `separate_house_number` boolean NOT NULL,
73 `customs_declarations` boolean NOT NULL,
74 `requires_email` boolean NOT NULL,
75 `requires_phone` boolean NOT NULL,
76 `requires_size` boolean NOT NULL,
77 `disallows_cod` boolean NOT NULL,
78 `country` varchar(255) NOT NULL,
79 `currency` varchar(255) NOT NULL,
80 `max_weight` float NOT NULL,
81 `deleted` boolean NOT NULL,
82 PRIMARY KEY (`id`)
83 ) ' . $wpdb->get_charset_collate()
84 );
85 }
86
87 /**
88 * Drop table used to store carriers.
89 */
90 public function drop(): void {
91 $wpdb = $this->get_wpdb();
92 $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->packetery_carrier . '`' );
93 }
94
95 /**
96 * Gets known carrier ids.
97 *
98 * @return array|null
99 */
100 public function get_carrier_ids(): ?array {
101 $wpdb = $this->get_wpdb();
102
103 return $wpdb->get_results( 'SELECT `id` FROM `' . $wpdb->packetery_carrier . '`', ARRAY_A );
104 }
105
106 /**
107 * Gets all active carriers including internal pickup point carriers.
108 *
109 * @return array|null
110 */
111 public function getAllIncludingZpoints(): ?array {
112 $wpdb = $this->get_wpdb();
113
114 $carriers = $wpdb->get_results( 'SELECT `id`, `name`, `is_pickup_points` FROM `' . $wpdb->packetery_carrier . '`', ARRAY_A );
115 $zpointCarriers = $this->getZpointCarriers();
116 foreach ( $zpointCarriers as $zpointCarrier ) {
117 array_unshift( $carriers, $zpointCarrier );
118 }
119
120 return $carriers;
121 }
122
123 /**
124 * Gets is_pickup_point attribute of a carrier.
125 *
126 * @param int $carrierId Carrier id.
127 *
128 * @return bool
129 */
130 public function hasPickupPoints( int $carrierId ): bool {
131 $wpdb = $this->get_wpdb();
132
133 return (bool) $wpdb->get_var( $wpdb->prepare( 'SELECT `is_pickup_points` FROM `' . $wpdb->packetery_carrier . '` WHERE `id` = %d', $carrierId ) );
134 }
135
136 /**
137 * Gets Carrier value object by id.
138 *
139 * @param int $carrierId Carrier id.
140 *
141 * @return Entity\Carrier|null
142 */
143 public function getById( int $carrierId ): ?Entity\Carrier {
144 $wpdb = $this->get_wpdb();
145 $result = $wpdb->get_row(
146 $wpdb->prepare(
147 'SELECT
148 `id`,
149 `name`,
150 `is_pickup_points`,
151 `has_carrier_direct_label`,
152 `separate_house_number`,
153 `customs_declarations`,
154 `requires_email`,
155 `requires_phone`,
156 `requires_size`,
157 `disallows_cod`,
158 `country`,
159 `currency`,
160 `max_weight`,
161 `deleted`
162 FROM `' . $wpdb->packetery_carrier . '` WHERE `id` = %s',
163 $carrierId
164 ),
165 ARRAY_A
166 );
167 if ( null === $result ) {
168 return null;
169 }
170
171 return $this->carrierEntityFactory->fromDbResult( $result );
172 }
173
174 /**
175 * Gets feed carrier or packeta carrier by id.
176 *
177 * @param string $extendedBranchServiceId Extended branch service id.
178 *
179 * @return Entity\Carrier|null
180 */
181 public function getAnyById( string $extendedBranchServiceId ): ?Entity\Carrier {
182 $zpointCarriers = $this->getZpointCarriers();
183
184 foreach ( $zpointCarriers as $zpointCountry => $zpointCarrier ) {
185 if ( $zpointCarrier['id'] === $extendedBranchServiceId ) {
186 return $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrier + [ 'country' => $zpointCountry ] );
187 }
188 }
189
190 if ( ! is_numeric( $extendedBranchServiceId ) ) {
191 return null;
192 }
193
194 return $this->getById( (int) $extendedBranchServiceId );
195 }
196
197 /**
198 * Gets all active carriers for a country.
199 *
200 * @param string $country ISO code.
201 *
202 * @return Entity\Carrier[]
203 */
204 public function getByCountry( string $country ): array {
205 $wpdb = $this->get_wpdb();
206
207 $entities = [];
208 $countryCarriers = $wpdb->get_results(
209 $wpdb->prepare(
210 'SELECT
211 `id`,
212 `name`,
213 `is_pickup_points`,
214 `has_carrier_direct_label`,
215 `separate_house_number`,
216 `customs_declarations`,
217 `requires_email`,
218 `requires_phone`,
219 `requires_size`,
220 `disallows_cod`,
221 `country`,
222 `currency`,
223 `max_weight`,
224 `deleted`
225 FROM `' . $wpdb->packetery_carrier . '` WHERE `country` = %s AND `deleted` = false',
226 $country
227 ),
228 ARRAY_A
229 );
230
231 foreach ( $countryCarriers as $carrierData ) {
232 $entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
233 }
234
235 return $entities;
236 }
237
238 /**
239 * Gets all active carriers.
240 *
241 * @return Entity\Carrier[]
242 */
243 public function getActiveCarriers(): array {
244 $wpdb = $this->wpdb;
245
246 $entities = [];
247 $countryCarriers = $wpdb->get_results(
248 'SELECT
249 `id`,
250 `name`,
251 `is_pickup_points`,
252 `has_carrier_direct_label`,
253 `separate_house_number`,
254 `customs_declarations`,
255 `requires_email`,
256 `requires_phone`,
257 `requires_size`,
258 `disallows_cod`,
259 `country`,
260 `currency`,
261 `max_weight`,
262 `deleted`
263 FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false',
264 ARRAY_A
265 );
266
267 foreach ( $countryCarriers as $carrierData ) {
268 $entities[ $carrierData['id'] ] = $this->carrierEntityFactory->fromDbResult( $carrierData );
269 }
270
271 return $entities;
272 }
273
274 /**
275 * Gets all active carriers for a country including internal pickup point carriers.
276 *
277 * @param string $country ISO code.
278 *
279 * @return Entity\Carrier[]
280 */
281 public function getByCountryIncludingZpoints( string $country ): array {
282 $countryCarriers = $this->getByCountry( $country );
283 $zpointCarriers = $this->getZpointCarriers();
284 if ( ! empty( $zpointCarriers[ $country ] ) ) {
285 $zpointCarrierData = $zpointCarriers[ $country ];
286 $zpointCarrierData['country'] = $country;
287 $zpointCarrier = $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrierData );
288 array_unshift( $countryCarriers, $zpointCarrier );
289 }
290
291 return $countryCarriers;
292 }
293
294 /**
295 * Get all carriers.
296 *
297 * @return Entity\Carrier[]
298 */
299 public function getAllCarriersIncludingZpoints(): array {
300 $feedCarriers = $this->getActiveCarriers();
301 $zpointCarriers = $this->getZpointCarrierCarriers();
302
303 return array_merge( $feedCarriers, $zpointCarriers );
304 }
305
306 /**
307 * Tells if there is any active feed carrier.
308 *
309 * @return bool
310 */
311 public function hasAnyActiveFeedCarrier(): bool {
312 $wpdb = $this->wpdb;
313
314 return (bool) $wpdb->get_var( 'SELECT 1 FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false LIMIT 1' );
315 }
316
317 /**
318 * Gets all active countries.
319 *
320 * @return array
321 */
322 public function getCountries(): array {
323 $wpdb = $this->get_wpdb();
324 $countries = $wpdb->get_results( 'SELECT `country` FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`', ARRAY_A );
325
326 return array_column( ( $countries ? $countries : [] ), 'country' );
327 }
328
329 /**
330 * Set those not in feed as deleted.
331 *
332 * @param array $carriers_in_feed Carriers in feed.
333 */
334 public function set_others_as_deleted( array $carriers_in_feed ): void {
335 $wpdb = $this->get_wpdb();
336 $wpdb->query(
337 'UPDATE `' .
338 $wpdb->packetery_carrier .
339 '` SET `deleted` = 1 WHERE `id` NOT IN (' .
340 // @codingStandardsIgnoreStart
341 implode( ',', $carriers_in_feed )
342 // @codingStandardsIgnoreEnd
343 . ')'
344 );
345 }
346
347 /**
348 * Inserts carrier data to db.
349 *
350 * @param array $data Carrier data.
351 */
352 public function insert( array $data ): void {
353 $wpdb = $this->get_wpdb();
354 $wpdb->insert( $wpdb->packetery_carrier, $data );
355 }
356
357 /**
358 * Updates carrier data in db.
359 *
360 * @param array $data Carrier data.
361 * @param int $carrier_id Carrier id.
362 */
363 public function update( array $data, int $carrier_id ): void {
364 $wpdb = $this->get_wpdb();
365 $wpdb->update( $wpdb->packetery_carrier, $data, array( 'id' => $carrier_id ) );
366 }
367
368 /**
369 * Returns internal pickup points configuration
370 *
371 * @return array[]
372 */
373 public function getZpointCarriers(): array {
374 return [
375 'cz' => [
376 'id' => 'zpointcz',
377 'name' => __( 'CZ Packeta pickup points', 'packeta' ),
378 'is_pickup_points' => 1,
379 'currency' => 'CZK',
380 'supports_age_verification' => true,
381 ],
382 'sk' => [
383 'id' => 'zpointsk',
384 'name' => __( 'SK Packeta pickup points', 'packeta' ),
385 'is_pickup_points' => 1,
386 'currency' => 'EUR',
387 'supports_age_verification' => true,
388 ],
389 'hu' => [
390 'id' => 'zpointhu',
391 'name' => __( 'HU Packeta pickup points', 'packeta' ),
392 'is_pickup_points' => 1,
393 'currency' => 'HUF',
394 'supports_age_verification' => true,
395 ],
396 'ro' => [
397 'id' => 'zpointro',
398 'name' => __( 'RO Packeta pickup points', 'packeta' ),
399 'is_pickup_points' => 1,
400 'currency' => 'RON',
401 'supports_age_verification' => true,
402 ],
403 ];
404 }
405
406 /**
407 * Gets zpoint carriers as object.
408 *
409 * @return Entity\Carrier[]
410 */
411 public function getZpointCarrierCarriers(): array {
412 $carriers = [];
413 $zpointCarriers = $this->getZpointCarriers();
414
415 foreach ( $zpointCarriers as $country => $zpointCarrier ) {
416 $carriers[ $zpointCarrier['id'] ] = $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrier + [ 'country' => $country ] );
417 }
418
419 return $carriers;
420 }
421
422 /**
423 * Checks if chosen carrier has pickup points and sets carrier id in provided array.
424 *
425 * @param string $carrierId Carrier id.
426 *
427 * @return bool
428 */
429 public function isPickupPointCarrier( string $carrierId ): bool {
430 if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) {
431 return true;
432 }
433
434 return $this->hasPickupPoints( (int) $carrierId );
435 }
436
437 /**
438 * Checks if carrier is home delivery carrier.
439 *
440 * @param string $carrierId Carrier ID.
441 *
442 * @return bool
443 */
444 public function isHomeDeliveryCarrier( string $carrierId ): bool {
445 if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) {
446 return false;
447 }
448
449 return false === $this->hasPickupPoints( (int) $carrierId );
450 }
451
452 }
453