Debian Codegraph @

classes/ReportLog/ReportLog.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2020-2023 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 namespace Liuch\DmarcSrg\ReportLog;
24
25 use Liuch\DmarcSrg\Core;
26
27 class ReportLog
28 {
29 public const ORDER_ASCENT = 1;
30 public const ORDER_DESCENT = 2;
31
32 private $db = null;
33 private $filter = [];
34 private $order = [
35 'direction' => 'ascent'
36 ];
37 private $rec_limit = 0;
38
39 public function __construct($db = null)
40 {
41 $this->db = $db ?? Core::instance()->database();
42 }
43
44 public function setOrder(int $dir)
45 {
46 $this->order['direction'] = ($dir === self::ORDER_DESCENT ? 'descent' : 'ascent');
47 return $this;
48 }
49
50 public function setMaxCount(int $n)
51 {
52 $this->rec_limit = $n;
53 return $this;
54 }
55
56 /**
57 * Sets filter value for the list and for deleting report log items
58 *
59 * @param array $filter Key-value array:
60 * 'from_time' => DateTime, start from the passed event timestamp
61 * 'till_time' => DateTime, until the passed event timestamp not including it
62 * 'success' => bool, whether the report upload was successful
63 * 'source' => string, Report source type
64 *
65 * @return $this
66 */
67 public function setFilter(array $filter)
68 {
69 $this->filter = $filter;
70 if (isset($filter['source'])) {
71 $this->filter['source'] = ReportLogItem::stringToSource($filter['source']);
72 }
73 return $this;
74 }
75
76 public function count(): int
77 {
78 $limit = [ 'offset' => 0, 'count' => $this->rec_limit ];
79 return $this->db->getMapper('report-log')->count($this->filter, $limit);
80 }
81
82 public function getList(int $pos): array
83 {
84 $max_rec = $this->rec_limit > 0 ? $this->rec_limit : 25;
85
86 $limit = [ 'offset' => $pos, 'count' => $max_rec + 1 ];
87
88 $list = $this->db->getMapper('report-log')->list($this->filter, $this->order, $limit);
89 if (count($list) > $max_rec) {
90 $more = true;
91 unset($list[$max_rec]);
92 } else {
93 $more = false;
94 }
95 foreach ($list as &$it) {
96 $it['source'] = ReportLogItem::sourceToString($it['source']);
97 }
98 unset($it);
99
100 return [
101 'items' => $list,
102 'more' => $more
103 ];
104 }
105
106 public function delete()
107 {
108 $limit = [ 'offset' => 0, 'count' => $this->rec_limit ];
109 $this->db->getMapper('report-log')->delete($this->filter, $this->order, $limit);
110 }
111 }