Passed
Push — master ( f7e90d...a11b53 )
by Caen
02:58 queued 12s
created

ChecksumService::unixsum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Services;
4
5
use Hyde\Framework\Hyde;
6
7
/**
8
 * Helper methods to interact with the filecache. The filecache is used to compare
9
 * published Blade views with the original Blade views in the Hyde Framework
10
 * so the user can be warned before overwriting their customizations.
11
 *
12
 * @see \Hyde\Framework\Testing\Feature\Services\ChecksumServiceTest
13
 */
14
class ChecksumService
15
{
16
    public static function getFilecache(): array
17
    {
18
        $filecache = [];
19
20
        $files = glob(Hyde::vendorPath('resources/views/**/*.blade.php'));
21
22
        foreach ($files as $file) {
23
            $filecache[str_replace(Hyde::vendorPath(), '', $file)] = [
24
                'unixsum' => static::unixsumFile($file),
25
            ];
26
        }
27
28
        return $filecache;
29
    }
30
31
    public static function getChecksums(): array
32
    {
33
        $cache = static::getFilecache();
34
35
        $checksums = [];
36
37
        foreach ($cache as $file) {
38
            $checksums[] = $file['unixsum'];
39
        }
40
41
        return $checksums;
42
    }
43
44
    public static function checksumMatchesAny(string $checksum): bool
45
    {
46
        return in_array($checksum, static::getChecksums());
47
    }
48
49
    /**
50
     * A EOL agnostic wrapper for calculating MD5 checksums.
51
     *
52
     * @internal This function is not cryptographically secure.
53
     *
54
     * @see https://github.com/hydephp/framework/issues/85
55
     */
56
    public static function unixsum(string $string): string
57
    {
58
        $string = str_replace(["\r\n", "\r"], "\n", $string);
59
60
        return md5($string);
61
    }
62
63
    /* Shorthand for @see static::unixsum() but loads a file */
64
    public static function unixsumFile(string $file): string
65
    {
66
        return static::unixsum(file_get_contents($file));
67
    }
68
}
69