Debian Codegraph @

classes/Directories/Directory.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 Directory
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\ErrorHandler;
35 use Liuch\DmarcSrg\Exception\SoftException;
36 use Liuch\DmarcSrg\Exception\LogicException;
37 use Liuch\DmarcSrg\Exception\RuntimeException;
38
39 /**
40 * This class is designed to work with the report directories which are listed in the configuration file.
41 */
42 class Directory
43 {
44 private $id = null;
45 private $name = null;
46 private $location = null;
47
48 /**
49 * It's the constructor of the class
50 *
51 * @param int $id Id of the directory. In fact, it is a serial number in the configuration file.
52 * @param array $data An array with the following fields:
53 * `location` (string) - Location of the directory in the file system.
54 * `name` (string) - Name of the directory. It is optional.
55 *
56 * @return void
57 */
58 public function __construct(int $id, array $data)
59 {
60 if (isset($data['name']) && gettype($data['name']) !== 'string') {
61 throw new LogicException('Directory name must be either null or a string value');
62 }
63 if (!isset($data['location']) || gettype($data['location']) !== 'string') {
64 throw new LogicException('Directory location must be a string value');
65 }
66 if (empty($data['location'])) {
67 throw new LogicException('Directory location must not be an empty string');
68 }
69
70 $this->id = $id;
71 $this->name = $data['name'] ?? null;
72 $this->location = $data['location'];
73 if (empty($this->name)) {
74 $this->name = 'Directory ' . $this->id;
75 }
76 if (substr($this->location, -1) !== '/') {
77 $this->location .= '/';
78 }
79 }
80
81 /**
82 * Returns an array with directory configuration data.
83 *
84 * @return array
85 */
86 public function toArray(): array
87 {
88 return [
89 'id' => $this->id,
90 'name' => $this->name,
91 'location' => $this->location
92 ];
93 }
94
95 /**
96 * Checks the existence and accessibility of the directory. Returns the result as an array.
97 *
98 * @return array
99 */
100 public function check(): array
101 {
102 try {
103 self::checkPath($this->location, true);
104 self::checkPath($this->location . 'failed/', false);
105 } catch (RuntimeException $e) {
106 return ErrorHandler::exceptionResult($e);
107 }
108
109 return [
110 'error_code' => 0,
111 'message' => 'Successfully',
112 'status' => [
113 'files' => $this->count()
114 ]
115 ];
116 }
117
118 /**
119 * Returns the total number of files in the directory.
120 *
121 * @return int
122 */
123 public function count(): int
124 {
125 $cnt = 0;
126 try {
127 $fs = new \FilesystemIterator($this->location);
128 } catch (\Exception $e) {
129 throw new RuntimeException("Error accessing directory {$this->location}", -1, $e);
130 }
131 foreach ($fs as $entry) {
132 if ($entry->isFile()) {
133 ++$cnt;
134 }
135 }
136 return $cnt;
137 }
138
139 /**
140 * Checks accessibility of a directory by its path. Throws an exception in case of any error.
141 *
142 * @param string $path Path to the directory to check.
143 * @param bool $existence If true, the absence of the directory causes an error.
144 *
145 * @return void
146 */
147 private static function checkPath(string $path, bool $existence): void
148 {
149 if (!file_exists($path)) {
150 if ($existence) {
151 throw new SoftException($path . ' directory does not exist!');
152 }
153 return;
154 }
155 if (!is_dir($path)) {
156 throw new SoftException($path . ' is not a directory!');
157 }
158 if (!is_readable($path)) {
159 throw new SoftException($path . ' directory is not readable!');
160 }
161 if (!is_writable($path)) {
162 throw new SoftException($path . ' directory is not writable!');
163 }
164 }
165 }