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
← All changes | src/Packetery/Module/Log/Repository.php +95 -94 1.4.22.0.9 View file →
@@ -6,13 +6,14 @@
6 6 */
7 7
8 8 declare( strict_types=1 );
9 9
10 -
11 10 namespace Packetery\Module\Log;
12 11
13 -use Packetery\Core\Helper;
12 +use Packetery\Core\CoreHelper;
14 13 use Packetery\Core\Log\Record;
14 +use Packetery\Module\ModuleHelper;
15 +use Packetery\Module\WpdbAdapter;
15 16
16 17 /**
17 18 * Class Repository
18 19 *
@@ -20,73 +21,65 @@
20 21 */
21 22 class Repository {
22 23
23 24 /**
24 - * WPDB.
25 + * WpdbAdapter.
25 26 *
26 - * @var \wpdb
27 + * @var WpdbAdapter
27 28 */
28 - private $wpdb;
29 + private $wpdbAdapter;
29 30
30 31 /**
31 32 * Constructor.
32 33 *
33 - * @param \wpdb $wpdb WPDB.
34 + * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
34 35 */
35 - public function __construct( \wpdb $wpdb ) {
36 - $this->wpdb = $wpdb;
36 + public function __construct( WpdbAdapter $wpdbAdapter ) {
37 + $this->wpdbAdapter = $wpdbAdapter;
37 38 }
38 39
39 40 /**
40 41 * Counts records.
41 - *
42 - * @param int $orderId Order ID.
43 - *
44 - * @return int
45 - */
46 - public function countByOrderId( int $orderId ): int {
47 - $wpdb = $this->wpdb;
48 42
49 - return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM `' . $wpdb->packetery_log . '` WHERE `order_id` = %d', $orderId ) );
50 - }
51 -
52 - /**
53 - * Counts records.
43 + * @param int|null $orderId Order ID.
44 + * @param string|null $action Action.
54 45 *
55 46 * @return int
56 47 */
57 - public function countAll(): int {
58 - $wpdb = $this->wpdb;
48 + public function countRows( ?int $orderId, ?string $action ): int {
49 + $whereClause = $this->getWhereClause( [], $orderId, $action );
59 50
60 - return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM `' . $wpdb->packetery_log . '`' );
51 + return (int) $this->wpdbAdapter->get_var( 'SELECT COUNT(*) FROM `' . $this->wpdbAdapter->packeteryLog . '`' . $whereClause );
61 52 }
62 53
63 54 /**
64 55 * Finds logs.
65 56 *
66 - * @param array $arguments Search arguments.
57 + * @param array<string, string|int|bool|float|null|array<string,mixed>> $arguments Search arguments.
67 58 *
68 - * @return iterable|Record[]
59 + * @return \Generator<Record>|array{}
69 60 * @throws \Exception From DateTimeImmutable.
70 61 */
71 - public function find( array $arguments ): iterable {
72 - $wpdb = $this->wpdb;
62 + public function find( array $arguments ) {
73 63 $orderId = $arguments['order_id'] ?? null;
64 + $action = $arguments['action'] ?? null;
74 65 $orderBy = $arguments['orderby'] ?? [];
75 66 $limit = $arguments['limit'] ?? null;
76 67 $dateQuery = $arguments['date_query'] ?? [];
77 68
78 69 $orderByTransformed = [];
79 - foreach ( $orderBy as $orderByKey => $orderByValue ) {
80 - if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) {
81 - $orderByValue = 'ASC';
70 + if ( count( $orderBy ) > 0 ) {
71 + foreach ( $orderBy as $orderByKey => $orderByValue ) {
72 + if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) {
73 + $orderByValue = 'ASC';
74 + }
75 +
76 + $orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue;
82 77 }
83 -
84 - $orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue;
85 78 }
86 79
87 80 $orderByClause = '';
88 - if ( $orderByTransformed ) {
81 + if ( count( $orderByTransformed ) > 0 ) {
89 82 $orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed );
90 83 }
91 84
92 85 $limitClause = '';
@@ -94,25 +87,19 @@
94 87 $limitClause = ' LIMIT ' . $limit;
95 88 }
96 89
97 90 $where = [];
98 - foreach ( $dateQuery as $dateQueryItem ) {
99 - if ( isset( $dateQueryItem['after'] ) ) {
100 - $where[] = $wpdb->prepare( '`date` > %s', Helper::now()->modify( $dateQueryItem['after'] )->format( Helper::MYSQL_DATETIME_FORMAT ) );
91 + if ( count( $dateQuery ) > 0 ) {
92 + foreach ( $dateQuery as $dateQueryItem ) {
93 + if ( isset( $dateQueryItem['after'] ) ) {
94 + $where[] = $this->wpdbAdapter->prepare( '`date` > %s', CoreHelper::now()->modify( $dateQueryItem['after'] )->format( CoreHelper::MYSQL_DATETIME_FORMAT ) );
95 + }
101 96 }
102 97 }
103 98
104 - if ( is_numeric( $orderId ) ) {
105 - $where[] = $wpdb->prepare( '`order_id` = %d', $orderId );
106 - }
99 + $whereClause = $this->getWhereClause( $where, $orderId, $action );
107 100
108 - $whereClause = '';
109 - if ( $where ) {
110 - $whereClause = ' WHERE ' . implode( ' AND ', $where );
111 - }
112 -
113 - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
114 - $result = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->packetery_log . $whereClause . $orderByClause . $limitClause );
101 + $result = $this->wpdbAdapter->get_results( 'SELECT * FROM `' . $this->wpdbAdapter->packeteryLog . '` ' . $whereClause . $orderByClause . $limitClause );
115 102 if ( is_iterable( $result ) ) {
116 103 return $this->remapToRecord( $result );
117 104 }
118 105
@@ -126,27 +113,27 @@
126 113 *
127 114 * @return void
128 115 */
129 116 public function deleteOld( string $before ): void {
130 - $wpdb = $this->wpdb;
131 - $dateToFormatted = Helper::now()->modify( $before )->format( Helper::MYSQL_DATETIME_FORMAT );
132 -
133 - $wpdb->query( $wpdb->prepare( 'DELETE FROM `' . $wpdb->packetery_log . '` WHERE `date` < %s', $dateToFormatted ) );
117 + $dateToFormatted = CoreHelper::now()->modify( $before )->format( CoreHelper::MYSQL_DATETIME_FORMAT );
118 + $this->wpdbAdapter->query(
119 + $this->wpdbAdapter->prepare( 'DELETE FROM `' . $this->wpdbAdapter->packeteryLog . '` WHERE `date` < %s', $dateToFormatted )
120 + );
134 121 }
135 122
136 123 /**
137 124 * Remaps logs.
138 125 *
139 - * @param iterable $logs Logs.
126 + * @param array $logs Logs.
140 127 *
141 - * @return \Generator|Record[]
128 + * @return \Generator<Record>
142 129 */
143 - public function remapToRecord( iterable $logs ): \Generator {
130 + public function remapToRecord( array $logs ): \Generator {
144 131 foreach ( $logs as $log ) {
145 132 $record = new Record();
146 133 $record->id = $log->id;
147 134 $record->status = $log->status;
148 - $record->date = \DateTimeImmutable::createFromFormat( Helper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) )
135 + $record->date = \DateTimeImmutable::createFromFormat( CoreHelper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) )
149 136 ->setTimezone( wp_timezone() );
150 137 $record->action = $log->action;
151 138 $record->title = $log->title;
152 139
@@ -155,8 +142,12 @@
155 142 } else {
156 143 $record->params = [];
157 144 }
158 145
146 + if ( ! is_array( $record->params ) ) {
147 + $record->params = [];
148 + }
149 +
159 150 $record->note = $this->getNote( $record->title, $record->params );
160 151
161 152 yield $record;
162 153 }
@@ -175,9 +166,9 @@
175 166 ' ',
176 167 array_filter(
177 168 [
178 169 $title,
179 - ( $params ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ),
170 + ( count( $params ) > 0 ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ),
180 171 ]
181 172 )
182 173 );
183 174 }
@@ -184,38 +175,23 @@
184 175
185 176 /**
186 177 * Creates log table.
187 178 *
188 - * @return void
179 + * @return bool
189 180 */
190 - public function createTable(): void {
191 - $wpdb = $this->wpdb;
192 - $wpdb->query(
193 - '
194 - CREATE TABLE IF NOT EXISTS `' . $wpdb->packetery_log . "` (
195 - `id` INT(11) NOT NULL AUTO_INCREMENT,
196 - `order_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
197 - `title` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
198 - `params` TEXT NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
199 - `status` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
200 - `action` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
201 - `date` DATETIME NOT NULL,
202 - PRIMARY KEY (`id`) USING BTREE
203 - )
204 - COLLATE='utf8_general_ci'
205 - ENGINE=InnoDB
206 - "
207 - );
208 - }
181 + public function createOrAlterTable(): bool {
182 + $createTableQuery = 'CREATE TABLE ' . $this->wpdbAdapter->packeteryLog . " (
183 + `id` int(11) NOT NULL AUTO_INCREMENT,
184 + `order_id` bigint(20) unsigned NULL,
185 + `title` varchar(255) NOT NULL DEFAULT '',
186 + `params` text NOT NULL,
187 + `status` varchar(255) NOT NULL DEFAULT '',
188 + `action` varchar(255) NOT NULL DEFAULT '',
189 + `date` datetime NOT NULL,
190 + PRIMARY KEY (`id`)
191 + ) " . $this->wpdbAdapter->get_charset_collate();
209 192
210 - /**
211 - * Adds order id column.
212 - *
213 - * @return void
214 - */
215 - public function addOrderIdColumn(): void {
216 - $wpdb = $this->wpdb;
217 - $wpdb->query( 'ALTER TABLE `' . $wpdb->packetery_log . '` ADD COLUMN `order_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL AFTER `id`' );
193 + return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryLog );
218 194 }
219 195
220 196 /**
221 197 * Drops log table.
@@ -222,10 +198,9 @@
222 198 *
223 199 * @return void
224 200 */
225 201 public function drop(): void {
226 - $wpdb = $this->wpdb;
227 - $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->packetery_log . '`' );
202 + $this->wpdbAdapter->query( 'DROP TABLE IF EXISTS `' . $this->wpdbAdapter->packeteryLog . '`' );
228 203 }
229 204
230 205 /**
231 206 * Save.
@@ -236,17 +211,18 @@
236 211 * @throws \Exception From DateTimeImmutable.
237 212 */
238 213 public function save( Record $record ): void {
239 214 $date = $record->date;
240 - if ( null === $date ) {
241 - $date = Helper::now();
215 + if ( $date === null ) {
216 + $date = CoreHelper::now();
242 217 }
243 218
244 - $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT );
219 + $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( CoreHelper::MYSQL_DATETIME_FORMAT );
245 220
246 221 $paramsString = '';
247 - if ( $record->params ) {
248 - $paramsString = wp_json_encode( $record->params );
222 + if ( $record->params !== null && count( $record->params ) > 0 ) {
223 + $params = ModuleHelper::convertArrayFloatsToStrings( $record->params );
224 + $paramsString = wp_json_encode( $params );
249 225 }
250 226
251 227 $orderId = $record->orderId;
252 228 if ( is_numeric( $orderId ) ) {
@@ -255,14 +231,39 @@
255 231
256 232 $data = [
257 233 'id' => $record->id,
258 234 'order_id' => $orderId,
259 - 'title' => ( $record->title ?? '' ),
260 - 'status' => ( $record->status ?? '' ),
261 - 'action' => ( $record->action ?? '' ),
235 + 'title' => $record->title,
236 + 'status' => $record->status,
237 + 'action' => $record->action,
262 238 'params' => $paramsString,
263 239 'date' => $dateString,
264 240 ];
265 241
266 - $this->wpdb->_insert_replace_helper( $this->wpdb->packetery_log, $data, null, 'REPLACE' );
242 + $this->wpdbAdapter->insertReplaceHelper( $this->wpdbAdapter->packeteryLog, $data, null, 'REPLACE' );
243 + }
244 +
245 + /**
246 + * Gets where clause for find and count queries.
247 + *
248 + * @param array $where Conditions.
249 + * @param int|null $orderId Order id.
250 + * @param string|null $action Action.
251 + *
252 + * @return string
253 + */
254 + private function getWhereClause( array $where, ?int $orderId, ?string $action ): string {
255 + if ( is_numeric( $orderId ) ) {
256 + $where[] = $this->wpdbAdapter->prepare( '`order_id` = %d', $orderId );
257 + }
258 + if ( $action !== null ) {
259 + $where[] = $this->wpdbAdapter->prepare( '`action` = %s', $action );
260 + }
261 +
262 + $whereClause = '';
263 + if ( count( $where ) > 0 ) {
264 + $whereClause = ' WHERE ' . implode( ' AND ', $where );
265 + }
266 +
267 + return $whereClause;
267 268 }
268 269 }