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
packeta / src / Packetery / Module / Log / Repository.php

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

270 lines 6.7 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 namespace Packetery\Module\Log;
11
12 use Packetery\Core\CoreHelper;
13 use Packetery\Core\Log\Record;
14 use Packetery\Module\ModuleHelper;
15 use Packetery\Module\WpdbAdapter;
16
17 /**
18 * Class Repository
19 *
20 * @package Packetery\Module\Log
21 */
22 class Repository {
23
24 /**
25 * WpdbAdapter.
26 *
27 * @var WpdbAdapter
28 */
29 private $wpdbAdapter;
30
31 /**
32 * Constructor.
33 *
34 * @param WpdbAdapter $wpdbAdapter WpdbAdapter.
35 */
36 public function __construct( WpdbAdapter $wpdbAdapter ) {
37 $this->wpdbAdapter = $wpdbAdapter;
38 }
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 /**
55 * Finds logs.
56 *
57 * @param array<string, string|int|bool|float|null|array<string,mixed>> $arguments Search arguments.
58 *
59 * @return \Generator<Record>|array{}
60 * @throws \Exception From DateTimeImmutable.
61 */
62 public function find( array $arguments ) {
63 $orderId = $arguments['order_id'] ?? null;
64 $action = $arguments['action'] ?? null;
65 $orderBy = $arguments['orderby'] ?? [];
66 $limit = $arguments['limit'] ?? null;
67 $dateQuery = $arguments['date_query'] ?? [];
68
69 $orderByTransformed = [];
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;
77 }
78 }
79
80 $orderByClause = '';
81 if ( count( $orderByTransformed ) > 0 ) {
82 $orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed );
83 }
84
85 $limitClause = '';
86 if ( is_numeric( $limit ) ) {
87 $limitClause = ' LIMIT ' . $limit;
88 }
89
90 $where = [];
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 }
96 }
97 }
98
99 $whereClause = $this->getWhereClause( $where, $orderId, $action );
100
101 $result = $this->wpdbAdapter->get_results( 'SELECT * FROM `' . $this->wpdbAdapter->packeteryLog . '` ' . $whereClause . $orderByClause . $limitClause );
102 if ( is_iterable( $result ) ) {
103 return $this->remapToRecord( $result );
104 }
105
106 return [];
107 }
108
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 /**
124 * Remaps logs.
125 *
126 * @param array $logs Logs.
127 *
128 * @return \Generator<Record>
129 */
130 public function remapToRecord( array $logs ): \Generator {
131 foreach ( $logs as $log ) {
132 $record = new Record();
133 $record->id = $log->id;
134 $record->status = $log->status;
135 $record->date = \DateTimeImmutable::createFromFormat( CoreHelper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) )
136 ->setTimezone( wp_timezone() );
137 $record->action = $log->action;
138 $record->title = $log->title;
139
140 if ( $log->params ) {
141 $record->params = json_decode( $log->params, true );
142 } else {
143 $record->params = [];
144 }
145
146 if ( ! is_array( $record->params ) ) {
147 $record->params = [];
148 }
149
150 $record->note = $this->getNote( $record->title, $record->params );
151
152 yield $record;
153 }
154 }
155
156 /**
157 * Gets note.
158 *
159 * @param string $title Title.
160 * @param array $params Params.
161 *
162 * @return string
163 */
164 private function getNote( string $title, array $params ): string {
165 return implode(
166 ' ',
167 array_filter(
168 [
169 $title,
170 ( count( $params ) > 0 ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ),
171 ]
172 )
173 );
174 }
175
176 /**
177 * Creates log table.
178 *
179 * @return bool
180 */
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();
192
193 return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryLog );
194 }
195
196 /**
197 * Drops log table.
198 *
199 * @return void
200 */
201 public function drop(): void {
202 $this->wpdbAdapter->query( 'DROP TABLE IF EXISTS `' . $this->wpdbAdapter->packeteryLog . '`' );
203 }
204
205 /**
206 * Save.
207 *
208 * @param Record $record Record.
209 *
210 * @return void
211 * @throws \Exception From DateTimeImmutable.
212 */
213 public function save( Record $record ): void {
214 $date = $record->date;
215 if ( $date === null ) {
216 $date = CoreHelper::now();
217 }
218
219 $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( CoreHelper::MYSQL_DATETIME_FORMAT );
220
221 $paramsString = '';
222 if ( $record->params !== null && count( $record->params ) > 0 ) {
223 $params = ModuleHelper::convertArrayFloatsToStrings( $record->params );
224 $paramsString = wp_json_encode( $params );
225 }
226
227 $orderId = $record->orderId;
228 if ( is_numeric( $orderId ) ) {
229 $orderId = (int) $orderId;
230 }
231
232 $data = [
233 'id' => $record->id,
234 'order_id' => $orderId,
235 'title' => $record->title,
236 'status' => $record->status,
237 'action' => $record->action,
238 'params' => $paramsString,
239 'date' => $dateString,
240 ];
241
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;
268 }
269 }
270