PluginProbe
Packeta / 1.2.3
Packeta v1.2.3
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.2.3, at src/Packetery/Module/Log/Repository.php

207 lines 4.5 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 * Finds logs.
41 *
42 * @param array $arguments Search arguments.
43 *
44 * @return iterable|Record[]
45 */
46 public function find( array $arguments ): iterable {
47 $wpdb = $this->wpdb;
48 $orderBy = $arguments['orderby'] ?? [];
49 $limit = $arguments['limit'] ?? null;
50 $dateQuery = $arguments['date_query'] ?? [];
51
52 $orderByTransformed = [];
53 foreach ( $orderBy as $orderByKey => $orderByValue ) {
54 if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) {
55 $orderByValue = 'ASC';
56 }
57
58 $orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue;
59 }
60
61 $orderByClause = '';
62 if ( $orderByTransformed ) {
63 $orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed );
64 }
65
66 $limitClause = '';
67 if ( is_numeric( $limit ) ) {
68 $limitClause = ' LIMIT ' . $limit;
69 }
70
71 $where = [];
72 foreach ( $dateQuery as $dateQueryItem ) {
73 if ( isset( $dateQueryItem['after'] ) ) {
74 $where[] = $wpdb->prepare( '`date` > %s', Helper::now()->modify( $dateQueryItem['after'] )->format( Helper::MYSQL_DATETIME_FORMAT ) );
75 }
76 }
77
78 $whereClause = '';
79 if ( $where ) {
80 $whereClause = ' WHERE ' . implode( ' AND ', $where );
81 }
82
83 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
84 $result = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->packetery_log . $whereClause . $orderByClause . $limitClause );
85 if ( is_iterable( $result ) ) {
86 return $this->remapToRecord( $result );
87 }
88
89 return [];
90 }
91
92 /**
93 * Remaps logs.
94 *
95 * @param iterable $logs Logs.
96 *
97 * @return \Generator|Record[]
98 */
99 public function remapToRecord( iterable $logs ): \Generator {
100 foreach ( $logs as $log ) {
101 $record = new Record();
102 $record->id = $log->id;
103 $record->status = $log->status;
104 $record->date = \DateTimeImmutable::createFromFormat( Helper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) )
105 ->setTimezone( wp_timezone() );
106 $record->action = $log->action;
107 $record->title = $log->title;
108
109 if ( $log->params ) {
110 $record->params = json_decode( $log->params, true );
111 } else {
112 $record->params = [];
113 }
114
115 $record->note = $this->getNote( $record->title, $record->params );
116
117 yield $record;
118 }
119 }
120
121 /**
122 * Gets note.
123 *
124 * @param string $title Title.
125 * @param array $params Params.
126 *
127 * @return string
128 */
129 private function getNote( string $title, array $params ): string {
130 return implode(
131 ' ',
132 array_filter(
133 [
134 $title,
135 ( $params ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ),
136 ]
137 )
138 );
139 }
140
141 /**
142 * Creates log table.
143 *
144 * @return void
145 */
146 public function createTable(): void {
147 $wpdb = $this->wpdb;
148 $wpdb->query(
149 '
150 CREATE TABLE IF NOT EXISTS `' . $wpdb->packetery_log . "` (
151 `id` INT(11) NOT NULL AUTO_INCREMENT,
152 `title` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
153 `params` TEXT NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
154 `status` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
155 `action` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
156 `date` DATETIME NOT NULL,
157 PRIMARY KEY (`id`) USING BTREE
158 )
159 COLLATE='utf8_general_ci'
160 ENGINE=InnoDB
161 "
162 );
163 }
164
165 /**
166 * Drops log table.
167 *
168 * @return void
169 */
170 public function drop(): void {
171 $wpdb = $this->wpdb;
172 $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->packetery_log . '`' );
173 }
174
175 /**
176 * Save.
177 *
178 * @param Record $record Record.
179 *
180 * @return void
181 */
182 public function save( Record $record ): void {
183 $date = $record->date;
184 if ( null === $date ) {
185 $date = Helper::now();
186 }
187
188 $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT );
189
190 $paramsString = '';
191 if ( $record->params ) {
192 $paramsString = wp_json_encode( $record->params );
193 }
194
195 $data = [
196 'id' => $record->id,
197 'title' => ( $record->title ?? '' ),
198 'status' => ( $record->status ?? '' ),
199 'action' => ( $record->action ?? '' ),
200 'params' => $paramsString,
201 'date' => $dateString,
202 ];
203
204 $this->wpdb->_insert_replace_helper( $this->wpdb->packetery_log, $data, null, 'REPLACE' );
205 }
206 }
207