Debian Codegraph @

classes/Sources/MailboxSource.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) 2021-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 *
24 * This file contains the class MailboxSource
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\Sources;
33
34 use Liuch\DmarcSrg\Mail\MailBox;
35 use Liuch\DmarcSrg\ReportFile\ReportFile;
36 use Liuch\DmarcSrg\Exception\SoftException;
37 use Liuch\DmarcSrg\Exception\RuntimeException;
38
39 /**
40 * This class is designed to process report files from an mail box.
41 */
42 class MailboxSource extends Source
43 {
44 private $list = null;
45 private $index = 0;
46 private $msg = null;
47 private $params = null;
48
49 /**
50 * Sets parameters that difine the behavior of the source
51 *
52 * @param $params Key-value array
53 * 'when_done' => one or more rules to be executed after successful report processing
54 * (array|string)
55 * 'when_failed' => one or more rules to be executed after report processing fails
56 * (array|string)
57 *
58 * @return void
59 */
60 public function setParams(array $params): void
61 {
62 $this->params = [];
63 $this->params['when_done'] = SourceAction::fromSetting(
64 $params['when_done'] ?? [],
65 0,
66 'mark_seen'
67 );
68 $this->params['when_failed'] = SourceAction::fromSetting(
69 $params['when_failed'] ?? [],
70 0,
71 'move_to:failed'
72 );
73 }
74
75 /**
76 * Returns an instance of the ReportFile class for the current email message.
77 *
78 * @return ReportFile
79 */
80 public function current(): object
81 {
82 $this->msg = $this->list[$this->index];
83 try {
84 $this->msg->validate();
85 } catch (SoftException $e) {
86 throw new SoftException('Incorrect message: ' . $e->getMessage(), $e->getCode());
87 } catch (RuntimeException $e) {
88 throw new RuntimeException('Incorrect message', -1, $e);
89 }
90 $att = $this->msg->attachment();
91 return ReportFile::fromStream($att->datastream(), $att->filename(), $att->mimeType());
92 }
93
94 /**
95 * Returns the index of the currect email message.
96 *
97 * @return int
98 */
99 public function key(): int
100 {
101 return $this->index;
102 }
103
104 /**
105 * Moves forward to the next email message
106 *
107 * @return void
108 */
109 public function next(): void
110 {
111 $this->msg = null;
112 ++$this->index;
113 }
114
115 /**
116 * Gets a list of unread messages and rewinds the position to the first email message.
117 *
118 * @return void
119 */
120 public function rewind(): void
121 {
122 $this->msg = null;
123 $this->list = $this->data->messages(MailBox::SEARCH_UNSEEN, MailBox::ORDER_ASCENT);
124 $this->index = 0;
125 if (is_null($this->params)) {
126 $this->setParams([]);
127 }
128 }
129
130 /**
131 * Checks if the current postion is valid
132 *
133 * @return bool
134 */
135 public function valid(): bool
136 {
137 return isset($this->list[$this->index]);
138 }
139
140 /**
141 * Processes the accepted email messages according to the settings
142 *
143 * @return void
144 */
145 public function accepted(): void
146 {
147 if ($this->msg) {
148 $this->processMessageActions($this->params['when_done']);
149 }
150 }
151
152 /**
153 * Processes the rejected email messages according to the settings
154 *
155 * @return void
156 */
157 public function rejected(): void
158 {
159 $this->processMessageActions($this->params['when_failed']);
160 }
161
162 /**
163 * Returns type of the source.
164 *
165 * @return int
166 */
167 public function type(): int
168 {
169 return Source::SOURCE_MAILBOX;
170 }
171
172 /**
173 * Returns the current email message.
174 *
175 * @return \Liuch\DmarcSrg\Mail\MailMessage|null
176 */
177 public function mailMessage()
178 {
179 return $this->msg;
180 }
181
182 /**
183 * Processes the current report message according to settings
184 *
185 * @param array $actions List of actions to apply to the message
186 *
187 * @return void
188 */
189 private function processMessageActions(array &$actions): void
190 {
191 foreach ($actions as $sa) {
192 switch ($sa->type) {
193 case SourceAction::ACTION_SEEN:
194 $this->markMessageSeen();
195 break;
196 case SourceAction::ACTION_MOVE:
197 $this->moveMessage($sa->param);
198 break;
199 case SourceAction::ACTION_DELETE:
200 $this->deleteMessage();
201 break;
202 }
203 }
204 }
205
206 /**
207 * Marks the current report message as seen
208 *
209 * @return void
210 */
211 public function markMessageSeen(): void
212 {
213 $this->msg->markSeen();
214 }
215
216 /**
217 * Moves the current report message
218 *
219 * @param string $folder Child mailbox name where to move the current message to.
220 * If the target mailbox does not exists, it will be created.
221 *
222 * @return void
223 */
224 private function moveMessage(string $folder): void
225 {
226 $this->data->ensureMailbox($folder);
227 $this->msg->move($folder);
228 }
229
230 /**
231 * Deletes the current report message
232 *
233 * @return void
234 */
235 private function deleteMessage(): void
236 {
237 $this->msg->delete();
238 }
239 }