PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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 +87 -323 1.42.0.9 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,70 +18,68 @@
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 + ];
24 38
25 39 /**
26 - * WordPress wpdb object from global
40 + * WpdbAdapter object from global
27 41 *
28 - * @var \wpdb
42 + * @var WpdbAdapter
29 43 */
30 - private $wpdb;
44 + private $wpdbAdapter;
31 45
32 46 /**
33 - * Carrier Entity Factory.
34 - *
35 - * @var EntityFactory\Carrier
36 - */
37 - private $carrierEntityFactory;
38 -
39 - /**
40 47 * Repository constructor.
41 48 *
42 - * @param \wpdb $wpdb wpdb.
43 - * @param EntityFactory\Carrier $carrierEntityFactory Carrier Entity Factory.
49 + * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
44 50 */
45 - public function __construct( \wpdb $wpdb, EntityFactory\Carrier $carrierEntityFactory ) {
46 - $this->wpdb = $wpdb;
47 - $this->carrierEntityFactory = $carrierEntityFactory;
51 + public function __construct(
52 + WpdbAdapter $wpdbAdapter
53 + ) {
54 + $this->wpdbAdapter = $wpdbAdapter;
48 55 }
49 56
50 57 /**
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 58 * Create table to store carriers.
61 59 *
62 60 * @return bool
63 61 */
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 - );
62 + public function createOrAlterTable(): bool {
63 + $createTableQuery = 'CREATE TABLE ' . $this->wpdbAdapter->packeteryCarrier . ' (
64 + `id` int(11) NOT NULL,
65 + `name` varchar(255) NOT NULL,
66 + `is_pickup_points` tinyint(1) NOT NULL,
67 + `has_carrier_direct_label` tinyint(1) NOT NULL,
68 + `separate_house_number` tinyint(1) NOT NULL,
69 + `customs_declarations` tinyint(1) NOT NULL,
70 + `requires_email` tinyint(1) NOT NULL,
71 + `requires_phone` tinyint(1) NOT NULL,
72 + `requires_size` tinyint(1) NOT NULL,
73 + `disallows_cod` tinyint(1) NOT NULL,
74 + `country` varchar(255) NOT NULL,
75 + `currency` varchar(255) NOT NULL,
76 + `max_weight` float NOT NULL,
77 + `deleted` tinyint(1) NOT NULL,
78 + PRIMARY KEY (`id`)
79 + ) ' . $this->wpdbAdapter->get_charset_collate();
80 +
81 + return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryCarrier );
85 82 }
86 83
87 84 /**
88 85 * Drop table used to store carriers.
@@ -87,41 +84,12 @@
87 84 /**
88 85 * Drop table used to store carriers.
89 86 */
90 87 public function drop(): void {
91 - $wpdb = $this->get_wpdb();
92 - $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->packetery_carrier . '`' );
88 + $this->wpdbAdapter->query( 'DROP TABLE IF EXISTS `' . $this->wpdbAdapter->packeteryCarrier . '`' );
93 89 }
94 90
95 91 /**
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 92 * Gets is_pickup_point attribute of a carrier.
125 93 *
126 94 * @param int $carrierId Carrier id.
127 95 *
@@ -127,11 +95,11 @@
127 95 *
128 96 * @return bool
129 97 */
130 98 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 ) );
99 + return (bool) $this->wpdbAdapter->get_var(
100 + $this->wpdbAdapter->prepare( 'SELECT `is_pickup_points` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `id` = %d', $carrierId )
101 + );
134 102 }
135 103
136 104 /**
137 105 * Gets Carrier value object by id.
@@ -137,182 +105,74 @@
137 105 * Gets Carrier value object by id.
138 106 *
139 107 * @param int $carrierId Carrier id.
140 108 *
141 - * @return Entity\Carrier|null
109 + * @return array<string, string>|null
142 110 */
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',
111 + public function getById( int $carrierId ): ?array {
112 + return $this->wpdbAdapter->get_row(
113 + $this->wpdbAdapter->prepare(
114 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
115 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `id` = %s',
163 116 $carrierId
164 117 ),
165 118 ARRAY_A
166 119 );
167 - if ( null === $result ) {
168 - return null;
169 - }
170 -
171 - return $this->carrierEntityFactory->fromDbResult( $result );
172 120 }
173 121
174 122 /**
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 123 * Gets all active carriers for a country.
199 124 *
200 125 * @param string $country ISO code.
201 126 *
202 - * @return Entity\Carrier[]
127 + * @return array|null
203 128 */
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',
129 + public function getByCountry( string $country ): ?array {
130 + return $this->wpdbAdapter->get_results(
131 + $this->wpdbAdapter->prepare(
132 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
133 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `country` = %s AND `deleted` = false',
226 134 $country
227 135 ),
228 136 ARRAY_A
229 137 );
230 -
231 - foreach ( $countryCarriers as $carrierData ) {
232 - $entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
233 - }
234 -
235 - return $entities;
236 138 }
237 139
238 140 /**
239 141 * Gets all active carriers.
240 142 *
241 - * @return Entity\Carrier[]
143 + * @return array|null
242 144 */
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',
145 + public function getActiveCarriers(): ?array {
146 + return $this->wpdbAdapter->get_results(
147 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
148 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false',
264 149 ARRAY_A
265 150 );
266 -
267 - foreach ( $countryCarriers as $carrierData ) {
268 - $entities[ $carrierData['id'] ] = $this->carrierEntityFactory->fromDbResult( $carrierData );
269 - }
270 -
271 - return $entities;
272 151 }
273 152
274 153 /**
275 - * Gets all active carriers for a country including internal pickup point carriers.
154 + * Gets all carriers.
276 155 *
277 - * @param string $country ISO code.
278 - *
279 - * @return Entity\Carrier[]
156 + * @return array<int, array<string, string|float|bool>>
280 157 */
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 - }
158 + public function getAllRawIndexed(): array {
159 + $unIndexedResult = $this->wpdbAdapter->get_results(
160 + 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
161 + FROM `' . $this->wpdbAdapter->packeteryCarrier . '`',
162 + ARRAY_A
163 + );
290 164
291 - return $countryCarriers;
165 + return array_combine( array_column( $unIndexedResult, 'id' ), $unIndexedResult );
292 166 }
293 167
294 168 /**
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 169 * Tells if there is any active feed carrier.
308 170 *
309 171 * @return bool
310 172 */
311 173 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' );
174 + return (bool) $this->wpdbAdapter->get_var( 'SELECT 1 FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false LIMIT 1' );
315 175 }
316 176
317 177 /**
318 178 * Gets all active countries.
@@ -319,29 +179,20 @@
319 179 *
320 180 * @return array
321 181 */
322 182 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' );
183 + return $this->wpdbAdapter->get_col( 'SELECT `country` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`' );
327 184 }
328 185
329 186 /**
330 - * Set those not in feed as deleted.
187 + * Set carriers specified by ids as deleted.
331 188 *
332 - * @param array $carriers_in_feed Carriers in feed.
189 + * @param array $carrierIdsNotInFeed Carriers not in feed.
333 190 */
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 - . ')'
191 + public function set_as_deleted( array $carrierIdsNotInFeed ): void {
192 + $this->wpdbAdapter->query(
193 + 'UPDATE `' . $this->wpdbAdapter->packeteryCarrier . '`
194 + SET `deleted` = 1 WHERE `id` IN (' . implode( ',', $carrierIdsNotInFeed ) . ')'
344 195 );
345 196 }
346 197
347 198 /**
@@ -349,10 +200,9 @@
349 200 *
350 201 * @param array $data Carrier data.
351 202 */
352 203 public function insert( array $data ): void {
353 - $wpdb = $this->get_wpdb();
354 - $wpdb->insert( $wpdb->packetery_carrier, $data );
204 + $this->wpdbAdapter->insert( $this->wpdbAdapter->packeteryCarrier, $data );
355 205 }
356 206
357 207 /**
358 208 * Updates carrier data in db.
@@ -357,96 +207,10 @@
357 207 /**
358 208 * Updates carrier data in db.
359 209 *
360 210 * @param array $data Carrier data.
361 - * @param int $carrier_id Carrier id.
211 + * @param int $carrierId Carrier id.
362 212 */
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 ) );
213 + public function update( array $data, int $carrierId ): void {
214 + $this->wpdbAdapter->update( $this->wpdbAdapter->packeteryCarrier, $data, [ 'id' => $carrierId ] );
366 215 }
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 216 }