| @@ -6,12 +6,12 @@ | ||
| 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; | |
| 13 | +use Packetery\Core\Log\ILogger; | |
| 14 | 14 | use Packetery\Core\Log\Record; |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * Class DbLogger |
| @@ -17,9 +17,9 @@ | ||
| 17 | 17 | * Class DbLogger |
| 18 | 18 | * |
| 19 | 19 | * @package Packetery\Module\Log |
| 20 | 20 | */ |
| 21 | -class DbLogger implements \Packetery\Core\Log\ILogger { | |
| 21 | +class DbLogger implements ILogger { | |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Log repository. |
| 25 | 25 | * |
| @@ -35,27 +35,11 @@ | ||
| 35 | 35 | public function __construct( Repository $logRepository ) { |
| 36 | 36 | $this->logRepository = $logRepository; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - /** | |
| 40 | - * Registers logger. | |
| 41 | - * | |
| 42 | - * @return void | |
| 43 | - */ | |
| 44 | - public function register(): void { | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Adds record. | |
| 49 | - * | |
| 50 | - * @param Record $record Record. | |
| 51 | - * | |
| 52 | - * @return void | |
| 53 | - * @throws \Exception From DateTimeImmutable. | |
| 54 | - */ | |
| 55 | 39 | public function add( Record $record ): void { |
| 56 | - if ( null === $record->date ) { | |
| 57 | - $record->date = Helper::now(); | |
| 40 | + if ( $record->date === null ) { | |
| 41 | + $record->date = CoreHelper::now(); | |
| 58 | 42 | } |
| 59 | 43 | |
| 60 | 44 | $this->logRepository->save( $record ); |
| 61 | 45 | } |
| @@ -62,21 +46,31 @@ | ||
| 62 | 46 | |
| 63 | 47 | /** |
| 64 | 48 | * Gets records. |
| 65 | 49 | * |
| 66 | - * @param array $sorting Sorting config. | |
| 50 | + * @param int|null $orderId Order ID. | |
| 51 | + * @param string|null $action Action. | |
| 52 | + * @param array<string, string> $sorting Sorting config. | |
| 53 | + * @param int $limit Limit. | |
| 67 | 54 | * |
| 68 | - * @return iterable|Record[] | |
| 55 | + * @return \Generator<Record>|array{} | |
| 69 | 56 | * @throws \Exception From DateTimeImmutable. |
| 70 | 57 | */ |
| 71 | - public function getRecords( array $sorting = [] ): iterable { | |
| 58 | + public function getRecords( ?int $orderId, ?string $action, array $sorting = [], int $limit = 100 ): iterable { | |
| 72 | 59 | $arguments = [ |
| 73 | 60 | 'orderby' => $sorting, |
| 74 | - 'limit' => 100, | |
| 61 | + 'limit' => $limit, | |
| 75 | 62 | ]; |
| 76 | 63 | |
| 64 | + if ( is_numeric( $orderId ) ) { | |
| 65 | + $arguments['order_id'] = $orderId; | |
| 66 | + } | |
| 67 | + if ( $action !== null ) { | |
| 68 | + $arguments['action'] = $action; | |
| 69 | + } | |
| 70 | + | |
| 77 | 71 | $logs = $this->logRepository->find( $arguments ); |
| 78 | - if ( ! $logs ) { | |
| 72 | + if ( ! $logs instanceof \Generator ) { | |
| 79 | 73 | return []; |
| 80 | 74 | } |
| 81 | 75 | |
| 82 | 76 | return $logs; |
| @@ -82,16 +76,28 @@ | ||
| 82 | 76 | return $logs; |
| 83 | 77 | } |
| 84 | 78 | |
| 85 | 79 | /** |
| 80 | + * Counts records. | |
| 81 | + * | |
| 82 | + * @param int|null $orderId Order ID. | |
| 83 | + * @param string|null $action Action. | |
| 84 | + * | |
| 85 | + * @return int | |
| 86 | + */ | |
| 87 | + public function countRecords( ?int $orderId = null, ?string $action = null ): int { | |
| 88 | + return $this->logRepository->countRows( $orderId, $action ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 86 | 92 | * Gets logs for given period as array. |
| 87 | 93 | * |
| 88 | - * @param array $dateQuery Date_query compatible array. | |
| 94 | + * @param array<array<string, string>> $dateQuery Date_query compatible array. | |
| 89 | 95 | * |
| 90 | - * @return array | |
| 96 | + * @return \Generator<Record>|array{} | |
| 91 | 97 | * @throws \Exception From DateTimeImmutable. |
| 92 | 98 | */ |
| 93 | - public function getForPeriodAsArray( array $dateQuery ): iterable { | |
| 99 | + public function getForPeriodAsArray( array $dateQuery ) { | |
| 94 | 100 | $arguments = [ |
| 95 | 101 | 'orderby' => [ 'date' => 'ASC' ], |
| 96 | 102 | 'date_query' => $dateQuery, |
| 97 | 103 | ]; |
| @@ -96,9 +102,9 @@ | ||
| 96 | 102 | 'date_query' => $dateQuery, |
| 97 | 103 | ]; |
| 98 | 104 | |
| 99 | 105 | $logs = $this->logRepository->find( $arguments ); |
| 100 | - if ( ! $logs ) { | |
| 106 | + if ( ! $logs instanceof \Generator ) { | |
| 101 | 107 | return []; |
| 102 | 108 | } |
| 103 | 109 | |
| 104 | 110 | return $logs; |