PluginProbe
Packeta / 2.1
Packeta v2.1
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
packeta / src / Packetery / Module / Log / DbLogger.php

DbLogger.php in Packeta 2.1, at src/Packetery/Module/Log/DbLogger.php

113 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class DbLogger
4 *
5 * @package Packetery\Module\Log
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Log;
11
12 use Packetery\Core\CoreHelper;
13 use Packetery\Core\Log\ILogger;
14 use Packetery\Core\Log\Record;
15
16 /**
17 * Class DbLogger
18 *
19 * @package Packetery\Module\Log
20 */
21 class DbLogger implements ILogger {
22
23 /**
24 * Log repository.
25 *
26 * @var Repository
27 */
28 private $logRepository;
29
30 /**
31 * Constructor.
32 *
33 * @param Repository $logRepository Log repository.
34 */
35 public function __construct( Repository $logRepository ) {
36 $this->logRepository = $logRepository;
37 }
38
39 public function add( Record $record ): void {
40 if ( $record->date === null ) {
41 $record->date = CoreHelper::now();
42 }
43
44 $this->logRepository->save( $record );
45 }
46
47 /**
48 * Gets records.
49 *
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.
54 *
55 * @return \Generator<Record>|array{}
56 * @throws \Exception From DateTimeImmutable.
57 */
58 public function getRecords( ?int $orderId, ?string $action, array $sorting = [], int $limit = 100 ): iterable {
59 $arguments = [
60 'orderby' => $sorting,
61 'limit' => $limit,
62 ];
63
64 if ( is_numeric( $orderId ) ) {
65 $arguments['order_id'] = $orderId;
66 }
67 if ( $action !== null ) {
68 $arguments['action'] = $action;
69 }
70
71 $logs = $this->logRepository->find( $arguments );
72 if ( ! $logs instanceof \Generator ) {
73 return [];
74 }
75
76 return $logs;
77 }
78
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 /**
92 * Gets logs for given period as array.
93 *
94 * @param array<array<string, string>> $dateQuery Date_query compatible array.
95 *
96 * @return \Generator<Record>|array{}
97 * @throws \Exception From DateTimeImmutable.
98 */
99 public function getForPeriodAsArray( array $dateQuery ) {
100 $arguments = [
101 'orderby' => [ 'date' => 'ASC' ],
102 'date_query' => $dateQuery,
103 ];
104
105 $logs = $this->logRepository->find( $arguments );
106 if ( ! $logs instanceof \Generator ) {
107 return [];
108 }
109
110 return $logs;
111 }
112 }
113