Total Complexity | 9 |
Total Lines | 39 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
10 | final class HashHelper extends AbstractStaticClass |
||
11 | { |
||
12 | /** |
||
13 | * @throws Exception |
||
14 | */ |
||
15 | 6 | public static function hashDirectory(string $path): string |
|
16 | { |
||
17 | 6 | if (!Filesystem::is_dir($path)) { |
|
18 | 2 | throw new RuntimeException("The given directory '$path' can not be found."); |
|
19 | } |
||
20 | |||
21 | 4 | $files = []; |
|
22 | 4 | $dir = Filesystem::dir($path); |
|
23 | |||
24 | 4 | while (($file = $dir->read()) !== false) { |
|
25 | 4 | if ($file !== '.' && $file !== '..') { |
|
26 | 4 | $files[] = Filesystem::is_dir($path . '/' . $file) |
|
27 | 2 | ? self::hashDirectory($path . '/' . $file) |
|
28 | 4 | : self::hashFile($path . '/' . $file); |
|
29 | } |
||
30 | } |
||
31 | |||
32 | 4 | $dir->close(); |
|
33 | |||
34 | 4 | return self::hashString(implode('', $files)); |
|
35 | } |
||
36 | |||
37 | 10 | public static function hashFile(string $path): string |
|
38 | { |
||
39 | 10 | if (!Filesystem::is_file($path)) { |
|
40 | 2 | throw new RuntimeException("The given file '$path' can not be found."); |
|
41 | } |
||
42 | |||
43 | 8 | return Md5::md5_file($path); |
|
44 | } |
||
45 | |||
46 | 8 | public static function hashString(string $string): string |
|
49 | } |
||
50 | } |
||
51 |