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

243 lines 5.9 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\Module\WpdbAdapter;
13
14 /**
15 * Class CarrierRepository
16 * TODO: cache - some queries may run more times during request.
17 *
18 * @package Packetery
19 */
20 class Repository {
21
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 ];
38
39 /**
40 * WpdbAdapter object from global
41 *
42 * @var WpdbAdapter
43 */
44 private $wpdbAdapter;
45
46 /**
47 * Internal pickup points config.
48 *
49 * @var PacketaPickupPointsConfig
50 */
51 private $pickupPointsConfig;
52
53 /**
54 * Repository constructor.
55 *
56 * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
57 * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config.
58 */
59 public function __construct(
60 WpdbAdapter $wpdbAdapter,
61 PacketaPickupPointsConfig $pickupPointsConfig
62 ) {
63 $this->wpdbAdapter = $wpdbAdapter;
64 $this->pickupPointsConfig = $pickupPointsConfig;
65 }
66
67 /**
68 * Create table to store carriers.
69 *
70 * @return bool
71 */
72 public function createOrAlterTable(): bool {
73 $createTableQuery = 'CREATE TABLE ' . $this->wpdbAdapter->packetery_carrier . ' (
74 `id` int(11) NOT NULL,
75 `name` varchar(255) NOT NULL,
76 `is_pickup_points` tinyint(1) NOT NULL,
77 `has_carrier_direct_label` tinyint(1) NOT NULL,
78 `separate_house_number` tinyint(1) NOT NULL,
79 `customs_declarations` tinyint(1) NOT NULL,
80 `requires_email` tinyint(1) NOT NULL,
81 `requires_phone` tinyint(1) NOT NULL,
82 `requires_size` tinyint(1) NOT NULL,
83 `disallows_cod` tinyint(1) NOT NULL,
84 `country` varchar(255) NOT NULL,
85 `currency` varchar(255) NOT NULL,
86 `max_weight` float NOT NULL,
87 `deleted` tinyint(1) NOT NULL,
88 PRIMARY KEY (`id`)
89 ) ' . $this->wpdbAdapter->get_charset_collate();
90
91 return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packetery_carrier );
92 }
93
94 /**
95 * Drop table used to store carriers.
96 */
97 public function drop(): void {
98 $this->wpdbAdapter->query( 'DROP TABLE IF EXISTS `' . $this->wpdbAdapter->packetery_carrier . '`' );
99 }
100
101 /**
102 * Gets is_pickup_point attribute of a carrier.
103 *
104 * @param int $carrierId Carrier id.
105 *
106 * @return bool
107 */
108 public function hasPickupPoints( int $carrierId ): bool {
109 return (bool) $this->wpdbAdapter->get_var(
110 $this->wpdbAdapter->prepare( 'SELECT `is_pickup_points` FROM `' . $this->wpdbAdapter->packetery_carrier . '` WHERE `id` = %d', $carrierId )
111 );
112 }
113
114 /**
115 * Gets Carrier value object by id.
116 *
117 * @param int $carrierId Carrier id.
118 *
119 * @return array|null
120 */
121 public function getById( int $carrierId ): ?array {
122 return $this->wpdbAdapter->get_row(
123 $this->wpdbAdapter->prepare(
124 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
125 FROM `' . $this->wpdbAdapter->packetery_carrier . '` WHERE `id` = %s',
126 $carrierId
127 ),
128 ARRAY_A
129 );
130 }
131
132 /**
133 * Gets all active carriers for a country.
134 *
135 * @param string $country ISO code.
136 *
137 * @return array|null
138 */
139 public function getByCountry( string $country ): ?array {
140 return $this->wpdbAdapter->get_results(
141 $this->wpdbAdapter->prepare(
142 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
143 FROM `' . $this->wpdbAdapter->packetery_carrier . '` WHERE `country` = %s AND `deleted` = false',
144 $country
145 ),
146 ARRAY_A
147 );
148 }
149
150 /**
151 * Gets all active carriers.
152 *
153 * @return array|null
154 */
155 public function getActiveCarriers(): ?array {
156 return $this->wpdbAdapter->get_results(
157 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
158 FROM `' . $this->wpdbAdapter->packetery_carrier . '` WHERE `deleted` = false',
159 ARRAY_A
160 );
161 }
162
163 /**
164 * Gets all carriers.
165 *
166 * @return array[]
167 */
168 public function getAllRawIndexed(): array {
169 $unIndexedResult = $this->wpdbAdapter->get_results(
170 'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
171 FROM `' . $this->wpdbAdapter->packetery_carrier . '`',
172 ARRAY_A
173 );
174
175 return array_combine( array_column( $unIndexedResult, 'id' ), $unIndexedResult );
176 }
177
178 /**
179 * Tells if there is any active feed carrier.
180 *
181 * @return bool
182 */
183 public function hasAnyActiveFeedCarrier(): bool {
184 return (bool) $this->wpdbAdapter->get_var( 'SELECT 1 FROM `' . $this->wpdbAdapter->packetery_carrier . '` WHERE `deleted` = false LIMIT 1' );
185 }
186
187 /**
188 * Gets all active countries.
189 *
190 * @return array
191 */
192 public function getCountries(): array {
193 return $this->wpdbAdapter->get_col( 'SELECT `country` FROM `' . $this->wpdbAdapter->packetery_carrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`' );
194 }
195
196 /**
197 * Set carriers specified by ids as deleted.
198 *
199 * @param array $carrierIdsNotInFeed Carriers not in feed.
200 */
201 public function set_as_deleted( array $carrierIdsNotInFeed ): void {
202 $this->wpdbAdapter->query(
203 'UPDATE `' . $this->wpdbAdapter->packetery_carrier . '`
204 SET `deleted` = 1 WHERE `id` IN (' . implode( ',', $carrierIdsNotInFeed ) . ')'
205 );
206 }
207
208 /**
209 * Inserts carrier data to db.
210 *
211 * @param array $data Carrier data.
212 */
213 public function insert( array $data ): void {
214 $this->wpdbAdapter->insert( $this->wpdbAdapter->packetery_carrier, $data );
215 }
216
217 /**
218 * Updates carrier data in db.
219 *
220 * @param array $data Carrier data.
221 * @param int $carrier_id Carrier id.
222 */
223 public function update( array $data, int $carrier_id ): void {
224 $this->wpdbAdapter->update( $this->wpdbAdapter->packetery_carrier, $data, [ 'id' => $carrier_id ] );
225 }
226
227 /**
228 * Checks if carrier is home delivery carrier.
229 *
230 * @param string $carrierId Carrier ID.
231 *
232 * @return bool
233 */
234 public function isHomeDeliveryCarrier( string $carrierId ): bool {
235 if ( $this->pickupPointsConfig->isInternalPickupPointCarrier( $carrierId ) ) {
236 return false;
237 }
238
239 return false === $this->hasPickupPoints( (int) $carrierId );
240 }
241
242 }
243