PluginProbe
Packeta / 1.4.3
Packeta v1.4.3
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.3, at src/Packetery/Module/Carrier/Repository.php

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