| @@ -6,13 +6,13 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | declare( strict_types=1 ); |
| 9 | 9 | |
| 10 | - | |
| 11 | 10 | namespace Packetery\Module\Log; |
| 12 | 11 | |
| 13 | -use Packetery\Core\Helper; | |
| 12 | +use Packetery\Core\CoreHelper; | |
| 14 | 13 | use Packetery\Core\Log\Record; |
| 14 | +use Packetery\Module\ModuleHelper; | |
| 15 | 15 | use Packetery\Module\WpdbAdapter; |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Class Repository |
| @@ -46,20 +46,21 @@ | ||
| 46 | 46 | * @return int |
| 47 | 47 | */ |
| 48 | 48 | public function countRows( ?int $orderId, ?string $action ): int { |
| 49 | 49 | $whereClause = $this->getWhereClause( [], $orderId, $action ); |
| 50 | - return (int) $this->wpdbAdapter->get_var( 'SELECT COUNT(*) FROM `' . $this->wpdbAdapter->packetery_log . '`' . $whereClause ); | |
| 50 | + | |
| 51 | + return (int) $this->wpdbAdapter->get_var( 'SELECT COUNT(*) FROM `' . $this->wpdbAdapter->packeteryLog . '`' . $whereClause ); | |
| 51 | 52 | } |
| 52 | 53 | |
| 53 | 54 | /** |
| 54 | 55 | * Finds logs. |
| 55 | 56 | * |
| 56 | - * @param array $arguments Search arguments. | |
| 57 | + * @param array<string, string|int|bool|float|null|array<string,mixed>> $arguments Search arguments. | |
| 57 | 58 | * |
| 58 | - * @return iterable|Record[] | |
| 59 | + * @return \Generator<Record>|array{} | |
| 59 | 60 | * @throws \Exception From DateTimeImmutable. |
| 60 | 61 | */ |
| 61 | - public function find( array $arguments ): iterable { | |
| 62 | + public function find( array $arguments ) { | |
| 62 | 63 | $orderId = $arguments['order_id'] ?? null; |
| 63 | 64 | $action = $arguments['action'] ?? null; |
| 64 | 65 | $orderBy = $arguments['orderby'] ?? []; |
| 65 | 66 | $limit = $arguments['limit'] ?? null; |
| @@ -65,18 +66,20 @@ | ||
| 65 | 66 | $limit = $arguments['limit'] ?? null; |
| 66 | 67 | $dateQuery = $arguments['date_query'] ?? []; |
| 67 | 68 | |
| 68 | 69 | $orderByTransformed = []; |
| 69 | - foreach ( $orderBy as $orderByKey => $orderByValue ) { | |
| 70 | - if ( ! in_array( $orderByValue, [ 'ASC', 'DESC' ], true ) ) { | |
| 71 | - $orderByValue = 'ASC'; | |
| 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; | |
| 72 | 77 | } |
| 73 | - | |
| 74 | - $orderByTransformed[] = '`' . $orderByKey . '` ' . $orderByValue; | |
| 75 | 78 | } |
| 76 | 79 | |
| 77 | 80 | $orderByClause = ''; |
| 78 | - if ( $orderByTransformed ) { | |
| 81 | + if ( count( $orderByTransformed ) > 0 ) { | |
| 79 | 82 | $orderByClause = ' ORDER BY ' . implode( ', ', $orderByTransformed ); |
| 80 | 83 | } |
| 81 | 84 | |
| 82 | 85 | $limitClause = ''; |
| @@ -84,17 +87,19 @@ | ||
| 84 | 87 | $limitClause = ' LIMIT ' . $limit; |
| 85 | 88 | } |
| 86 | 89 | |
| 87 | 90 | $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 | + 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 | + } | |
| 91 | 96 | } |
| 92 | 97 | } |
| 93 | 98 | |
| 94 | 99 | $whereClause = $this->getWhereClause( $where, $orderId, $action ); |
| 95 | 100 | |
| 96 | - $result = $this->wpdbAdapter->get_results( 'SELECT * FROM `' . $this->wpdbAdapter->packetery_log . '` ' . $whereClause . $orderByClause . $limitClause ); | |
| 101 | + $result = $this->wpdbAdapter->get_results( 'SELECT * FROM `' . $this->wpdbAdapter->packeteryLog . '` ' . $whereClause . $orderByClause . $limitClause ); | |
| 97 | 102 | if ( is_iterable( $result ) ) { |
| 98 | 103 | return $this->remapToRecord( $result ); |
| 99 | 104 | } |
| 100 | 105 | |
| @@ -108,11 +113,11 @@ | ||
| 108 | 113 | * |
| 109 | 114 | * @return void |
| 110 | 115 | */ |
| 111 | 116 | public function deleteOld( string $before ): void { |
| 112 | - $dateToFormatted = Helper::now()->modify( $before )->format( Helper::MYSQL_DATETIME_FORMAT ); | |
| 117 | + $dateToFormatted = CoreHelper::now()->modify( $before )->format( CoreHelper::MYSQL_DATETIME_FORMAT ); | |
| 113 | 118 | $this->wpdbAdapter->query( |
| 114 | - $this->wpdbAdapter->prepare( 'DELETE FROM `' . $this->wpdbAdapter->packetery_log . '` WHERE `date` < %s', $dateToFormatted ) | |
| 119 | + $this->wpdbAdapter->prepare( 'DELETE FROM `' . $this->wpdbAdapter->packeteryLog . '` WHERE `date` < %s', $dateToFormatted ) | |
| 115 | 120 | ); |
| 116 | 121 | } |
| 117 | 122 | |
| 118 | 123 | /** |
| @@ -117,18 +122,18 @@ | ||
| 117 | 122 | |
| 118 | 123 | /** |
| 119 | 124 | * Remaps logs. |
| 120 | 125 | * |
| 121 | - * @param iterable $logs Logs. | |
| 126 | + * @param array $logs Logs. | |
| 122 | 127 | * |
| 123 | - * @return \Generator|Record[] | |
| 128 | + * @return \Generator<Record> | |
| 124 | 129 | */ |
| 125 | - public function remapToRecord( iterable $logs ): \Generator { | |
| 130 | + public function remapToRecord( array $logs ): \Generator { | |
| 126 | 131 | foreach ( $logs as $log ) { |
| 127 | 132 | $record = new Record(); |
| 128 | 133 | $record->id = $log->id; |
| 129 | 134 | $record->status = $log->status; |
| 130 | - $record->date = \DateTimeImmutable::createFromFormat( Helper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) ) | |
| 135 | + $record->date = \DateTimeImmutable::createFromFormat( CoreHelper::MYSQL_DATETIME_FORMAT, $log->date, new \DateTimeZone( 'UTC' ) ) | |
| 131 | 136 | ->setTimezone( wp_timezone() ); |
| 132 | 137 | $record->action = $log->action; |
| 133 | 138 | $record->title = $log->title; |
| 134 | 139 | |
| @@ -137,8 +142,12 @@ | ||
| 137 | 142 | } else { |
| 138 | 143 | $record->params = []; |
| 139 | 144 | } |
| 140 | 145 | |
| 146 | + if ( ! is_array( $record->params ) ) { | |
| 147 | + $record->params = []; | |
| 148 | + } | |
| 149 | + | |
| 141 | 150 | $record->note = $this->getNote( $record->title, $record->params ); |
| 142 | 151 | |
| 143 | 152 | yield $record; |
| 144 | 153 | } |
| @@ -157,9 +166,9 @@ | ||
| 157 | 166 | ' ', |
| 158 | 167 | array_filter( |
| 159 | 168 | [ |
| 160 | 169 | $title, |
| 161 | - ( $params ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ), | |
| 170 | + ( count( $params ) > 0 ? 'Data: ' . wp_json_encode( $params, JSON_UNESCAPED_UNICODE ) : '' ), | |
| 162 | 171 | ] |
| 163 | 172 | ) |
| 164 | 173 | ); |
| 165 | 174 | } |
| @@ -169,9 +178,9 @@ | ||
| 169 | 178 | * |
| 170 | 179 | * @return bool |
| 171 | 180 | */ |
| 172 | 181 | public function createOrAlterTable(): bool { |
| 173 | - $createTableQuery = 'CREATE TABLE ' . $this->wpdbAdapter->packetery_log . " ( | |
| 182 | + $createTableQuery = 'CREATE TABLE ' . $this->wpdbAdapter->packeteryLog . " ( | |
| 174 | 183 | `id` int(11) NOT NULL AUTO_INCREMENT, |
| 175 | 184 | `order_id` bigint(20) unsigned NULL, |
| 176 | 185 | `title` varchar(255) NOT NULL DEFAULT '', |
| 177 | 186 | `params` text NOT NULL, |
| @@ -180,21 +189,12 @@ | ||
| 180 | 189 | `date` datetime NOT NULL, |
| 181 | 190 | PRIMARY KEY (`id`) |
| 182 | 191 | ) " . $this->wpdbAdapter->get_charset_collate(); |
| 183 | 192 | |
| 184 | - return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packetery_log ); | |
| 193 | + return $this->wpdbAdapter->dbDelta( $createTableQuery, $this->wpdbAdapter->packeteryLog ); | |
| 185 | 194 | } |
| 186 | 195 | |
| 187 | 196 | /** |
| 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 | 197 | * Save. |
| 198 | 198 | * |
| 199 | 199 | * @param Record $record Record. |
| 200 | 200 | * |
| @@ -202,17 +202,17 @@ | ||
| 202 | 202 | * @throws \Exception From DateTimeImmutable. |
| 203 | 203 | */ |
| 204 | 204 | public function save( Record $record ): void { |
| 205 | 205 | $date = $record->date; |
| 206 | - if ( null === $date ) { | |
| 207 | - $date = Helper::now(); | |
| 206 | + if ( $date === null ) { | |
| 207 | + $date = CoreHelper::now(); | |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( Helper::MYSQL_DATETIME_FORMAT ); | |
| 210 | + $dateString = $date->setTimezone( new \DateTimeZone( 'UTC' ) )->format( CoreHelper::MYSQL_DATETIME_FORMAT ); | |
| 211 | 211 | |
| 212 | 212 | $paramsString = ''; |
| 213 | - if ( $record->params ) { | |
| 214 | - $params = \Packetery\Module\Helper::convertArrayFloatsToStrings( $record->params ); | |
| 213 | + if ( $record->params !== null && count( $record->params ) > 0 ) { | |
| 214 | + $params = ModuleHelper::convertArrayFloatsToStrings( $record->params ); | |
| 215 | 215 | $paramsString = wp_json_encode( $params ); |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | $orderId = $record->orderId; |
| @@ -222,16 +222,16 @@ | ||
| 222 | 222 | |
| 223 | 223 | $data = [ |
| 224 | 224 | 'id' => $record->id, |
| 225 | 225 | 'order_id' => $orderId, |
| 226 | - 'title' => ( $record->title ?? '' ), | |
| 227 | - 'status' => ( $record->status ?? '' ), | |
| 228 | - 'action' => ( $record->action ?? '' ), | |
| 226 | + 'title' => $record->title, | |
| 227 | + 'status' => $record->status, | |
| 228 | + 'action' => $record->action, | |
| 229 | 229 | 'params' => $paramsString, |
| 230 | 230 | 'date' => $dateString, |
| 231 | 231 | ]; |
| 232 | 232 | |
| 233 | - $this->wpdbAdapter->insertReplaceHelper( $this->wpdbAdapter->packetery_log, $data, null, 'REPLACE' ); | |
| 233 | + $this->wpdbAdapter->insertReplaceHelper( $this->wpdbAdapter->packeteryLog, $data, null, 'REPLACE' ); | |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | /** |
| 237 | 237 | * Gets where clause for find and count queries. |
| @@ -245,17 +245,16 @@ | ||
| 245 | 245 | private function getWhereClause( array $where, ?int $orderId, ?string $action ): string { |
| 246 | 246 | if ( is_numeric( $orderId ) ) { |
| 247 | 247 | $where[] = $this->wpdbAdapter->prepare( '`order_id` = %d', $orderId ); |
| 248 | 248 | } |
| 249 | - if ( null !== $action ) { | |
| 249 | + if ( $action !== null ) { | |
| 250 | 250 | $where[] = $this->wpdbAdapter->prepare( '`action` = %s', $action ); |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | $whereClause = ''; |
| 254 | - if ( $where ) { | |
| 254 | + if ( count( $where ) > 0 ) { | |
| 255 | 255 | $whereClause = ' WHERE ' . implode( ' AND ', $where ); |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | return $whereClause; |
| 259 | 259 | } |
| 260 | - | |
| 261 | 260 | } |