| 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 |
* @throws \Exception From DateTimeImmutable. |
| 46 |
*/ |
| 47 |
public function find( array $arguments ): iterable { |
| 48 |
$wpdb = $this->wpdb; |
| 49 |
$orderBy = $arguments['orderby'] ?? []; |
| 50 |
$limit = $arguments['limit'] ?? null; |
| 51 |
$dateQuery = $arguments['date_query'] ?? []; |
| 52 |
|
| 53 |
$orderByTransformed = []; |
| 54 |
foreach ( $orderBy as $orderByKey => $orderByValue ) { |
| 55 |
if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) { |
| 56 |
$orderByValue = 'ASC'; |
| 57 |
} |
| 58 |
|
| 59 |
$orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue; |
| 60 |
} |
| 61 |
|
| 62 |
$orderByClause = ''; |
| 63 |
if ( $orderByTransformed ) { |
| 64 |
$orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed ); |
| 65 |
} |
| 66 |
|
| 67 |
$limitClause = ''; |
| 68 |
if ( is_numeric( $limit ) ) { |
| 69 |
$limitClause = ' LIMIT ' . $limit; |
| 70 |
} |
| 71 |
|
| 72 |
$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 ) ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$whereClause = ''; |
| 80 |
if ( $where ) { |
| 81 |
$whereClause = ' WHERE ' . implode( ' AND ', $where ); |
| 82 |
} |
| 83 |
|
| 84 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 85 |
$result = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->packetery_log . $whereClause . $orderByClause . $limitClause ); |
| 86 |
if ( is_iterable( $result ) ) { |
| 87 |
return $this->remapToRecord( $result ); |
| 88 |
} |
| 89 |
|
| 90 |
return []; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Remaps logs. |
| 95 |
* |
| 96 |
* @param iterable $logs Logs. |
| 97 |
* |
| 98 |
* @return \Generator|Record[] |
| 99 |
*/ |
| 100 |
public function remapToRecord( iterable $logs ): \Generator { |
| 101 |
foreach ( $logs as $log ) { |
| 102 |
$record = new Record(); |
| 103 |
$record->id = $log->id; |
| 104 |
$record->status = $log->status; |
| 105 |
$record->date = \DateTimeImmutable::createFromFormat( Helper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) ) |
| 106 |
->setTimezone( wp_timezone() ); |
| 107 |
$record->action = $log->action; |
| 108 |
$record->title = $log->title; |
| 109 |
|
| 110 |
if ( $log->params ) { |
| 111 |
$record->params = json_decode( $log->params, true ); |
| 112 |
} else { |
| 113 |
$record->params = []; |
| 114 |
} |
| 115 |
|
| 116 |
$record->note = $this->getNote( $record->title, $record->params ); |
| 117 |
|
| 118 |
yield $record; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Gets note. |
| 124 |
* |
| 125 |
* @param string $title Title. |
| 126 |
* @param array $params Params. |
| 127 |
* |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
private function getNote( string $title, array $params ): string { |
| 131 |
return implode( |
| 132 |
' ', |
| 133 |
array_filter( |
| 134 |
[ |
| 135 |
$title, |
| 136 |
( $params ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ), |
| 137 |
] |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Creates log table. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 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 |
} |
| 165 |
|
| 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 . '`' ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Save. |
| 178 |
* |
| 179 |
* @param Record $record Record. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
* @throws \Exception From DateTimeImmutable. |
| 183 |
*/ |
| 184 |
public function save( Record $record ): void { |
| 185 |
$date = $record->date; |
| 186 |
if ( null === $date ) { |
| 187 |
$date = Helper::now(); |
| 188 |
} |
| 189 |
|
| 190 |
$dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT ); |
| 191 |
|
| 192 |
$paramsString = ''; |
| 193 |
if ( $record->params ) { |
| 194 |
$paramsString = wp_json_encode( $record->params ); |
| 195 |
} |
| 196 |
|
| 197 |
$data = [ |
| 198 |
'id' => $record->id, |
| 199 |
'title' => ( $record->title ?? '' ), |
| 200 |
'status' => ( $record->status ?? '' ), |
| 201 |
'action' => ( $record->action ?? '' ), |
| 202 |
'params' => $paramsString, |
| 203 |
'date' => $dateString, |
| 204 |
]; |
| 205 |
|
| 206 |
$this->wpdb->_insert_replace_helper( $this->wpdb->packetery_log, $data, null, 'REPLACE' ); |
| 207 |
} |
| 208 |
} |
| 209 |
|