Debian Codegraph @

classes/Directories/DirectoryList.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-2022 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 DirectoryList
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\Directories;
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 report directories which are listed in the configuration file.
39 */
40 class DirectoryList
41 {
42 private $list = null;
43
44 /**
45 * Returns a list of directories for the setting file
46 *
47 * @return array Array with instances of Directory class
48 */
49 public function list(): array
50 {
51 $this->ensureList();
52 return $this->list;
53 }
54
55 /**
56 * Returns an instance of the Directory class by its Id
57 *
58 * @param int $id Id of the required directory
59 *
60 * @return Directory
61 */
62 public function directory(int $id)
63 {
64 $this->ensureList();
65 if ($id <= 0 || $id > count($this->list)) {
66 throw new LogicException('Incorrect directory Id');
67 }
68 return $this->list[$id - 1];
69 }
70
71 /**
72 * Checks the accessibility of the specified directory or all the directories from configuration file if $id is 0.
73 *
74 * @param int $id Directory Id to check
75 *
76 * @return array Result array with `error_code` and `message` fields. For one directory and if there is no error,
77 * a field `status` will be added to the result.
78 */
79 public function check(int $id): array
80 {
81 if ($id !== 0) {
82 $dir = $this->directory($id);
83 return $dir->check();
84 }
85
86 $this->ensureList();
87 $results = [];
88 $err_cnt = 0;
89 $dir_cnt = count($this->list);
90 for ($i = 0; $i < $dir_cnt; ++$i) {
91 $r = $this->list[$i]->check();
92 if ($r['error_code'] !== 0) {
93 ++$err_cnt;
94 }
95 $results[] = $r;
96 }
97 $res = [];
98 if ($err_cnt === 0) {
99 $res['error_code'] = 0;
100 $res['message'] = 'Successfully';
101 } else {
102 $res['error_code'] = -1;
103 $res['message'] = sprintf('%d of %d directories have failed the check', $err_cnt, $dir_cnt);
104 }
105 $res['results'] = $results;
106 return $res;
107 }
108
109 /**
110 * Creates an array of directories from the configuration file if it does not exist
111 * for using in other methods of the class.
112 *
113 * @return void
114 */
115 private function ensureList(): void
116 {
117 if (!is_null($this->list)) {
118 return;
119 }
120
121 $directories = Core::instance()->config('directories');
122
123 $this->list = [];
124 if (is_array($directories)) {
125 $cnt = count($directories);
126 if ($cnt > 0) {
127 if (isset($directories[0])) {
128 $id = 1;
129 for ($i = 0; $i < $cnt; ++$i) {
130 try {
131 $this->list[] = new Directory($id, $directories[$i]);
132 ++$id;
133 } catch (LogicException $d) {
134 // Just ignore this directory setting.
135 }
136 }
137 } else {
138 try {
139 $this->list[] = new Directory(1, $directories);
140 } catch (LogicException $e) {
141 // Just ignore this directory setting.
142 }
143 }
144 }
145 }
146 }
147 }