Passed
Push — master ( 794674...e99464 )
by Caen
03:59 queued 13s
created

ViewDiffService::checksumMatchesAny()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use function glob;
8
use Hyde\Hyde;
9
use function Hyde\unixsum_file;
10
use function in_array;
11
use function str_replace;
12
use function unslash;
13
14
/**
15
 * @internal This class may be refactored to better suit its intended purpose.
16
 *
17
 * Helper methods to interact with the virtual filecache that is used to compare
18
 * published Blade views with the original Blade views in the Hyde Framework
19
 * so the user can be warned before overwriting their customizations.
20
 *
21
 * @see \Hyde\Framework\Testing\Feature\Services\ViewDiffServiceTest
22
 */
23
class ViewDiffService
24
{
25
    /** @return array<string, array{unixsum: string}> */
26
    public static function getViewFileHashIndex(): array
27
    {
28
        $filecache = [];
29
30
        foreach (glob(Hyde::vendorPath('resources/views/**/*.blade.php')) as $file) {
31
            $filecache[unslash(str_replace(Hyde::vendorPath(), '', (string) $file))] = [
32
                'unixsum' => unixsum_file($file),
33
            ];
34
        }
35
36
        return $filecache;
37
    }
38
39
    /** @return array<string> */
40
    public static function getChecksums(): array
41
    {
42
        $checksums = [];
43
44
        foreach (static::getViewFileHashIndex() as $file) {
45
            $checksums[] = $file['unixsum'];
46
        }
47
48
        return $checksums;
49
    }
50
51
    public static function checksumMatchesAny(string $checksum): bool
52
    {
53
        return in_array($checksum, static::getChecksums());
54
    }
55
}
56