Debian Codegraph @

classes/Report/Report.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 namespace Liuch\DmarcSrg\Report;
24
25 use Liuch\DmarcSrg\Core;
26 use Liuch\DmarcSrg\Domains\Domain;
27 use Liuch\DmarcSrg\Exception\SoftException;
28 use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
29
30 class Report
31 {
32 private $db = null;
33 private $data = null;
34
35 public function __construct($data, $db = null)
36 {
37 if (is_array($data)) {
38 $domain = $data['domain'];
39 $data = ReportData::fromArray($data);
40 } else {
41 $domain = $data->domain;
42 }
43 if ($domain instanceof Domain) {
44 $data->domain = $domain->fqdn();
45 }
46 $this->data = $data;
47 $this->db = $db ?? Core::instance()->database();
48 }
49
50 public static function fromXmlFile($fd)
51 {
52 $data = ReportData::fromXmlFile($fd);
53 if (!$data->isValid()) {
54 throw new SoftException('Incorrect or incomplete report data');
55 }
56 return new Report($data);
57 }
58
59 public function fetch()
60 {
61 $this->prepareData(true);
62 try {
63 $this->db->getMapper('report')->fetch($this->data);
64 } catch (DatabaseNotFoundException $e) {
65 throw new SoftException('Report not found');
66 }
67 }
68
69 public function save(string $real_fname)
70 {
71 $b_ts = $this->data->date['begin'];
72 $e_ts = $this->data->date['end'];
73 if (!$b_ts->getTimestamp() || !$e_ts->getTimestamp()
74 || strlen($b_ts->format('Y')) !== 4 || strlen($e_ts->format('Y')) !== 4
75 ) {
76 throw new SoftException('Failed to add an incoming report: wrong date value');
77 }
78 if ($b_ts > $e_ts) {
79 throw new SoftException('Failed to add an incoming report: start date is later than end date');
80 }
81
82 $this->db->getMapper('report')->save($this->data);
83 return [ 'message' => 'The report is loaded successfully' ];
84 }
85
86 public function __get(string $name)
87 {
88 return $this->data->$name;
89 }
90
91 public function toArray(): array
92 {
93 return $this->data->toArray();
94 }
95
96 public function set($name, $value)
97 {
98 $this->prepareData(false);
99 $this->db->getMapper('report')->setProperty($this->data, $name, $value);
100 return [ 'message' => 'Ok' ];
101 }
102
103 /**
104 * Checks and prepares report data for queries
105 *
106 * @param bool $replace If true, it leaves only the data required for the request
107 *
108 * @return void
109 */
110 private function prepareData(bool $replace): void
111 {
112 $data = [];
113 $ne_f = false;
114 foreach ([ 'domain', 'date', 'org_name', 'report_id'] as $fld) {
115 if (empty($this->data->$fld)) {
116 $ne_f = true;
117 break;
118 }
119 if ($replace) {
120 $data[$fld] = $this->data->$fld;
121 }
122 }
123 if ($ne_f || empty($this->data->date['begin'])) {
124 throw new SoftException('Not enough data to identify the report');
125 }
126 if ($replace) {
127 $this->data = ReportData::fromArray($data);
128 }
129 }
130 }