| Total Complexity | 11 |
| Total Lines | 58 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class ReplyAggregator |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | private $replyDirectory; |
||
| 15 | |||
| 16 | public function __construct(string $replyDirectory = null) |
||
| 17 | { |
||
| 18 | if ($replyDirectory === null) { |
||
| 19 | $replyDirectory = \sys_get_temp_dir().'/replies'; |
||
| 20 | } |
||
| 21 | $this->replyDirectory = rtrim($replyDirectory, '/').'/'; |
||
| 22 | if (!\file_exists($replyDirectory)) { |
||
| 23 | \mkdir($replyDirectory, 0777, true); |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Purges all received replies |
||
| 29 | */ |
||
| 30 | public function clear(): void |
||
| 31 | { |
||
| 32 | $files = glob($this->replyDirectory.'*'); // get all file names |
||
| 33 | foreach ($files as $file) { // iterate files |
||
| 34 | if (is_file($file)) { |
||
| 35 | unlink($file); // delete file |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | private function getNextFileName(): string |
||
| 41 | { |
||
| 42 | $i = 0; |
||
| 43 | while (\file_exists($this->replyDirectory.'tmp'.$i)) { |
||
| 44 | $i++; |
||
| 45 | } |
||
| 46 | return 'tmp'.$i; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function storeReply(string $payload): void |
||
| 50 | { |
||
| 51 | $path = $this->replyDirectory.$this->getNextFileName(); |
||
| 52 | \file_put_contents($path, $payload); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return string[] |
||
| 57 | */ |
||
| 58 | public function getReplies(): array |
||
| 67 | } |
||
| 68 | } |
||
| 69 |