Passed
Push — master ( f2f8c0...5c7f99 )
by Caen
02:56 queued 13s
created

ViewDiffService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

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