PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/Log/DbLogger.php +39 -30 1.2.62.3.2 View file →
@@ -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,47 +36,44 @@
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 array $sorting Sorting config.
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.
67 57 *
68 - * @return iterable|Record[]
58 + * @return \Generator<Record>|array{}
69 59 * @throws \Exception From DateTimeImmutable.
70 60 */
71 - public function getRecords( array $sorting = [] ): iterable {
61 + public function getRecords( ?int $orderId, ?string $action, array $sorting = [], int $limit = 100 ): iterable {
72 62 $arguments = [
73 63 'orderby' => $sorting,
74 - 'limit' => 100,
64 + 'limit' => $limit,
75 65 ];
76 66
67 + if ( is_numeric( $orderId ) ) {
68 + $arguments['order_id'] = $orderId;
69 + }
70 + if ( $action !== null ) {
71 + $arguments['action'] = $action;
72 + }
73 +
77 74 $logs = $this->logRepository->find( $arguments );
78 - if ( ! $logs ) {
75 + if ( ! $logs instanceof \Generator ) {
79 76 return [];
80 77 }
81 78
82 79 return $logs;
@@ -82,16 +79,28 @@
82 79 return $logs;
83 80 }
84 81
85 82 /**
83 + * Counts records.
84 + *
85 + * @param int|null $orderId Order ID.
86 + * @param string|null $action Action.
87 + *
88 + * @return int
89 + */
90 + public function countRecords( ?int $orderId = null, ?string $action = null ): int {
91 + return $this->logRepository->countRows( $orderId, $action );
92 + }
93 +
94 + /**
86 95 * Gets logs for given period as array.
87 96 *
88 - * @param array $dateQuery Date_query compatible array.
97 + * @param array<array<string, string>> $dateQuery Date_query compatible array.
89 98 *
90 - * @return array
99 + * @return \Generator<Record>|array{}
91 100 * @throws \Exception From DateTimeImmutable.
92 101 */
93 - public function getForPeriodAsArray( array $dateQuery ): iterable {
102 + public function getForPeriodAsArray( array $dateQuery ) {
94 103 $arguments = [
95 104 'orderby' => [ 'date' => 'ASC' ],
96 105 'date_query' => $dateQuery,
97 106 ];
@@ -96,9 +105,9 @@
96 105 'date_query' => $dateQuery,
97 106 ];
98 107
99 108 $logs = $this->logRepository->find( $arguments );
100 - if ( ! $logs ) {
109 + if ( ! $logs instanceof \Generator ) {
101 110 return [];
102 111 }
103 112
104 113 return $logs;