| @@ -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 | * |
| @@ -36,43 +36,30 @@ | ||
| 36 | 36 | $this->logRepository = $logRepository; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | - * Registers logger. | |
| 41 | - * | |
| 42 | - * @return void | |
| 40 | + * @return int|false The number of rows inserted, or false on error. | |
| 43 | 41 | */ |
| 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 | - public function add( Record $record ): void { | |
| 56 | - if ( null === $record->date ) { | |
| 57 | - $record->date = Helper::now(); | |
| 42 | + public function add( Record $record ) { | |
| 43 | + if ( $record->date === null ) { | |
| 44 | + $record->date = CoreHelper::now(); | |
| 58 | 45 | } |
| 59 | 46 | |
| 60 | - $this->logRepository->save( $record ); | |
| 47 | + return $this->logRepository->save( $record ); | |
| 61 | 48 | } |
| 62 | 49 | |
| 63 | 50 | /** |
| 64 | 51 | * Gets records. |
| 65 | 52 | * |
| 66 | - * @param mixed $orderId Order ID. | |
| 67 | - * @param string|null $action Action. | |
| 68 | - * @param array $sorting Sorting config. | |
| 69 | - * @param int $limit Limit. | |
| 53 | + * @param int|null $orderId Order ID. | |
| 54 | + * @param string|null $action Action. | |
| 55 | + * @param array<string, string> $sorting Sorting config. | |
| 56 | + * @param int $limit Limit. | |
| 70 | 57 | * |
| 71 | - * @return iterable|Record[] | |
| 58 | + * @return \Generator<Record>|array{} | |
| 72 | 59 | * @throws \Exception From DateTimeImmutable. |
| 73 | 60 | */ |
| 74 | - public function getRecords( $orderId, ?string $action, array $sorting = [], int $limit = 100 ): iterable { | |
| 61 | + public function getRecords( ?int $orderId, ?string $action, array $sorting = [], int $limit = 100 ): iterable { | |
| 75 | 62 | $arguments = [ |
| 76 | 63 | 'orderby' => $sorting, |
| 77 | 64 | 'limit' => $limit, |
| 78 | 65 | ]; |
| @@ -79,14 +66,14 @@ | ||
| 79 | 66 | |
| 80 | 67 | if ( is_numeric( $orderId ) ) { |
| 81 | 68 | $arguments['order_id'] = $orderId; |
| 82 | 69 | } |
| 83 | - if ( null !== $action ) { | |
| 70 | + if ( $action !== null ) { | |
| 84 | 71 | $arguments['action'] = $action; |
| 85 | 72 | } |
| 86 | 73 | |
| 87 | 74 | $logs = $this->logRepository->find( $arguments ); |
| 88 | - if ( ! $logs ) { | |
| 75 | + if ( ! $logs instanceof \Generator ) { | |
| 89 | 76 | return []; |
| 90 | 77 | } |
| 91 | 78 | |
| 92 | 79 | return $logs; |
| @@ -99,9 +86,9 @@ | ||
| 99 | 86 | * @param string|null $action Action. |
| 100 | 87 | * |
| 101 | 88 | * @return int |
| 102 | 89 | */ |
| 103 | - public function countRecords( $orderId = null, ?string $action = null ): int { | |
| 90 | + public function countRecords( ?int $orderId = null, ?string $action = null ): int { | |
| 104 | 91 | return $this->logRepository->countRows( $orderId, $action ); |
| 105 | 92 | } |
| 106 | 93 | |
| 107 | 94 | /** |
| @@ -106,14 +93,14 @@ | ||
| 106 | 93 | |
| 107 | 94 | /** |
| 108 | 95 | * Gets logs for given period as array. |
| 109 | 96 | * |
| 110 | - * @param array $dateQuery Date_query compatible array. | |
| 97 | + * @param array<array<string, string>> $dateQuery Date_query compatible array. | |
| 111 | 98 | * |
| 112 | - * @return array | |
| 99 | + * @return \Generator<Record>|array{} | |
| 113 | 100 | * @throws \Exception From DateTimeImmutable. |
| 114 | 101 | */ |
| 115 | - public function getForPeriodAsArray( array $dateQuery ): iterable { | |
| 102 | + public function getForPeriodAsArray( array $dateQuery ) { | |
| 116 | 103 | $arguments = [ |
| 117 | 104 | 'orderby' => [ 'date' => 'ASC' ], |
| 118 | 105 | 'date_query' => $dateQuery, |
| 119 | 106 | ]; |
| @@ -118,9 +105,9 @@ | ||
| 118 | 105 | 'date_query' => $dateQuery, |
| 119 | 106 | ]; |
| 120 | 107 | |
| 121 | 108 | $logs = $this->logRepository->find( $arguments ); |
| 122 | - if ( ! $logs ) { | |
| 109 | + if ( ! $logs instanceof \Generator ) { | |
| 123 | 110 | return []; |
| 124 | 111 | } |
| 125 | 112 | |
| 126 | 113 | return $logs; |