Debian Codegraph @

classes/Statistics.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-2024 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 *
24 * This file contains the class Statistics
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg;
33
34 use Liuch\DmarcSrg\DateTime;
35
36 /**
37 * This class is designed to get statistics on DMARC reports of a specified period
38 */
39 class Statistics
40 {
41 /** @var Database\DatabaseController*/
42 private $db = null;
43 /** @var Domains\Domain|null */
44 private $domain = null;
45 /** @var Users\User|null */
46 private $user = null;
47 /** @var array<string,DateTime|null> */
48 private $range = [
49 'date1' => null,
50 'date2' => null
51 ];
52 private $filter = [];
53
54 /**
55 * The constructor of the class, it only uses in static methods of this class
56 *
57 * @param Domains\Domain|null $domain The domain for which you need to get statistics,
58 * null for all the domains.
59 * @param Database\DatabaseController|null $db The database controller
60 *
61 * @return void
62 */
63 private function __construct($domain, $db)
64 {
65 $this->domain = $domain;
66 $this->db = $db ?? Core::instance()->database();
67 }
68
69 /**
70 * Returns an instance of the class for the period from $date1 to $date2
71 *
72 * @param Domains\Domain|null $domain See the constructor for the details
73 * @param DateTime $date1 The date you need statistics from
74 * @param DateTime $date2 The date you need statistics to (not included)
75 * @param Database\DatabaseController $db The database controller
76 *
77 * @return Statistics Instance of the class
78 */
79 public static function fromTo($domain, $date1, $date2, $db = null)
80 {
81 $r = new Statistics($domain, $db);
82 $r->range['date1'] = $date1;
83 $r->range['date2'] = $date2;
84 return $r;
85 }
86
87 /**
88 * Returns an instance of the class for the last week
89 *
90 * @param Domains\Domain|null $domain See the constructor for the details
91 * @param int $offset Offset to the past (weeks)
92 * @param Database\DatabaseController $db The database controller
93 *
94 * @return Statistics Instance of the class
95 */
96 public static function lastWeek($domain, int $offset = 0, $db = null)
97 {
98 $r = new Statistics($domain, $db);
99 $r->range['date1'] = new DateTime('monday last week');
100 if ($offset > 0) {
101 $r->range['date1']->sub(new \DateInterval("P{$offset}W"));
102 }
103 $r->range['date2'] = (clone $r->range['date1'])->add(new \DateInterval('P1W'));
104 return $r;
105 }
106
107 /**
108 * Returns an instance of the class for the last month
109 *
110 * @param Domains\Domain|null $domain See the construct for the details
111 * @param int $offset Offset to the past (months)
112 * @param Database\DatabaseController $db The database controller
113 *
114 * @return Statistics Instance of the class
115 */
116 public static function lastMonth($domain, int $offset = 0, $db = null)
117 {
118 $r = new Statistics($domain, $db);
119 $r->range['date1'] = new DateTime('midnight first day of last month');
120 if ($offset > 0) {
121 $r->range['date1']->sub(new \DateInterval("P{$offset}M"));
122 }
123 $r->range['date2'] = (clone $r->range['date1'])->add(new \DateInterval('P1M'));
124 return $r;
125 }
126
127 /**
128 * Returns an instance of the class for the last N days
129 *
130 * @param Domains\Domain|null $domain See the construct for the details
131 * @param int $ndays Number of days
132 * @param int $offset Offset to the past (days)
133 * @param Database\DatabaseController $db The database controller
134 *
135 * @return Statistics Instance of the class
136 */
137 public static function lastNDays($domain, int $ndays, int $offset = 0, $db = null)
138 {
139 $r = new Statistics($domain, $db);
140 $r->range['date2'] = new DateTime('midnight');
141 if ($offset > 0) {
142 $r->range['date2']->sub(new \DateInterval("P{$offset}D"));
143 }
144 $r->range['date1'] = (clone $r->range['date2'])->sub(new \DateInterval("P{$ndays}D"));
145 return $r;
146 }
147
148 /**
149 * Sets the user for whom statistics are generated
150 *
151 * @param Users\User $user User to generate statistics
152 *
153 * @return self
154 */
155 public function setUser($user)
156 {
157 $this->user = $user;
158 return $this;
159 }
160
161 /**
162 * Returns the date from and the date to in an array
163 *
164 * @return DateTime[] - The range of the statistics
165 */
166 public function range(): array
167 {
168 return [ (clone $this->range['date1']), (clone $this->range['date2'])->sub(new \DateInterval('PT1S')) ];
169 }
170
171 /**
172 * Returns summary information for e-mail messages as an array
173 *
174 * @return array Array with summary information
175 */
176 public function summary(): array
177 {
178 return $this->db->getMapper('statistics')->summary(
179 $this->domain,
180 $this->range,
181 $this->filter,
182 $this->user ? $this->user->id() : 0
183 );
184 }
185
186 /**
187 * Returns a list of ip-addresses from which the e-mail messages were received, with some statistics for each one
188 *
189 * @return array A list of ip-addresses with fields
190 * `ip`, `emails`, `dkim_aligned`, `spf_aligned`
191 */
192 public function ips(): array
193 {
194 return $this->db->getMapper('statistics')->ips(
195 $this->domain,
196 $this->range,
197 $this->filter,
198 $this->user ? $this->user->id() : 0
199 );
200 }
201
202 /**
203 * Returns a list of organizations that sent the reports with some statistics for each one
204 *
205 * @return array List of organizations with fields `name`, `reports`, `emails`
206 */
207 public function organizations(): array
208 {
209 return $this->db->getMapper('statistics')->organizations(
210 $this->domain,
211 $this->range,
212 $this->filter,
213 $this->user ? $this->user->id() : 0
214 );
215 }
216
217 /**
218 * Sets filter values for the returning information
219 *
220 * @param array $filter Key-value array with filtering parameters:
221 * 'dkim' => string, 'fail' or 'pass'
222 * 'organization' => string
223 * 'spf' => string, 'fail' or 'pass'
224 * 'status' => string, 'read' or 'unread'
225 *
226 * @return void
227 */
228 public function setFilter(array $filter): void
229 {
230 $this->filter = $filter;
231 }
232 }