PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
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 +125 -73 1.3.22.3.2 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,48 +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 /**
41 + * Counts records.
42 +
43 + * @param int|null $orderId Order ID.
44 + * @param string|null $action Action.
45 + *
46 + * @return int
47 + */
48 + public function countRows( ?int $orderId, ?string $action ): int {
49 + $whereClause = $this->getWhereClause( [], $orderId, $action );
50 +
51 + return (int) $this->wpdbAdapter->get_var( 'SELECT COUNT(*) FROM `' . $this->wpdbAdapter->packeteryLog . '`' . $whereClause );
52 + }
53 +
54 + /**
40 55 * Finds logs.
41 56 *
42 - * @param array $arguments Search arguments.
57 + * @param array<string, string|int|bool|float|null|array<string,mixed>> $arguments Search arguments.
43 58 *
44 - * @return iterable|Record[]
59 + * @return \Generator<Record>|array{}
45 60 * @throws \Exception From DateTimeImmutable.
46 61 */
47 - public function find( array $arguments ): iterable {
48 - $wpdb = $this->wpdb;
62 + public function find( array $arguments ) {
63 + $orderId = $arguments['order_id'] ?? null;
64 + $action = $arguments['action'] ?? null;
49 65 $orderBy = $arguments['orderby'] ?? [];
50 66 $limit = $arguments['limit'] ?? null;
51 67 $dateQuery = $arguments['date_query'] ?? [];
52 68
53 69 $orderByTransformed = [];
54 - foreach ( $orderBy as $orderByKey => $orderByValue ) {
55 - if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) {
56 - $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;
57 77 }
58 -
59 - $orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue;
60 78 }
61 79
62 80 $orderByClause = '';
63 - if ( $orderByTransformed ) {
81 + if ( count( $orderByTransformed ) > 0 ) {
64 82 $orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed );
65 83 }
66 84
67 85 $limitClause = '';
@@ -69,21 +87,19 @@
69 87 $limitClause = ' LIMIT ' . $limit;
70 88 }
71 89
72 90 $where = [];
73 - foreach ( $dateQuery as $dateQueryItem ) {
74 - if ( isset( $dateQueryItem['after'] ) ) {
75 - $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 + }
76 96 }
77 97 }
78 98
79 - $whereClause = '';
80 - if ( $where ) {
81 - $whereClause = ' WHERE ' . implode( ' AND ', $where );
82 - }
99 + $whereClause = $this->getWhereClause( $where, $orderId, $action );
83 100
84 - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
85 - $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 );
86 102 if ( is_iterable( $result ) ) {
87 103 return $this->remapToRecord( $result );
88 104 }
89 105
@@ -90,20 +106,34 @@
90 106 return [];
91 107 }
92 108
93 109 /**
110 + * Delete old records.
111 + *
112 + * @param string $before DateTime modifier.
113 + *
114 + * @return void
115 + */
116 + public function deleteOld( string $before ): void {
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 + );
121 + }
122 +
123 + /**
94 124 * Remaps logs.
95 125 *
96 - * @param iterable $logs Logs.
126 + * @param array $logs Logs.
97 127 *
98 - * @return \Generator|Record[]
128 + * @return \Generator<Record>
99 129 */
100 - public function remapToRecord( iterable $logs ): \Generator {
130 + public function remapToRecord( array $logs ): \Generator {
101 131 foreach ( $logs as $log ) {
102 132 $record = new Record();
103 133 $record->id = $log->id;
104 134 $record->status = $log->status;
105 - $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' ) )
106 136 ->setTimezone( wp_timezone() );
107 137 $record->action = $log->action;
108 138 $record->title = $log->title;
109 139
@@ -112,8 +142,12 @@
112 142 } else {
113 143 $record->params = [];
114 144 }
115 145
146 + if ( ! is_array( $record->params ) ) {
147 + $record->params = [];
148 + }
149 +
116 150 $record->note = $this->getNote( $record->title, $record->params );
117 151
118 152 yield $record;
119 153 }
@@ -132,9 +166,9 @@
132 166 ' ',
133 167 array_filter(
134 168 [
135 169 $title,
136 - ( $params ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ),
170 + ( count( $params ) > 0 ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ),
137 171 ]
138 172 )
139 173 );
140 174 }
@@ -141,37 +175,23 @@
141 175
142 176 /**
143 177 * Creates log table.
144 178 *
145 - * @return void
179 + * @return bool
146 180 */
147 - public function createTable(): void {
148 - $wpdb = $this->wpdb;
149 - $wpdb->query(
150 - '
151 - CREATE TABLE IF NOT EXISTS `' . $wpdb->packetery_log . "` (
152 - `id` INT(11) NOT NULL AUTO_INCREMENT,
153 - `title` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
154 - `params` TEXT NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
155 - `status` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
156 - `action` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
157 - `date` DATETIME NOT NULL,
158 - PRIMARY KEY (`id`) USING BTREE
159 - )
160 - COLLATE='utf8_general_ci'
161 - ENGINE=InnoDB
162 - "
163 - );
164 - }
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();
165 192
166 - /**
167 - * Drops log table.
168 - *
169 - * @return void
170 - */
171 - public function drop(): void {
172 - $wpdb = $this->wpdb;
173 - $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->packetery_log . '`' );
193 + return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryLog );
174 194 }
175 195
176 196 /**
177 197 * Save.
@@ -177,32 +197,64 @@
177 197 * Save.
178 198 *
179 199 * @param Record $record Record.
180 200 *
181 - * @return void
201 + * @return int|false The number of rows updated, or false on error.
182 202 * @throws \Exception From DateTimeImmutable.
183 203 */
184 - public function save( Record $record ): void {
204 + public function save( Record $record ) {
185 205 $date = $record->date;
186 - if ( null === $date ) {
187 - $date = Helper::now();
206 + if ( $date === null ) {
207 + $date = CoreHelper::now();
188 208 }
189 209
190 - $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT );
210 + $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( CoreHelper::MYSQL_DATETIME_FORMAT );
191 211
192 212 $paramsString = '';
193 - if ( $record->params ) {
194 - $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 );
195 216 }
196 217
218 + $orderId = $record->orderId;
219 + if ( is_numeric( $orderId ) ) {
220 + $orderId = (int) $orderId;
221 + }
222 +
197 223 $data = [
198 - 'id' => $record->id,
199 - 'title' => ( $record->title ?? '' ),
200 - 'status' => ( $record->status ?? '' ),
201 - 'action' => ( $record->action ?? '' ),
202 - 'params' => $paramsString,
203 - 'date' => $dateString,
224 + 'id' => $record->id,
225 + 'order_id' => $orderId,
226 + 'title' => $record->title,
227 + 'status' => $record->status,
228 + 'action' => $record->action,
229 + 'params' => $paramsString,
230 + 'date' => $dateString,
204 231 ];
205 232
206 - $this->wpdb->_insert_replace_helper( $this->wpdb->packetery_log, $data, null, 'REPLACE' );
233 + return $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;
207 259 }
208 260 }