HashHelper::hashDirectory()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 1
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hgraca\DoctrineTestDbRegenerationBundle\StdLib;
6
7
use Exception;
8
use RuntimeException;
9
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
47
    {
48 8
        return Md5::md5($string);
49
    }
50
}
51