|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Domain\Common; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Common\Model\IspRestrictions; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | + |
| 10 | +class IspRestrictionsProvider |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + private readonly string $confPath, |
| 14 | + private readonly LoggerInterface $logger, |
| 15 | + ) { |
| 16 | + } |
| 17 | + |
| 18 | + public function load(): IspRestrictions |
| 19 | + { |
| 20 | + $contents = $this->readConfigFile(); |
| 21 | + if ($contents === null) { |
| 22 | + return new IspRestrictions(null, null, null); |
| 23 | + } |
| 24 | + |
| 25 | + [$raw, $maxBatch, $minBatchPeriod, $lockFile] = $this->parseContents($contents); |
| 26 | + |
| 27 | + $this->logIfDetected($maxBatch, $minBatchPeriod, $lockFile); |
| 28 | + |
| 29 | + return new IspRestrictions($maxBatch, $minBatchPeriod, $lockFile, $raw); |
| 30 | + } |
| 31 | + |
| 32 | + private function readConfigFile(): ?string |
| 33 | + { |
| 34 | + if (!is_file($this->confPath) || !is_readable($this->confPath)) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + $contents = file_get_contents($this->confPath); |
| 38 | + if ($contents === false) { |
| 39 | + $this->logger->warning('Cannot read ISP restrictions file', ['path' => $this->confPath]); |
| 40 | + return null; |
| 41 | + } |
| 42 | + return $contents; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return array{0: array<string,string>, 1: ?int, 2: ?int, 3: ?string} |
| 47 | + */ |
| 48 | + private function parseContents(string $contents): array |
| 49 | + { |
| 50 | + $maxBatch = null; |
| 51 | + $minBatchPeriod = null; |
| 52 | + $lockFile = null; |
| 53 | + $raw = []; |
| 54 | + |
| 55 | + foreach (preg_split('/\R/', $contents) as $line) { |
| 56 | + [$key, $val] = $this->parseLine($line); |
| 57 | + if ($key === null) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + $raw[$key] = $val; |
| 61 | + [$maxBatch, $minBatchPeriod, $lockFile] = $this->applyKeyValue( |
| 62 | + $key, |
| 63 | + $val, |
| 64 | + $maxBatch, |
| 65 | + $minBatchPeriod, |
| 66 | + $lockFile |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return [$raw, $maxBatch, $minBatchPeriod, $lockFile]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return array{0: ?string, 1: string} |
| 75 | + */ |
| 76 | + private function parseLine(string $line): array |
| 77 | + { |
| 78 | + $line = trim($line); |
| 79 | + if ($line === '' || str_starts_with($line, '#') || str_starts_with($line, ';')) { |
| 80 | + return [null, '']; |
| 81 | + } |
| 82 | + $parts = explode('=', $line, 2); |
| 83 | + if (\count($parts) !== 2) { |
| 84 | + return [null, '']; |
| 85 | + } |
| 86 | + |
| 87 | + return array_map('trim', $parts); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param string $key |
| 92 | + * @param string $val |
| 93 | + * @param ?int $maxBatch |
| 94 | + * @param ?int $minBatchPeriod |
| 95 | + * @param ?string $lockFile |
| 96 | + * @return array{0: ?int, 1: ?int, 2: ?string} |
| 97 | + */ |
| 98 | + private function applyKeyValue( |
| 99 | + string $key, |
| 100 | + string $val, |
| 101 | + ?int $maxBatch, |
| 102 | + ?int $minBatchPeriod, |
| 103 | + ?string $lockFile |
| 104 | + ): array { |
| 105 | + if ($key === 'maxbatch') { |
| 106 | + if ($val !== '' && ctype_digit($val)) { |
| 107 | + $maxBatch = (int) $val; |
| 108 | + } |
| 109 | + return [$maxBatch, $minBatchPeriod, $lockFile]; |
| 110 | + } |
| 111 | + if ($key === 'minbatchperiod') { |
| 112 | + if ($val !== '' && ctype_digit($val)) { |
| 113 | + $minBatchPeriod = (int) $val; |
| 114 | + } |
| 115 | + return [$maxBatch, $minBatchPeriod, $lockFile]; |
| 116 | + } |
| 117 | + if ($key === 'lockfile') { |
| 118 | + if ($val !== '') { |
| 119 | + $lockFile = $val; |
| 120 | + } |
| 121 | + return [$maxBatch, $minBatchPeriod, $lockFile]; |
| 122 | + } |
| 123 | + return [$maxBatch, $minBatchPeriod, $lockFile]; |
| 124 | + } |
| 125 | + |
| 126 | + private function logIfDetected(?int $maxBatch, ?int $minBatchPeriod, ?string $lockFile): void |
| 127 | + { |
| 128 | + if ($maxBatch !== null || $minBatchPeriod !== null || $lockFile !== null) { |
| 129 | + $this->logger->info('ISP restrictions detected', [ |
| 130 | + 'path' => $this->confPath, |
| 131 | + 'maxbatch' => $maxBatch, |
| 132 | + 'minbatchperiod' => $minBatchPeriod, |
| 133 | + 'lockfile' => $lockFile, |
| 134 | + ]); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments