Debian Codegraph @

classes/Sources/RemoteFilesystemSource.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) 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 RemoteFilesystemSource
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\Core;
35 use Liuch\DmarcSrg\ReportFile\ReportFile;
36 use Liuch\DmarcSrg\Exception\RuntimeException;
37
38 /**
39 * This class is designed to process report files from remote filesystems.
40 */
41 class RemoteFilesystemSource extends Source
42 {
43 private $index = 0;
44 private $f_attr = null;
45 private $params = null;
46 private $iterator = null;
47
48 /**
49 * Sets parameters that difine the behavior of the source
50 *
51 * @param $params Key-value array
52 * 'when_done' => one or more rules to be executed after successful report processing
53 * (array|string)
54 * 'when_failed' => one or more rules to be executed after report processing fails
55 * (array|string)
56 *
57 * @return void
58 */
59 public function setParams(array $params): void
60 {
61 $this->params = [];
62 $this->params['when_done'] = SourceAction::fromSetting(
63 $params['when_done'] ?? [],
64 SourceAction::FLAG_BASENAME,
65 'delete'
66 );
67 $this->params['when_failed'] = SourceAction::fromSetting(
68 $params['when_failed'] ?? [],
69 SourceAction::FLAG_BASENAME,
70 'move_to:failed'
71 );
72 }
73
74 /**
75 * Returns an instance of the ReportFile class for the current item.
76 *
77 * @return ReportFile
78 */
79 public function current(): object
80 {
81 if (!$this->f_attr) {
82 $this->f_attr = $this->iterator->current();
83 }
84 $path = $this->f_attr->path();
85 return ReportFile::fromStream(
86 $this->data->readStream($path),
87 basename($path),
88 $this->f_attr->mimeType()
89 );
90 }
91
92 /**
93 * Returns the index of the current item.
94 *
95 * @return int
96 */
97 public function key(): int
98 {
99 return $this->index;
100 }
101
102 /**
103 * Moves forward to the next item.
104 *
105 * @return void
106 */
107 public function next(): void
108 {
109 $this->f_attr = null;
110 $this->iterator->next();
111 ++$this->index;
112 }
113
114 /**
115 * Rewinds the position to the first item.
116 *
117 * @return void
118 */
119 public function rewind(): void
120 {
121 $this->iterator = $this->data->listFiles()->getIterator();
122 $this->index = 0;
123 }
124
125 /**
126 * Checks if the current postion is valid
127 *
128 * @return bool
129 */
130 public function valid(): bool
131 {
132 return $this->iterator->valid();
133 }
134
135 /**
136 * Processes the accepted report file according to the settings
137 *
138 * @return void
139 */
140 public function accepted(): void
141 {
142 $this->processReportFileActions($this->params['when_done']);
143 }
144
145 /**
146 * Processes the rejected report file according to the settings
147 *
148 * @return void
149 */
150 public function rejected(): void
151 {
152 $this->processReportFileActions($this->params['when_failed']);
153 }
154
155 /**
156 * Returns type of the source.
157 *
158 * @return int
159 */
160 public function type(): int
161 {
162 return Source::SOURCE_REMOTE_FILESYSTEM;
163 }
164
165 /**
166 * Logs an error message
167 *
168 * @param string $message
169 */
170 private function logError(string $message): void
171 {
172 Core::instance()->logger()->error($message);
173 }
174
175 /**
176 * Processes the current report file according to settings
177 *
178 * @param array $actions List of actions to apply to the file
179 *
180 * @return void
181 */
182 private function processReportFileActions(array &$actions): void
183 {
184 foreach ($actions as $sa) {
185 switch ($sa->type) {
186 case SourceAction::ACTION_DELETE:
187 $this->deleteReportFile();
188 break;
189 case SourceAction::ACTION_MOVE:
190 $this->moveReportFile($sa->param);
191 break;
192 }
193 }
194 }
195
196 /**
197 * Deletes the current report file
198 *
199 * @return void
200 */
201 private function deleteReportFile(): void
202 {
203 $path = $this->f_attr->path();
204 try {
205 $this->data->delete($path);
206 } catch (\ErrorException $e) {
207 $error_message = "Error deleting file from remote filesystem: {$path}";
208 $this->logError($error_message);
209 throw new RuntimeException($error_message, -1, $e);
210 }
211 }
212
213 /**
214 * Moves the current report file
215 *
216 * @param string $dir_name Directory name where to move the report file to
217 *
218 * @return void
219 */
220 private function moveReportFile(string $dir_name): void
221 {
222 $sou_path = $this->f_attr->path();
223 $des_path = dirname($sou_path) . '/' . $dir_name . '/' . basename($sou_path);
224 try {
225 $this->data->move($sou_path, $des_path);
226 } catch (\ErrorException $e) {
227 $error_message = "Error moving file within remote filesystem: {$sou_path} to {$des_path}";
228 $this->logError($error_message);
229 throw new RuntimeException($error_message, -1, $e);
230 }
231 }
232 }