1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace PmgDev\DatabaseReplicator\Source; |
||
4 | |||
5 | use Nette\Utils\FileSystem; |
||
6 | use PmgDev\DatabaseReplicator\Exceptions; |
||
7 | |||
8 | class Hash |
||
9 | { |
||
10 | /** @var string */ |
||
11 | private $name; |
||
12 | |||
13 | /** @var string */ |
||
14 | private $tempDir = ''; |
||
15 | |||
16 | /** @var string */ |
||
17 | private $md5 = ''; |
||
18 | |||
19 | /** @var int */ |
||
20 | private $expiration = 1800; // [s] default 30 min |
||
21 | |||
22 | /** @var Files */ |
||
23 | private $files; |
||
24 | |||
25 | |||
26 | public function __construct( |
||
27 | string $name, |
||
28 | string $tempDir, |
||
29 | Files $files |
||
30 | ) |
||
31 | { |
||
32 | Filesystem::createDir($tempDir); |
||
33 | $this->name = $name; |
||
34 | $this->tempDir = $tempDir; |
||
35 | $this->files = $files; |
||
36 | } |
||
37 | |||
38 | |||
39 | public function getFiles(): Files |
||
40 | { |
||
41 | return $this->files; |
||
42 | } |
||
43 | |||
44 | |||
45 | public function setExpiration(int $expiration): void |
||
46 | { |
||
47 | $this->expiration = max($expiration, 0); |
||
48 | } |
||
49 | |||
50 | |||
51 | public function begin(): void |
||
52 | { |
||
53 | $this->removeActiveFile(); |
||
54 | $this->md5 = $this->md5Dry(); |
||
55 | } |
||
56 | |||
57 | |||
58 | public function removeActiveFile(): void |
||
59 | { |
||
60 | @unlink($this->activeFile()); |
||
0 ignored issues
–
show
|
|||
61 | $this->md5 = ''; |
||
62 | } |
||
63 | |||
64 | |||
65 | public function getName(): string |
||
66 | { |
||
67 | return $this->name; |
||
68 | } |
||
69 | |||
70 | |||
71 | public function commit(): void |
||
72 | { |
||
73 | if ($this->md5 === '') { |
||
74 | throw new Exceptions\InvalidStateException('Let\'s start by method begin().'); |
||
75 | } |
||
76 | file_put_contents($this->activeFile(), $this->md5); |
||
77 | } |
||
78 | |||
79 | |||
80 | public function md5(): string |
||
81 | { |
||
82 | if ($this->md5 === '') { |
||
83 | $filePath = $this->activeFile(); |
||
84 | if (!is_file($filePath) || (time() - filemtime($filePath) >= $this->expiration)) { |
||
85 | throw new Exceptions\ActiveFileNotFoundException($filePath); |
||
86 | } |
||
87 | $this->md5 = (string) file_get_contents($filePath); |
||
88 | |||
89 | if ($this->md5 === '') { |
||
90 | throw new Exceptions\ActiveFileNotFoundException($filePath); |
||
91 | } |
||
92 | } |
||
93 | return $this->md5; |
||
94 | } |
||
95 | |||
96 | |||
97 | private function activeFile(): string |
||
98 | { |
||
99 | return $this->tempDir . DIRECTORY_SEPARATOR . $this->name; |
||
100 | } |
||
101 | |||
102 | |||
103 | private function md5Dry(): string |
||
104 | { |
||
105 | return md5($this->getName() . $this->md5Files()); |
||
106 | } |
||
107 | |||
108 | |||
109 | private function md5Files(): string |
||
110 | { |
||
111 | $token = ''; |
||
112 | foreach ($this->files as $filepath) { |
||
113 | $token .= '.' . md5_file($filepath); |
||
114 | } |
||
115 | return $token; |
||
116 | } |
||
117 | |||
118 | } |
||
119 |
If you suppress an error, we recommend checking for the error condition explicitly: