PluginProbe
Packeta / 2.1
Packeta v2.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
← All changes | src/Packetery/Module/Log/Repository.php +94 -102 1.42.1 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,51 +175,26 @@
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 - * Drops log table.
222 - *
223 - * @return void
224 - */
225 - public function drop(): void {
226 - $wpdb = $this->wpdb;
227 - $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->packetery_log . '`' );
228 - }
229 -
230 - /**
231 197 * Save.
232 198 *
233 199 * @param Record $record Record.
234 200 *
@@ -236,17 +202,18 @@
236 202 * @throws \Exception From DateTimeImmutable.
237 203 */
238 204 public function save( Record $record ): void {
239 205 $date = $record->date;
240 - if ( null === $date ) {
241 - $date = Helper::now();
206 + if ( $date === null ) {
207 + $date = CoreHelper::now();
242 208 }
243 209
244 - $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT );
210 + $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( CoreHelper::MYSQL_DATETIME_FORMAT );
245 211
246 212 $paramsString = '';
247 - if ( $record->params ) {
248 - $paramsString = wp_json_encode( $record->params );
213 + if ( $record->params !== null && count( $record->params ) > 0 ) {
214 + $params = ModuleHelper::convertArrayFloatsToStrings( $record->params );
215 + $paramsString = wp_json_encode( $params );
249 216 }
250 217
251 218 $orderId = $record->orderId;
252 219 if ( is_numeric( $orderId ) ) {
@@ -255,14 +222,39 @@
255 222
256 223 $data = [
257 224 'id' => $record->id,
258 225 'order_id' => $orderId,
259 - 'title' => ( $record->title ?? '' ),
260 - 'status' => ( $record->status ?? '' ),
261 - 'action' => ( $record->action ?? '' ),
226 + 'title' => $record->title,
227 + 'status' => $record->status,
228 + 'action' => $record->action,
262 229 'params' => $paramsString,
263 230 'date' => $dateString,
264 231 ];
265 232
266 - $this->wpdb->_insert_replace_helper( $this->wpdb->packetery_log, $data, null, 'REPLACE' );
233 + $this->wpdbAdapter->insertReplaceHelper( $this->wpdbAdapter->packeteryLog, $data, null, 'REPLACE' );
234 + }
235 +
236 + /**
237 + * Gets where clause for find and count queries.
238 + *
239 + * @param array $where Conditions.
240 + * @param int|null $orderId Order id.
241 + * @param string|null $action Action.
242 + *
243 + * @return string
244 + */
245 + private function getWhereClause( array $where, ?int $orderId, ?string $action ): string {
246 + if ( is_numeric( $orderId ) ) {
247 + $where[] = $this->wpdbAdapter->prepare( '`order_id` = %d', $orderId );
248 + }
249 + if ( $action !== null ) {
250 + $where[] = $this->wpdbAdapter->prepare( '`action` = %s', $action );
251 + }
252 +
253 + $whereClause = '';
254 + if ( count( $where ) > 0 ) {
255 + $whereClause = ' WHERE ' . implode( ' AND ', $where );
256 + }
257 +
258 + return $whereClause;
267 259 }
268 260 }