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
packeta / src / Packetery / Module / Carrier / Repository.php

Repository.php in Packeta 2.0.9, at src/Packetery/Module/Carrier/Repository.php

217 lines 5.3 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 * Repository constructor.
48 *
49 * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
50 */
51 public function __construct(
52 WpdbAdapter $wpdbAdapter
53 ) {
54 $this->wpdbAdapter = $wpdbAdapter;
55 }
56
57 /**
58 * Create table to store carriers.
59 *
60 * @return bool
61 */
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 );
82 }
83
84 /**
85 * Drop table used to store carriers.
86 */
87 public function drop(): void {
88 $this->wpdbAdapter->query( 'DROP TABLE IF EXISTS `' . $this->wpdbAdapter->packeteryCarrier . '`' );
89 }
90
91 /**
92 * Gets is_pickup_point attribute of a carrier.
93 *
94 * @param int $carrierId Carrier id.
95 *
96 * @return bool
97 */
98 public function hasPickupPoints( int $carrierId ): bool {
99 return (bool) $this->wpdbAdapter->get_var(
100 $this->wpdbAdapter->prepare( 'SELECT `is_pickup_points` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `id` = %d', $carrierId )
101 );
102 }
103
104 /**
105 * Gets Carrier value object by id.
106 *
107 * @param int $carrierId Carrier id.
108 *
109 * @return array<string, string>|null
110 */
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',
116 $carrierId
117 ),
118 ARRAY_A
119 );
120 }
121
122 /**
123 * Gets all active carriers for a country.
124 *
125 * @param string $country ISO code.
126 *
127 * @return array|null
128 */
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',
134 $country
135 ),
136 ARRAY_A
137 );
138 }
139
140 /**
141 * Gets all active carriers.
142 *
143 * @return array|null
144 */
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',
149 ARRAY_A
150 );
151 }
152
153 /**
154 * Gets all carriers.
155 *
156 * @return array<int, array<string, string|float|bool>>
157 */
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 );
164
165 return array_combine( array_column( $unIndexedResult, 'id' ), $unIndexedResult );
166 }
167
168 /**
169 * Tells if there is any active feed carrier.
170 *
171 * @return bool
172 */
173 public function hasAnyActiveFeedCarrier(): bool {
174 return (bool) $this->wpdbAdapter->get_var( 'SELECT 1 FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false LIMIT 1' );
175 }
176
177 /**
178 * Gets all active countries.
179 *
180 * @return array
181 */
182 public function getCountries(): array {
183 return $this->wpdbAdapter->get_col( 'SELECT `country` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`' );
184 }
185
186 /**
187 * Set carriers specified by ids as deleted.
188 *
189 * @param array $carrierIdsNotInFeed Carriers not in feed.
190 */
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 ) . ')'
195 );
196 }
197
198 /**
199 * Inserts carrier data to db.
200 *
201 * @param array $data Carrier data.
202 */
203 public function insert( array $data ): void {
204 $this->wpdbAdapter->insert( $this->wpdbAdapter->packeteryCarrier, $data );
205 }
206
207 /**
208 * Updates carrier data in db.
209 *
210 * @param array $data Carrier data.
211 * @param int $carrierId Carrier id.
212 */
213 public function update( array $data, int $carrierId ): void {
214 $this->wpdbAdapter->update( $this->wpdbAdapter->packeteryCarrier, $data, [ 'id' => $carrierId ] );
215 }
216 }
217