Debian Codegraph @

classes/ReportLog/ReportLogItem.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\ReportLog;
24
25 use Liuch\DmarcSrg\Core;
26 use Liuch\DmarcSrg\Sources\Source;
27 use Liuch\DmarcSrg\Exception\LogicException;
28 use Liuch\DmarcSrg\Exception\SoftException;
29 use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
30
31 class ReportLogItem
32 {
33 private $db = null;
34 private $data = [
35 'id' => null,
36 'domain' => null,
37 'report_id' => null,
38 'event_time' => null,
39 'filename' => null,
40 'source' => 0,
41 'success' => false,
42 'message' => null
43 ];
44
45 private function __construct($source, $filename, $db)
46 {
47 if (!is_null($source)) {
48 if (gettype($source) !== 'integer' || $source <= 0) {
49 throw new LogicException('Invalid parameter passed');
50 }
51 }
52 $this->data['source'] = $source;
53 $this->data['filename'] = gettype($filename) == 'string' ? $filename : null;
54 $this->db = $db ?? Core::instance()->database();
55 }
56
57 public static function success(int $source, $report, $filename, $message, $db = null)
58 {
59 $li = new ReportLogItem($source, $filename, $db);
60 $li->data['success'] = true;
61 $li->data['domain'] = $report->domain;
62 $li->data['report_id'] = $report->report_id;
63 $li->data['message'] = $message;
64 return $li;
65 }
66
67 public static function failed(int $source, $report, $filename, $message, $db = null)
68 {
69 $li = new ReportLogItem($source, $filename, $db);
70 $li->data['success'] = false;
71 if (!is_null($report)) {
72 $li->data['domain'] = $report->domain;
73 $li->data['report_id'] = $report->report_id;
74 } else {
75 $li->data['domain'] = null;
76 $li->data['report_id'] = null;
77 }
78 $li->data['message'] = $message;
79 return $li;
80 }
81
82 /**
83 * Returns an instance of ReportLogItem with the passed Id
84 *
85 * @param int $id Item Id to return
86 * @param \Liuch\DmarcSrg\Database\DatabaseController $db The database controller
87 *
88 * @return ReportLogItem an instance of ReportLogItem with the specified Id.
89 */
90 public static function byId(int $id, $db = null)
91 {
92 $li = new ReportLogItem(null, null, $db);
93 $li->data['id'] = $id;
94 try {
95 $li->db->getMapper('report-log')->fetch($li->data);
96 } catch (DatabaseNotFoundException $e) {
97 throw new SoftException('The log item is not found');
98 }
99 return $li;
100 }
101
102 /**
103 * Converts a string source value to integer
104 *
105 * @param string $source_str - string value to convert
106 *
107 * @return int
108 */
109 public static function stringToSource(string $source_str): int
110 {
111 switch ($source_str) {
112 case 'uploaded_file':
113 return Source::SOURCE_UPLOADED_FILE;
114 case 'email':
115 return Source::SOURCE_MAILBOX;
116 case 'directory':
117 return Source::SOURCE_DIRECTORY;
118 case 'remotefs':
119 return Source::SOURCE_REMOTE_FILESYSTEM;
120 }
121 return 0;
122 }
123
124 /**
125 * Converts an integer source value to a string representation
126 *
127 * Returns a string with the source name or an empty string if the integer value is incorrect.
128 *
129 * @param int $source - an integer value to convert
130 *
131 * @return string A string value of the passed source
132 */
133 public static function sourceToString(int $source): string
134 {
135 switch ($source) {
136 case Source::SOURCE_UPLOADED_FILE:
137 return 'uploaded_file';
138 case Source::SOURCE_MAILBOX:
139 return 'email';
140 case Source::SOURCE_DIRECTORY:
141 return 'directory';
142 case Source::SOURCE_REMOTE_FILESYSTEM:
143 return 'remotefs';
144 }
145 return '';
146 }
147
148 /**
149 * Returns an array with log item data
150 *
151 * @return array Log item data
152 */
153 public function toArray(): array
154 {
155 $res = $this->data;
156 $res['source'] = static::sourceToString($this->data['source']);
157 return $res;
158 }
159
160 /**
161 * Saves the report log item to the database
162 *
163 * @return void
164 */
165 public function save(): void
166 {
167 $this->db->getMapper('report-log')->save($this->data);
168 }
169 }