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

216 lines 5.5 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 'available',
38 ];
39
40 /**
41 * WpdbAdapter object from global
42 *
43 * @var WpdbAdapter
44 */
45 private $wpdbAdapter;
46
47 /**
48 * Repository constructor.
49 *
50 * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
51 */
52 public function __construct(
53 WpdbAdapter $wpdbAdapter
54 ) {
55 $this->wpdbAdapter = $wpdbAdapter;
56 }
57
58 /**
59 * Create table to store carriers.
60 *
61 * @return bool
62 */
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();
82
83 return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryCarrier );
84 }
85
86 /**
87 * Gets is_pickup_point attribute of a carrier.
88 *
89 * @param int $carrierId Carrier id.
90 *
91 * @return bool
92 */
93 public function hasPickupPoints( int $carrierId ): bool {
94 return (bool) $this->wpdbAdapter->get_var(
95 $this->wpdbAdapter->prepare( 'SELECT `is_pickup_points` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `id` = %d', $carrierId )
96 );
97 }
98
99 /**
100 * Gets Carrier value object by id.
101 *
102 * @param int $carrierId Carrier id.
103 *
104 * @return array<string, string>|null
105 */
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',
111 $carrierId
112 ),
113 ARRAY_A
114 );
115 }
116
117 /**
118 * Gets all active carriers for a country.
119 *
120 * @param string $country ISO code.
121 * @param bool $includeUnavailable Include unavailable carriers.
122 *
123 * @return array|null
124 */
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' ),
131 $country
132 ),
133 ARRAY_A
134 );
135 }
136
137 /**
138 * Gets all active carriers.
139 *
140 * @return array|null
141 */
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 );
148 }
149
150 /**
151 * Gets all carriers.
152 *
153 * @return array<int, array<string, string|float|bool>>
154 */
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 );
161
162 return array_combine( array_column( $unIndexedResult, 'id' ), $unIndexedResult );
163 }
164
165 /**
166 * Tells if there is any active feed carrier.
167 *
168 * @return bool
169 */
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' );
172 }
173
174 /**
175 * Gets all active countries.
176 *
177 * @return array
178 */
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`' );
181 }
182
183 /**
184 * Set carriers specified by ids as deleted.
185 *
186 * @param array $carrierIdsNotInFeed Carriers not in feed.
187 */
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 );
193 }
194
195 /**
196 * Inserts carrier data to db.
197 *
198 * @param array $data Carrier data.
199 * @return int|false The number of rows inserted, or false on error.
200 */
201 public function insert( array $data ) {
202 return $this->wpdbAdapter->insert( $this->wpdbAdapter->packeteryCarrier, $data );
203 }
204
205 /**
206 * Updates carrier data in db.
207 *
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.
211 */
212 public function update( array $data, int $carrierId ) {
213 return $this->wpdbAdapter->update( $this->wpdbAdapter->packeteryCarrier, $data, [ 'id' => $carrierId ] );
214 }
215 }
216