PluginProbe
Packeta / trunk
Packeta vtrunk
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 trunk, at src/Packetery/Module/Log/DbLogger.php

116 lines 2.4 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 /**
40 * @return int|false The number of rows inserted, or false on error.
41 */
42 public function add( Record $record ) {
43 if ( $record->date === null ) {
44 $record->date = CoreHelper::now();
45 }
46
47 return $this->logRepository->save( $record );
48 }
49
50 /**
51 * Gets records.
52 *
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.
57 *
58 * @return \Generator<Record>|array{}
59 * @throws \Exception From DateTimeImmutable.
60 */
61 public function getRecords( ?int $orderId, ?string $action, array $sorting = [], int $limit = 100 ): iterable {
62 $arguments = [
63 'orderby' => $sorting,
64 'limit' => $limit,
65 ];
66
67 if ( is_numeric( $orderId ) ) {
68 $arguments['order_id'] = $orderId;
69 }
70 if ( $action !== null ) {
71 $arguments['action'] = $action;
72 }
73
74 $logs = $this->logRepository->find( $arguments );
75 if ( ! $logs instanceof \Generator ) {
76 return [];
77 }
78
79 return $logs;
80 }
81
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 /**
95 * Gets logs for given period as array.
96 *
97 * @param array<array<string, string>> $dateQuery Date_query compatible array.
98 *
99 * @return \Generator<Record>|array{}
100 * @throws \Exception From DateTimeImmutable.
101 */
102 public function getForPeriodAsArray( array $dateQuery ) {
103 $arguments = [
104 'orderby' => [ 'date' => 'ASC' ],
105 'date_query' => $dateQuery,
106 ];
107
108 $logs = $this->logRepository->find( $arguments );
109 if ( ! $logs instanceof \Generator ) {
110 return [];
111 }
112
113 return $logs;
114 }
115 }
116