Debian Codegraph @

classes/RemoteFilesystems/RemoteFilesystemList.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 RemoteFilesystemList
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\RemoteFilesystems;
33
34 use Liuch\DmarcSrg\Core;
35 use Liuch\DmarcSrg\Exception\LogicException;
36
37 /**
38 * This class is designed to work with the list of remote filesystems which are listed in the configuration file.
39 */
40 class RemoteFilesystemList
41 {
42 private $list = null;
43 private $silent = false;
44
45 /**
46 * Constructor
47 *
48 * @param bool $silent If true, it skips incorrect items, otherwise it throws an exception
49 */
50 public function __construct(bool $silent)
51 {
52 $this->silent = $silent;
53 }
54
55 /**
56 * Returns a list of remote filesystems for the setting file
57 *
58 * @return array Array with instances of RemoteFilesystem class
59 */
60 public function list(): array
61 {
62 $this->ensureList();
63 return $this->list;
64 }
65
66 /**
67 * Returns an instance of the RemoteFilesystem class by its Id
68 *
69 * @param int $id Id of the required filesystem
70 *
71 * @return RemoteFilesystem
72 */
73 public function filesystem(int $id)
74 {
75 $this->ensureList();
76 if ($id <= 0 || $id > count($this->list)) {
77 throw new LogicException('Incorrect filesystem Id');
78 }
79 return $this->list[$id - 1];
80 }
81
82 /**
83 * Checks the accessibility of the specified filesystem or all the filesystem from configuration file if $id is 0.
84 *
85 * @param int $id RemoteFilesystem Id to check
86 *
87 * @return array Result array with `error_code` and `message` fields. For one filesystem and if there is no error,
88 * a field `status` will be added to the result.
89 */
90 public function check(int $id): array
91 {
92 if ($id !== 0) {
93 $fs = $this->filesystem($id);
94 return $fs->check();
95 }
96
97 $this->ensureList();
98 $results = [];
99 $err_cnt = 0;
100 $fs_cnt = count($this->list);
101 for ($i = 0; $i < $fs_cnt; ++$i) {
102 $r = $this->list[$i]->check();
103 if ($r['error_code'] !== 0) {
104 ++$err_cnt;
105 }
106 $results[] = $r;
107 }
108 $res = [];
109 if ($err_cnt === 0) {
110 $res['error_code'] = 0;
111 $res['message'] = 'Successfully';
112 } else {
113 $res['error_code'] = -1;
114 $res['message'] = sprintf('%d of %d filesystems have failed the check', $err_cnt, $fs_cnt);
115 }
116 $res['results'] = $results;
117 return $res;
118 }
119
120 /**
121 * Creates an array of filesystems from the configuration file if it does not exist
122 * for using in other methods of the class.
123 *
124 * @return void
125 */
126 private function ensureList(): void
127 {
128 if (!is_null($this->list)) {
129 return;
130 }
131
132 $filesystems = Core::instance()->config('remote_filesystems');
133
134 $this->list = [];
135 if (is_array($filesystems)) {
136 $cnt = count($filesystems);
137 if ($cnt > 0) {
138 if (isset($filesystems[0])) {
139 $id = 1;
140 foreach ($filesystems as &$fs) {
141 try {
142 $this->list[] = new RemoteFilesystem($id, $fs);
143 ++$id;
144 } catch (LogicException $e) {
145 if (!$this->silent) {
146 throw $e;
147 }
148 }
149 }
150 unset($fs);
151 } else {
152 try {
153 $this->list[] = new RemoteFilesystem(1, $filesystems);
154 } catch (LogicException $e) {
155 if (!$this->silent) {
156 throw $e;
157 }
158 }
159 }
160 }
161 }
162 }
163 }