| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace Liuch\DmarcSrg\Directories; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Core; |
| 35 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class DirectoryList |
| 41 |
{ |
| 42 |
private $list = null; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function list(): array |
| 50 |
{ |
| 51 |
$this->ensureList(); |
| 52 |
return $this->list; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 135 |
} |
| 136 |
} |
| 137 |
} else { |
| 138 |
try { |
| 139 |
$this->list[] = new Directory(1, $directories); |
| 140 |
} catch (LogicException $e) { |
| 141 |
|
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |