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 +106 -241 1.2.32.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,109 +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 87 * Gets is_pickup_point attribute of a carrier.
125 88 *
126 89 * @param int $carrierId Carrier id.
127 90 *
@@ -127,11 +90,11 @@
127 90 *
128 91 * @return bool
129 92 */
130 93 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 ) );
94 + return (bool) $this->wpdbAdapter->get_var(
95 + $this->wpdbAdapter->prepare( 'SELECT `is_pickup_points` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `id` = %d', $carrierId )
96 + );
134 97 }
135 98
136 99 /**
137 100 * Gets Carrier value object by id.
@@ -137,39 +100,19 @@
137 100 * Gets Carrier value object by id.
138 101 *
139 102 * @param int $carrierId Carrier id.
140 103 *
141 - * @return Entity\Carrier|null
104 + * @return array<string, string>|null
142 105 */
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',
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',
163 111 $carrierId
164 112 ),
165 113 ARRAY_A
166 114 );
167 - if ( null === $result ) {
168 - return null;
169 - }
170 -
171 - return $this->carrierEntityFactory->fromDbResult( $result );
172 115 }
173 116
174 117 /**
175 118 * Gets all active carriers for a country.
@@ -174,177 +117,99 @@
174 117 /**
175 118 * Gets all active carriers for a country.
176 119 *
177 120 * @param string $country ISO code.
121 + * @param bool $includeUnavailable Include unavailable carriers.
178 122 *
179 - * @return Entity\Carrier[]
123 + * @return array|null
180 124 */
181 - public function getByCountry( string $country ): array {
182 - $wpdb = $this->get_wpdb();
183 -
184 - $entities = [];
185 - $countryCarriers = $wpdb->get_results(
186 - $wpdb->prepare(
187 - 'SELECT
188 - `id`,
189 - `name`,
190 - `is_pickup_points`,
191 - `has_carrier_direct_label`,
192 - `separate_house_number`,
193 - `customs_declarations`,
194 - `requires_email`,
195 - `requires_phone`,
196 - `requires_size`,
197 - `disallows_cod`,
198 - `country`,
199 - `currency`,
200 - `max_weight`,
201 - `deleted`
202 - 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' ),
203 131 $country
204 132 ),
205 133 ARRAY_A
206 134 );
207 -
208 - foreach ( $countryCarriers as $carrierData ) {
209 - $entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
210 - }
211 -
212 - return $entities;
213 135 }
214 136
215 137 /**
216 - * Gets all active carriers for a country including internal pickup point carriers.
138 + * Gets all active carriers.
217 139 *
218 - * @param string $country ISO code.
219 - *
220 - * @return Entity\Carrier[]
140 + * @return array|null
221 141 */
222 - public function getByCountryIncludingZpoints( string $country ): array {
223 - $countryCarriers = $this->getByCountry( $country );
224 - $zpointCarriers = $this->getZpointCarriers();
225 - if ( ! empty( $zpointCarriers[ $country ] ) ) {
226 - $zpointCarrierData = $zpointCarriers[ $country ];
227 - $zpointCarrierData['country'] = $country;
228 - $zpointCarrier = $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrierData );
229 - array_unshift( $countryCarriers, $zpointCarrier );
230 - }
231 -
232 - return $countryCarriers;
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',
146 + ARRAY_A
147 + );
233 148 }
234 149
235 150 /**
236 - * Gets all active countries.
151 + * Gets all carriers.
237 152 *
238 - * @return array
153 + * @return array<int, array<string, string|float|bool>>
239 154 */
240 - public function getCountries(): array {
241 - $wpdb = $this->get_wpdb();
242 - $countries = $wpdb->get_results( 'SELECT `country` FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`', ARRAY_A );
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 + );
243 161
244 - return array_column( ( $countries ? $countries : [] ), 'country' );
162 + return array_combine( array_column( $unIndexedResult, 'id' ), $unIndexedResult );
245 163 }
246 164
247 165 /**
248 - * Set those not in feed as deleted.
166 + * Tells if there is any active feed carrier.
249 167 *
250 - * @param array $carriers_in_feed Carriers in feed.
168 + * @return bool
251 169 */
252 - public function set_others_as_deleted( array $carriers_in_feed ): void {
253 - $wpdb = $this->get_wpdb();
254 - $wpdb->query(
255 - 'UPDATE `' .
256 - $wpdb->packetery_carrier .
257 - '` SET `deleted` = 1 WHERE `id` NOT IN (' .
258 - // @codingStandardsIgnoreStart
259 - implode( ',', $carriers_in_feed )
260 - // @codingStandardsIgnoreEnd
261 - . ')'
262 - );
170 + public function hasAnyActiveFeedCarrier(): bool {
171 + return (bool) $this->wpdbAdapter->get_var( 'SELECT 1 FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false AND `available` = true LIMIT 1' );
263 172 }
264 173
265 174 /**
266 - * Inserts carrier data to db.
175 + * Gets all active countries.
267 176 *
268 - * @param array $data Carrier data.
177 + * @return array
269 178 */
270 - public function insert( array $data ): void {
271 - $wpdb = $this->get_wpdb();
272 - $wpdb->insert( $wpdb->packetery_carrier, $data );
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`' );
273 181 }
274 182
275 183 /**
276 - * Updates carrier data in db.
184 + * Set carriers specified by ids as deleted.
277 185 *
278 - * @param array $data Carrier data.
279 - * @param int $carrier_id Carrier id.
186 + * @param array $carrierIdsNotInFeed Carriers not in feed.
280 187 */
281 - public function update( array $data, int $carrier_id ): void {
282 - $wpdb = $this->get_wpdb();
283 - $wpdb->update( $wpdb->packetery_carrier, $data, array( 'id' => $carrier_id ) );
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 ) . ')'
192 + );
284 193 }
285 194
286 195 /**
287 - * Returns internal pickup points configuration
196 + * Inserts carrier data to db.
288 197 *
289 - * @return array[]
198 + * @param array $data Carrier data.
199 + * @return int|false The number of rows inserted, or false on error.
290 200 */
291 - public function getZpointCarriers(): array {
292 - return [
293 - 'cz' => [
294 - 'id' => 'zpointcz',
295 - 'name' => __( 'CZ Packeta pickup points', 'packetery' ),
296 - 'is_pickup_points' => 1,
297 - 'currency' => 'CZK',
298 - ],
299 - 'sk' => [
300 - 'id' => 'zpointsk',
301 - 'name' => __( 'SK Packeta pickup points', 'packetery' ),
302 - 'is_pickup_points' => 1,
303 - 'currency' => 'EUR',
304 - ],
305 - 'hu' => [
306 - 'id' => 'zpointhu',
307 - 'name' => __( 'HU Packeta pickup points', 'packetery' ),
308 - 'is_pickup_points' => 1,
309 - 'currency' => 'HUF',
310 - ],
311 - 'ro' => [
312 - 'id' => 'zpointro',
313 - 'name' => __( 'RO Packeta pickup points', 'packetery' ),
314 - 'is_pickup_points' => 1,
315 - 'currency' => 'RON',
316 - ],
317 - ];
201 + public function insert( array $data ) {
202 + return $this->wpdbAdapter->insert( $this->wpdbAdapter->packeteryCarrier, $data );
318 203 }
319 204
320 205 /**
321 - * Checks if chosen carrier has pickup points and sets carrier id in provided array.
206 + * Updates carrier data in db.
322 207 *
323 - * @param string $carrierId Carrier id.
324 - *
325 - * @return bool
208 + * @param array $data Carrier data.
209 + * @param int $carrierId Carrier id.
210 + * @return int|false The number of rows updated, or false on error.
326 211 */
327 - public function isPickupPointCarrier( string $carrierId ): bool {
328 - if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) {
329 - return true;
330 - }
331 -
332 - return $this->hasPickupPoints( (int) $carrierId );
212 + public function update( array $data, int $carrierId ) {
213 + return $this->wpdbAdapter->update( $this->wpdbAdapter->packeteryCarrier, $data, [ 'id' => $carrierId ] );
333 214 }
334 -
335 - /**
336 - * Checks if carrier is home delivery carrier.
337 - *
338 - * @param string $carrierId Carrier ID.
339 - *
340 - * @return bool
341 - */
342 - public function isHomeDeliveryCarrier( string $carrierId ): bool {
343 - if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) {
344 - return false;
345 - }
346 -
347 - return false === $this->hasPickupPoints( (int) $carrierId );
348 - }
349 -
350 215 }