PluginProbe
Packeta / 1.4.2
Packeta v1.4.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
packeta / src / Packetery / Module / Log / Repository.php

Repository.php in Packeta 1.4.2, at src/Packetery/Module/Log/Repository.php

269 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Page
4 *
5 * @package Packetery\Module\Log
6 */
7
8 declare( strict_types=1 );
9
10
11 namespace Packetery\Module\Log;
12
13 use Packetery\Core\Helper;
14 use Packetery\Core\Log\Record;
15
16 /**
17 * Class Repository
18 *
19 * @package Packetery\Module\Log
20 */
21 class Repository {
22
23 /**
24 * WPDB.
25 *
26 * @var \wpdb
27 */
28 private $wpdb;
29
30 /**
31 * Constructor.
32 *
33 * @param \wpdb $wpdb WPDB.
34 */
35 public function __construct( \wpdb $wpdb ) {
36 $this->wpdb = $wpdb;
37 }
38
39 /**
40 * 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
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.
54 *
55 * @return int
56 */
57 public function countAll(): int {
58 $wpdb = $this->wpdb;
59
60 return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM `' . $wpdb->packetery_log . '`' );
61 }
62
63 /**
64 * Finds logs.
65 *
66 * @param array $arguments Search arguments.
67 *
68 * @return iterable|Record[]
69 * @throws \Exception From DateTimeImmutable.
70 */
71 public function find( array $arguments ): iterable {
72 $wpdb = $this->wpdb;
73 $orderId = $arguments['order_id'] ?? null;
74 $orderBy = $arguments['orderby'] ?? [];
75 $limit = $arguments['limit'] ?? null;
76 $dateQuery = $arguments['date_query'] ?? [];
77
78 $orderByTransformed = [];
79 foreach ( $orderBy as $orderByKey => $orderByValue ) {
80 if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) {
81 $orderByValue = 'ASC';
82 }
83
84 $orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue;
85 }
86
87 $orderByClause = '';
88 if ( $orderByTransformed ) {
89 $orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed );
90 }
91
92 $limitClause = '';
93 if ( is_numeric( $limit ) ) {
94 $limitClause = ' LIMIT ' . $limit;
95 }
96
97 $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 ) );
101 }
102 }
103
104 if ( is_numeric( $orderId ) ) {
105 $where[] = $wpdb->prepare( '`order_id` = %d', $orderId );
106 }
107
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 );
115 if ( is_iterable( $result ) ) {
116 return $this->remapToRecord( $result );
117 }
118
119 return [];
120 }
121
122 /**
123 * Delete old records.
124 *
125 * @param string $before DateTime modifier.
126 *
127 * @return void
128 */
129 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 ) );
134 }
135
136 /**
137 * Remaps logs.
138 *
139 * @param iterable $logs Logs.
140 *
141 * @return \Generator|Record[]
142 */
143 public function remapToRecord( iterable $logs ): \Generator {
144 foreach ( $logs as $log ) {
145 $record = new Record();
146 $record->id = $log->id;
147 $record->status = $log->status;
148 $record->date = \DateTimeImmutable::createFromFormat( Helper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) )
149 ->setTimezone( wp_timezone() );
150 $record->action = $log->action;
151 $record->title = $log->title;
152
153 if ( $log->params ) {
154 $record->params = json_decode( $log->params, true );
155 } else {
156 $record->params = [];
157 }
158
159 $record->note = $this->getNote( $record->title, $record->params );
160
161 yield $record;
162 }
163 }
164
165 /**
166 * Gets note.
167 *
168 * @param string $title Title.
169 * @param array $params Params.
170 *
171 * @return string
172 */
173 private function getNote( string $title, array $params ): string {
174 return implode(
175 ' ',
176 array_filter(
177 [
178 $title,
179 ( $params ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ),
180 ]
181 )
182 );
183 }
184
185 /**
186 * Creates log table.
187 *
188 * @return void
189 */
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 }
209
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`' );
218 }
219
220 /**
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 * Save.
232 *
233 * @param Record $record Record.
234 *
235 * @return void
236 * @throws \Exception From DateTimeImmutable.
237 */
238 public function save( Record $record ): void {
239 $date = $record->date;
240 if ( null === $date ) {
241 $date = Helper::now();
242 }
243
244 $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT );
245
246 $paramsString = '';
247 if ( $record->params ) {
248 $paramsString = wp_json_encode( $record->params );
249 }
250
251 $orderId = $record->orderId;
252 if ( is_numeric( $orderId ) ) {
253 $orderId = (int) $orderId;
254 }
255
256 $data = [
257 'id' => $record->id,
258 'order_id' => $orderId,
259 'title' => ( $record->title ?? '' ),
260 'status' => ( $record->status ?? '' ),
261 'action' => ( $record->action ?? '' ),
262 'params' => $paramsString,
263 'date' => $dateString,
264 ];
265
266 $this->wpdb->_insert_replace_helper( $this->wpdb->packetery_log, $data, null, 'REPLACE' );
267 }
268 }
269