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

ViewDiffService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 10
dl 0
loc 31
rs 10
c 6
b 1
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewFileHashIndex() 0 11 2
A checksumMatchesAny() 0 3 1
A getChecksums() 0 9 2
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