PluginProbe
Packeta / 1.4.1
Packeta v1.4.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
packeta / src / Packetery / Module / Carrier / Repository.php

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

475 lines 11.7 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 all active carriers for checkbox list
125 *
126 * @return array
127 */
128 public function getAllActiveCarriersList(): array {
129 $activeCarriers = [];
130 $carriers = $this->getAllCarriersIncludingZpoints();
131 foreach ( $carriers as $carrier ) {
132 $carrierOptions = Options::createByCarrierId( $carrier->getId() );
133 if ( $carrierOptions->isActive() ) {
134 $carrierId = 'packetery_carrier_' . $carrierOptions->getOptionId();
135 $activeCarriers[] = [
136 'id' => $carrierId,
137 'label' => $carrierOptions->getName(),
138 ];
139 }
140 }
141
142 return $activeCarriers;
143 }
144
145 /**
146 * Gets is_pickup_point attribute of a carrier.
147 *
148 * @param int $carrierId Carrier id.
149 *
150 * @return bool
151 */
152 public function hasPickupPoints( int $carrierId ): bool {
153 $wpdb = $this->get_wpdb();
154
155 return (bool) $wpdb->get_var( $wpdb->prepare( 'SELECT `is_pickup_points` FROM `' . $wpdb->packetery_carrier . '` WHERE `id` = %d', $carrierId ) );
156 }
157
158 /**
159 * Gets Carrier value object by id.
160 *
161 * @param int $carrierId Carrier id.
162 *
163 * @return Entity\Carrier|null
164 */
165 public function getById( int $carrierId ): ?Entity\Carrier {
166 $wpdb = $this->get_wpdb();
167 $result = $wpdb->get_row(
168 $wpdb->prepare(
169 'SELECT
170 `id`,
171 `name`,
172 `is_pickup_points`,
173 `has_carrier_direct_label`,
174 `separate_house_number`,
175 `customs_declarations`,
176 `requires_email`,
177 `requires_phone`,
178 `requires_size`,
179 `disallows_cod`,
180 `country`,
181 `currency`,
182 `max_weight`,
183 `deleted`
184 FROM `' . $wpdb->packetery_carrier . '` WHERE `id` = %s',
185 $carrierId
186 ),
187 ARRAY_A
188 );
189 if ( null === $result ) {
190 return null;
191 }
192
193 return $this->carrierEntityFactory->fromDbResult( $result );
194 }
195
196 /**
197 * Gets feed carrier or packeta carrier by id.
198 *
199 * @param string $extendedBranchServiceId Extended branch service id.
200 *
201 * @return Entity\Carrier|null
202 */
203 public function getAnyById( string $extendedBranchServiceId ): ?Entity\Carrier {
204 $zpointCarriers = $this->getZpointCarriers();
205
206 foreach ( $zpointCarriers as $zpointCountry => $zpointCarrier ) {
207 if ( $zpointCarrier['id'] === $extendedBranchServiceId ) {
208 return $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrier + [ 'country' => $zpointCountry ] );
209 }
210 }
211
212 if ( ! is_numeric( $extendedBranchServiceId ) ) {
213 return null;
214 }
215
216 return $this->getById( (int) $extendedBranchServiceId );
217 }
218
219 /**
220 * Gets all active carriers for a country.
221 *
222 * @param string $country ISO code.
223 *
224 * @return Entity\Carrier[]
225 */
226 public function getByCountry( string $country ): array {
227 $wpdb = $this->get_wpdb();
228
229 $entities = [];
230 $countryCarriers = $wpdb->get_results(
231 $wpdb->prepare(
232 'SELECT
233 `id`,
234 `name`,
235 `is_pickup_points`,
236 `has_carrier_direct_label`,
237 `separate_house_number`,
238 `customs_declarations`,
239 `requires_email`,
240 `requires_phone`,
241 `requires_size`,
242 `disallows_cod`,
243 `country`,
244 `currency`,
245 `max_weight`,
246 `deleted`
247 FROM `' . $wpdb->packetery_carrier . '` WHERE `country` = %s AND `deleted` = false',
248 $country
249 ),
250 ARRAY_A
251 );
252
253 foreach ( $countryCarriers as $carrierData ) {
254 $entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
255 }
256
257 return $entities;
258 }
259
260 /**
261 * Gets all active carriers.
262 *
263 * @return Entity\Carrier[]
264 */
265 public function getActiveCarriers(): array {
266 $wpdb = $this->wpdb;
267
268 $entities = [];
269 $countryCarriers = $wpdb->get_results(
270 'SELECT
271 `id`,
272 `name`,
273 `is_pickup_points`,
274 `has_carrier_direct_label`,
275 `separate_house_number`,
276 `customs_declarations`,
277 `requires_email`,
278 `requires_phone`,
279 `requires_size`,
280 `disallows_cod`,
281 `country`,
282 `currency`,
283 `max_weight`,
284 `deleted`
285 FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false',
286 ARRAY_A
287 );
288
289 foreach ( $countryCarriers as $carrierData ) {
290 $entities[ $carrierData['id'] ] = $this->carrierEntityFactory->fromDbResult( $carrierData );
291 }
292
293 return $entities;
294 }
295
296 /**
297 * Gets all active carriers for a country including internal pickup point carriers.
298 *
299 * @param string $country ISO code.
300 *
301 * @return Entity\Carrier[]
302 */
303 public function getByCountryIncludingZpoints( string $country ): array {
304 $countryCarriers = $this->getByCountry( $country );
305 $zpointCarriers = $this->getZpointCarriers();
306 if ( ! empty( $zpointCarriers[ $country ] ) ) {
307 $zpointCarrierData = $zpointCarriers[ $country ];
308 $zpointCarrierData['country'] = $country;
309 $zpointCarrier = $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrierData );
310 array_unshift( $countryCarriers, $zpointCarrier );
311 }
312
313 return $countryCarriers;
314 }
315
316 /**
317 * Get all carriers.
318 *
319 * @return Entity\Carrier[]
320 */
321 public function getAllCarriersIncludingZpoints(): array {
322 $feedCarriers = $this->getActiveCarriers();
323 $zpointCarriers = $this->getZpointCarrierCarriers();
324
325 return array_merge( $feedCarriers, $zpointCarriers );
326 }
327
328 /**
329 * Tells if there is any active feed carrier.
330 *
331 * @return bool
332 */
333 public function hasAnyActiveFeedCarrier(): bool {
334 $wpdb = $this->wpdb;
335
336 return (bool) $wpdb->get_var( 'SELECT 1 FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false LIMIT 1' );
337 }
338
339 /**
340 * Gets all active countries.
341 *
342 * @return array
343 */
344 public function getCountries(): array {
345 $wpdb = $this->get_wpdb();
346 $countries = $wpdb->get_results( 'SELECT `country` FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`', ARRAY_A );
347
348 return array_column( ( $countries ? $countries : [] ), 'country' );
349 }
350
351 /**
352 * Set those not in feed as deleted.
353 *
354 * @param array $carriers_in_feed Carriers in feed.
355 */
356 public function set_others_as_deleted( array $carriers_in_feed ): void {
357 $wpdb = $this->get_wpdb();
358 $wpdb->query(
359 'UPDATE `' .
360 $wpdb->packetery_carrier .
361 '` SET `deleted` = 1 WHERE `id` NOT IN (' .
362 // @codingStandardsIgnoreStart
363 implode( ',', $carriers_in_feed )
364 // @codingStandardsIgnoreEnd
365 . ')'
366 );
367 }
368
369 /**
370 * Inserts carrier data to db.
371 *
372 * @param array $data Carrier data.
373 */
374 public function insert( array $data ): void {
375 $wpdb = $this->get_wpdb();
376 $wpdb->insert( $wpdb->packetery_carrier, $data );
377 }
378
379 /**
380 * Updates carrier data in db.
381 *
382 * @param array $data Carrier data.
383 * @param int $carrier_id Carrier id.
384 */
385 public function update( array $data, int $carrier_id ): void {
386 $wpdb = $this->get_wpdb();
387 $wpdb->update( $wpdb->packetery_carrier, $data, array( 'id' => $carrier_id ) );
388 }
389
390 /**
391 * Returns internal pickup points configuration
392 *
393 * @return array[]
394 */
395 public function getZpointCarriers(): array {
396 return [
397 'cz' => [
398 'id' => 'zpointcz',
399 'name' => __( 'CZ Packeta pickup points', 'packeta' ),
400 'is_pickup_points' => 1,
401 'currency' => 'CZK',
402 'supports_age_verification' => true,
403 ],
404 'sk' => [
405 'id' => 'zpointsk',
406 'name' => __( 'SK Packeta pickup points', 'packeta' ),
407 'is_pickup_points' => 1,
408 'currency' => 'EUR',
409 'supports_age_verification' => true,
410 ],
411 'hu' => [
412 'id' => 'zpointhu',
413 'name' => __( 'HU Packeta pickup points', 'packeta' ),
414 'is_pickup_points' => 1,
415 'currency' => 'HUF',
416 'supports_age_verification' => true,
417 ],
418 'ro' => [
419 'id' => 'zpointro',
420 'name' => __( 'RO Packeta pickup points', 'packeta' ),
421 'is_pickup_points' => 1,
422 'currency' => 'RON',
423 'supports_age_verification' => true,
424 ],
425 ];
426 }
427
428 /**
429 * Gets zpoint carriers as object.
430 *
431 * @return Entity\Carrier[]
432 */
433 public function getZpointCarrierCarriers(): array {
434 $carriers = [];
435 $zpointCarriers = $this->getZpointCarriers();
436
437 foreach ( $zpointCarriers as $country => $zpointCarrier ) {
438 $carriers[ $zpointCarrier['id'] ] = $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrier + [ 'country' => $country ] );
439 }
440
441 return $carriers;
442 }
443
444 /**
445 * Checks if chosen carrier has pickup points and sets carrier id in provided array.
446 *
447 * @param string $carrierId Carrier id.
448 *
449 * @return bool
450 */
451 public function isPickupPointCarrier( string $carrierId ): bool {
452 if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) {
453 return true;
454 }
455
456 return $this->hasPickupPoints( (int) $carrierId );
457 }
458
459 /**
460 * Checks if carrier is home delivery carrier.
461 *
462 * @param string $carrierId Carrier ID.
463 *
464 * @return bool
465 */
466 public function isHomeDeliveryCarrier( string $carrierId ): bool {
467 if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) {
468 return false;
469 }
470
471 return false === $this->hasPickupPoints( (int) $carrierId );
472 }
473
474 }
475