| 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 |
* Save. |
| 198 |
* |
| 199 |
* @param Record $record Record. |
| 200 |
* |
| 201 |
* @return int|false The number of rows updated, or false on error. |
| 202 |
* @throws \Exception From DateTimeImmutable. |
| 203 |
*/ |
| 204 |
public function save( Record $record ) { |
| 205 |
$date = $record->date; |
| 206 |
if ( $date === null ) { |
| 207 |
$date = CoreHelper::now(); |
| 208 |
} |
| 209 |
|
| 210 |
$dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( CoreHelper::MYSQL_DATETIME_FORMAT ); |
| 211 |
|
| 212 |
$paramsString = ''; |
| 213 |
if ( $record->params !== null && count( $record->params ) > 0 ) { |
| 214 |
$params = ModuleHelper::convertArrayFloatsToStrings( $record->params ); |
| 215 |
$paramsString = wp_json_encode( $params ); |
| 216 |
} |
| 217 |
|
| 218 |
$orderId = $record->orderId; |
| 219 |
if ( is_numeric( $orderId ) ) { |
| 220 |
$orderId = (int) $orderId; |
| 221 |
} |
| 222 |
|
| 223 |
$data = [ |
| 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, |
| 231 |
]; |
| 232 |
|
| 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; |
| 259 |
} |
| 260 |
} |
| 261 |
|