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

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