ReplyAggregator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 4
A clear() 0 6 3
A storeReply() 0 4 1
A getNextFileName() 0 7 2
A getReplies() 0 13 3
1
<?php
2
3
namespace TheAentMachine\Helper;
4
5
use Safe\Exceptions\FilesystemException;
6
use Safe\Exceptions\StringsException;
7
use function Safe\mkdir;
8
use function Safe\sprintf;
9
use function Safe\glob;
10
use function Safe\unlink;
11
use function Safe\file_put_contents;
12
use function Safe\file_get_contents;
13
14
/**
15
 * Class in charge of assembling replies from the different containers.
16
 */
17
final class ReplyAggregator
18
{
19
    /**
20
     * @var string
21
     */
22
    private $replyDirectory;
23
24
    /**
25
     * ReplyAggregator constructor.
26
     * @param string|null $replyDirectory
27
     * @throws FilesystemException
28
     * @throws StringsException
29
     */
30
    public function __construct(string $replyDirectory = null)
31
    {
32
        if ($replyDirectory === null) {
33
            $replyDirectory = \sys_get_temp_dir().'/replies';
34
        }
35
        $this->replyDirectory = rtrim($replyDirectory, '/').'/';
36
        if (!\file_exists($replyDirectory)) {
37
            mkdir($replyDirectory, 0777, true);
38
            if (!is_dir($replyDirectory)) {
39
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $replyDirectory));
40
            }
41
        }
42
    }
43
44
    /**
45
     * Purges all received replies
46
     * @throws FilesystemException
47
     */
48
    public function clear(): void
49
    {
50
        $files = glob($this->replyDirectory.'*'); // get all file names
51
        foreach ($files as $file) { // iterate files
52
            if (is_file($file)) {
53
                unlink($file); // delete file
54
            }
55
        }
56
    }
57
58
    private function getNextFileName(): string
59
    {
60
        $i = 0;
61
        while (\file_exists($this->replyDirectory.'tmp'.$i)) {
62
            $i++;
63
        }
64
        return 'tmp'.$i;
65
    }
66
67
    /**
68
     * @param string $payload
69
     * @throws FilesystemException
70
     */
71
    public function storeReply(string $payload): void
72
    {
73
        $path = $this->replyDirectory.$this->getNextFileName();
74
        file_put_contents($path, $payload);
75
    }
76
77
    /**
78
     * @return string[]
79
     * @throws FilesystemException
80
     */
81
    public function getReplies(): array
82
    {
83
        $i = 0;
84
        $replies = [];
85
        while (\file_exists($this->replyDirectory.'tmp'.$i)) {
86
            $content = file_get_contents($this->replyDirectory.'tmp'.$i);
87
            if ($content === false) {
88
                throw new \RuntimeException('Failed to load file '.$this->replyDirectory.'tmp'.$i);
89
            }
90
            $replies[] = $content;
91
            $i++;
92
        }
93
        return $replies;
94
    }
95
}
96