| 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 |
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 |
return (int) $this->wpdbAdapter->get_var( 'SELECT COUNT(*) FROM `' . $this->wpdbAdapter->packetery_log . '`' . $whereClause ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Finds logs. |
| 55 |
* |
| 56 |
* @param array $arguments Search arguments. |
| 57 |
* |
| 58 |
* @return iterable|Record[] |
| 59 |
* @throws \Exception From DateTimeImmutable. |
| 60 |
*/ |
| 61 |
public function find( array $arguments ): iterable { |
| 62 |
$orderId = $arguments['order_id'] ?? null; |
| 63 |
$action = $arguments['action'] ?? null; |
| 64 |
$orderBy = $arguments['orderby'] ?? []; |
| 65 |
$limit = $arguments['limit'] ?? null; |
| 66 |
$dateQuery = $arguments['date_query'] ?? []; |
| 67 |
|
| 68 |
$orderByTransformed = []; |
| 69 |
foreach ( $orderBy as $orderByKey => $orderByValue ) { |
| 70 |
if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) { |
| 71 |
$orderByValue = 'ASC'; |
| 72 |
} |
| 73 |
|
| 74 |
$orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue; |
| 75 |
} |
| 76 |
|
| 77 |
$orderByClause = ''; |
| 78 |
if ( $orderByTransformed ) { |
| 79 |
$orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed ); |
| 80 |
} |
| 81 |
|
| 82 |
$limitClause = ''; |
| 83 |
if ( is_numeric( $limit ) ) { |
| 84 |
$limitClause = ' LIMIT ' . $limit; |
| 85 |
} |
| 86 |
|
| 87 |
$where = []; |
| 88 |
foreach ( $dateQuery as $dateQueryItem ) { |
| 89 |
if ( isset( $dateQueryItem['after'] ) ) { |
| 90 |
$where[] = $this->wpdbAdapter->prepare( '`date` > %s', Helper::now()->modify( $dateQueryItem['after'] )->format( Helper::MYSQL_DATETIME_FORMAT ) ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
$whereClause = $this->getWhereClause( $where, $orderId, $action ); |
| 95 |
|
| 96 |
$result = $this->wpdbAdapter->get_results( 'SELECT * FROM `' . $this->wpdbAdapter->packetery_log . '` ' . $whereClause . $orderByClause . $limitClause ); |
| 97 |
if ( is_iterable( $result ) ) { |
| 98 |
return $this->remapToRecord( $result ); |
| 99 |
} |
| 100 |
|
| 101 |
return []; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Delete old records. |
| 106 |
* |
| 107 |
* @param string $before DateTime modifier. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function deleteOld( string $before ): void { |
| 112 |
$dateToFormatted = Helper::now()->modify( $before )->format( Helper::MYSQL_DATETIME_FORMAT ); |
| 113 |
$this->wpdbAdapter->query( |
| 114 |
$this->wpdbAdapter->prepare( 'DELETE FROM `' . $this->wpdbAdapter->packetery_log . '` WHERE `date` < %s', $dateToFormatted ) |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Remaps logs. |
| 120 |
* |
| 121 |
* @param iterable $logs Logs. |
| 122 |
* |
| 123 |
* @return \Generator|Record[] |
| 124 |
*/ |
| 125 |
public function remapToRecord( iterable $logs ): \Generator { |
| 126 |
foreach ( $logs as $log ) { |
| 127 |
$record = new Record(); |
| 128 |
$record->id = $log->id; |
| 129 |
$record->status = $log->status; |
| 130 |
$record->date = \DateTimeImmutable::createFromFormat( Helper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) ) |
| 131 |
->setTimezone( wp_timezone() ); |
| 132 |
$record->action = $log->action; |
| 133 |
$record->title = $log->title; |
| 134 |
|
| 135 |
if ( $log->params ) { |
| 136 |
$record->params = json_decode( $log->params, true ); |
| 137 |
} else { |
| 138 |
$record->params = []; |
| 139 |
} |
| 140 |
|
| 141 |
$record->note = $this->getNote( $record->title, $record->params ); |
| 142 |
|
| 143 |
yield $record; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Gets note. |
| 149 |
* |
| 150 |
* @param string $title Title. |
| 151 |
* @param array $params Params. |
| 152 |
* |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
private function getNote( string $title, array $params ): string { |
| 156 |
return implode( |
| 157 |
' ', |
| 158 |
array_filter( |
| 159 |
[ |
| 160 |
$title, |
| 161 |
( $params ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ), |
| 162 |
] |
| 163 |
) |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Creates log table. |
| 169 |
* |
| 170 |
* @return bool |
| 171 |
*/ |
| 172 |
public function createOrAlterTable(): bool { |
| 173 |
$createTableQuery = 'CREATE TABLE ' . $this->wpdbAdapter->packetery_log . " ( |
| 174 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 175 |
`order_id` bigint(20) unsigned NULL, |
| 176 |
`title` varchar(255) NOT NULL DEFAULT '', |
| 177 |
`params` text NOT NULL, |
| 178 |
`status` varchar(255) NOT NULL DEFAULT '', |
| 179 |
`action` varchar(255) NOT NULL DEFAULT '', |
| 180 |
`date` datetime NOT NULL, |
| 181 |
PRIMARY KEY (`id`) |
| 182 |
) " . $this->wpdbAdapter->get_charset_collate(); |
| 183 |
|
| 184 |
return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packetery_log ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Drops log table. |
| 189 |
* |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public function drop(): void { |
| 193 |
$this->wpdbAdapter->query( 'DROP TABLE IF EXISTS `' . $this->wpdbAdapter->packetery_log . '`' ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Save. |
| 198 |
* |
| 199 |
* @param Record $record Record. |
| 200 |
* |
| 201 |
* @return void |
| 202 |
* @throws \Exception From DateTimeImmutable. |
| 203 |
*/ |
| 204 |
public function save( Record $record ): void { |
| 205 |
$date = $record->date; |
| 206 |
if ( null === $date ) { |
| 207 |
$date = Helper::now(); |
| 208 |
} |
| 209 |
|
| 210 |
$dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT ); |
| 211 |
|
| 212 |
$paramsString = ''; |
| 213 |
if ( $record->params ) { |
| 214 |
$params = \Packetery\Module\Helper::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 |
$this->wpdbAdapter->insertReplaceHelper( $this->wpdbAdapter->packetery_log, $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 ( null !== $action ) { |
| 250 |
$where[] = $this->wpdbAdapter->prepare( '`action` = %s', $action ); |
| 251 |
} |
| 252 |
|
| 253 |
$whereClause = ''; |
| 254 |
if ( $where ) { |
| 255 |
$whereClause = ' WHERE ' . implode( ' AND ', $where ); |
| 256 |
} |
| 257 |
|
| 258 |
return $whereClause; |
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
|