PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
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
← All changes | src/Packetery/Module/Carrier/Repository.php +93 -351 1.4.22.3.2 View file →
@@ -8,10 +8,9 @@
8 8 declare( strict_types=1 );
9 9
10 10 namespace Packetery\Module\Carrier;
11 11
12 -use Packetery\Core\Entity;
13 -use Packetery\Module\EntityFactory;
12 +use Packetery\Module\WpdbAdapter;
14 13
15 14 /**
16 15 * Class CarrierRepository
17 16 * TODO: cache - some queries may run more times during request.
@@ -19,130 +18,73 @@
19 18 * @package Packetery
20 19 */
21 20 class Repository {
22 21
23 - public const INTERNAL_PICKUP_POINTS_ID = 'packeta';
22 + private const COLUMN_NAMES = [
23 + 'id',
24 + 'name',
25 + 'is_pickup_points',
26 + 'has_carrier_direct_label',
27 + 'separate_house_number',
28 + 'customs_declarations',
29 + 'requires_email',
30 + 'requires_phone',
31 + 'requires_size',
32 + 'disallows_cod',
33 + 'country',
34 + 'currency',
35 + 'max_weight',
36 + 'deleted',
37 + 'available',
38 + ];
24 39
25 40 /**
26 - * WordPress wpdb object from global
41 + * WpdbAdapter object from global
27 42 *
28 - * @var \wpdb
43 + * @var WpdbAdapter
29 44 */
30 - private $wpdb;
45 + private $wpdbAdapter;
31 46
32 47 /**
33 - * Carrier Entity Factory.
34 - *
35 - * @var EntityFactory\Carrier
36 - */
37 - private $carrierEntityFactory;
38 -
39 - /**
40 48 * Repository constructor.
41 49 *
42 - * @param \wpdb $wpdb wpdb.
43 - * @param EntityFactory\Carrier $carrierEntityFactory Carrier Entity Factory.
50 + * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
44 51 */
45 - public function __construct( \wpdb $wpdb, EntityFactory\Carrier $carrierEntityFactory ) {
46 - $this->wpdb = $wpdb;
47 - $this->carrierEntityFactory = $carrierEntityFactory;
52 + public function __construct(
53 + WpdbAdapter $wpdbAdapter
54 + ) {
55 + $this->wpdbAdapter = $wpdbAdapter;
48 56 }
49 57
50 58 /**
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 59 * Create table to store carriers.
61 60 *
62 61 * @return bool
63 62 */
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 - }
63 + public function createOrAlterTable(): bool {
64 + $createTableQuery = 'CREATE TABLE ' . $this->wpdbAdapter->packeteryCarrier . ' (
65 + `id` int(11) NOT NULL,
66 + `name` varchar(255) NOT NULL,
67 + `is_pickup_points` tinyint(1) NOT NULL,
68 + `has_carrier_direct_label` tinyint(1) NOT NULL,
69 + `separate_house_number` tinyint(1) NOT NULL,
70 + `customs_declarations` tinyint(1) NOT NULL,
71 + `requires_email` tinyint(1) NOT NULL,
72 + `requires_phone` tinyint(1) NOT NULL,
73 + `requires_size` tinyint(1) NOT NULL,
74 + `disallows_cod` tinyint(1) NOT NULL,
75 + `country` varchar(255) NOT NULL,
76 + `currency` varchar(255) NOT NULL,
77 + `max_weight` float NOT NULL,
78 + `available` tinyint(1) NOT NULL DEFAULT 1,
79 + `deleted` tinyint(1) NOT NULL,
80 + PRIMARY KEY (`id`)
81 + ) ' . $this->wpdbAdapter->get_charset_collate();
86 82
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 . '`' );
83 + return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryCarrier );
93 84 }
94 85
95 86 /**
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 87 * Gets is_pickup_point attribute of a carrier.
146 88 *
147 89 * @param int $carrierId Carrier id.
148 90 *
@@ -148,11 +90,11 @@
148 90 *
149 91 * @return bool
150 92 */
151 93 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 ) );
94 + return (bool) $this->wpdbAdapter->get_var(
95 + $this->wpdbAdapter->prepare( 'SELECT `is_pickup_points` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `id` = %d', $carrierId )
96 + );
155 97 }
156 98
157 99 /**
158 100 * Gets Carrier value object by id.
@@ -158,182 +100,76 @@
158 100 * Gets Carrier value object by id.
159 101 *
160 102 * @param int $carrierId Carrier id.
161 103 *
162 - * @return Entity\Carrier|null
104 + * @return array<string, string>|null
163 105 */
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',
106 + public function getById( int $carrierId ): ?array {
107 + return $this->wpdbAdapter->get_row(
108 + $this->wpdbAdapter->prepare(
109 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
110 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `id` = %s',
184 111 $carrierId
185 112 ),
186 113 ARRAY_A
187 114 );
188 - if ( null === $result ) {
189 - return null;
190 - }
191 -
192 - return $this->carrierEntityFactory->fromDbResult( $result );
193 115 }
194 116
195 117 /**
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 118 * Gets all active carriers for a country.
220 119 *
221 120 * @param string $country ISO code.
121 + * @param bool $includeUnavailable Include unavailable carriers.
222 122 *
223 - * @return Entity\Carrier[]
123 + * @return array|null
224 124 */
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',
125 + public function getByCountry( string $country, bool $includeUnavailable ): ?array {
126 + return $this->wpdbAdapter->get_results(
127 + $this->wpdbAdapter->prepare(
128 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
129 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `country` = %s AND `deleted` = false' .
130 + ( $includeUnavailable ? '' : ' AND `available` = true' ),
247 131 $country
248 132 ),
249 133 ARRAY_A
250 134 );
251 -
252 - foreach ( $countryCarriers as $carrierData ) {
253 - $entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
254 - }
255 -
256 - return $entities;
257 135 }
258 136
259 137 /**
260 138 * Gets all active carriers.
261 139 *
262 - * @return Entity\Carrier[]
140 + * @return array|null
263 141 */
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',
142 + public function getActiveCarriers(): ?array {
143 + return $this->wpdbAdapter->get_results(
144 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
145 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false AND `available` = true',
285 146 ARRAY_A
286 147 );
287 -
288 - foreach ( $countryCarriers as $carrierData ) {
289 - $entities[ $carrierData['id'] ] = $this->carrierEntityFactory->fromDbResult( $carrierData );
290 - }
291 -
292 - return $entities;
293 148 }
294 149
295 150 /**
296 - * Gets all active carriers for a country including internal pickup point carriers.
151 + * Gets all carriers.
297 152 *
298 - * @param string $country ISO code.
299 - *
300 - * @return Entity\Carrier[]
153 + * @return array<int, array<string, string|float|bool>>
301 154 */
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 - }
155 + public function getAllRawIndexed(): array {
156 + $unIndexedResult = $this->wpdbAdapter->get_results(
157 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
158 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '`',
159 + ARRAY_A
160 + );
311 161
312 - return $countryCarriers;
162 + return array_combine( array_column( $unIndexedResult, 'id' ), $unIndexedResult );
313 163 }
314 164
315 165 /**
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 166 * Tells if there is any active feed carrier.
329 167 *
330 168 * @return bool
331 169 */
332 170 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' );
171 + return (bool) $this->wpdbAdapter->get_var( 'SELECT 1 FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false AND `available` = true LIMIT 1' );
336 172 }
337 173
338 174 /**
339 175 * Gets all active countries.
@@ -339,30 +175,21 @@
339 175 * Gets all active countries.
340 176 *
341 177 * @return array
342 178 */
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' );
179 + public function getCountriesWithUnavailable(): array {
180 + return $this->wpdbAdapter->get_col( 'SELECT `country` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`' );
348 181 }
349 182
350 183 /**
351 - * Set those not in feed as deleted.
184 + * Set carriers specified by ids as deleted.
352 185 *
353 - * @param array $carriers_in_feed Carriers in feed.
186 + * @param array $carrierIdsNotInFeed Carriers not in feed.
354 187 */
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 - . ')'
188 + public function set_as_deleted( array $carrierIdsNotInFeed ): void {
189 + $this->wpdbAdapter->query(
190 + 'UPDATE `' . $this->wpdbAdapter->packeteryCarrier . '`
191 + SET `deleted` = 1 WHERE `id` IN (' . implode( ',', $carrierIdsNotInFeed ) . ')'
365 192 );
366 193 }
367 194
368 195 /**
@@ -368,12 +195,12 @@
368 195 /**
369 196 * Inserts carrier data to db.
370 197 *
371 198 * @param array $data Carrier data.
199 + * @return int|false The number of rows inserted, or false on error.
372 200 */
373 - public function insert( array $data ): void {
374 - $wpdb = $this->get_wpdb();
375 - $wpdb->insert( $wpdb->packetery_carrier, $data );
201 + public function insert( array $data ) {
202 + return $this->wpdbAdapter->insert( $this->wpdbAdapter->packeteryCarrier, $data );
376 203 }
377 204
378 205 /**
379 206 * Updates carrier data in db.
@@ -378,96 +205,11 @@
378 205 /**
379 206 * Updates carrier data in db.
380 207 *
381 208 * @param array $data Carrier data.
382 - * @param int $carrier_id Carrier id.
209 + * @param int $carrierId Carrier id.
210 + * @return int|false The number of rows updated, or false on error.
383 211 */
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 ) );
212 + public function update( array $data, int $carrierId ) {
213 + return $this->wpdbAdapter->update( $this->wpdbAdapter->packeteryCarrier, $data, [ 'id' => $carrierId ] );
387 214 }
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 215 }