Test Failed
Pull Request — master (#74)
by Peter
12:33 queued 05:48
created

NativeCache   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteCacheFileIfConfigFileWasChanged() 0 6 2
B getCachedPathToFiles() 0 29 7
A cacheFiles() 0 17 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace YamlStandards\Model\Component\Cache;
6
7
use YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound;
8
9
class NativeCache implements CacheInterface
10
{
11
    public const CACHE_FILE_NAME = 'yaml-standards.cache';
12
    public const CONFIG_NUMBER_FOR_CONFIG_FILE = 0;
13
14
    /**
15
     * @inheritDoc
16
     *
17
     * @throws \YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound
18
     */
19
    public function getCachedPathToFiles(array $pathToFiles, int $configNumber, string $pathToCacheDir): array
20
    {
21
        if (is_dir($pathToCacheDir) === false) {
22
            throw new CustomPathToCacheDirectoryNotFound(sprintf('Custom path to cache directory %s was not found', $pathToCacheDir));
23
        }
24
25
        if (file_exists($pathToCacheDir . self::CACHE_FILE_NAME) === false) {
26
            return $pathToFiles;
27
        }
28
29
        $cacheFileContent = file_get_contents($pathToCacheDir . self::CACHE_FILE_NAME);
30
        $filesModifiedTimeIndexedByPathToFileAndConfigNumber = unserialize($cacheFileContent, ['allowed_classes' => false]);
31
32
        if (array_key_exists($configNumber, $filesModifiedTimeIndexedByPathToFileAndConfigNumber) === false) {
33
            return $pathToFiles;
34
        }
35
36
        $changedFiles = [];
37
        foreach ($pathToFiles as $pathToFile) {
38
            if (array_key_exists($pathToFile, $filesModifiedTimeIndexedByPathToFileAndConfigNumber[$configNumber])) {
39
                if (filemtime($pathToFile) !== $filesModifiedTimeIndexedByPathToFileAndConfigNumber[$configNumber][$pathToFile]) {
40
                    $changedFiles[] = $pathToFile;
41
                }
42
            } else {
43
                $changedFiles[] = $pathToFile;
44
            }
45
        }
46
47
        return $changedFiles;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     *
53
     * @throws \YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound
54
     */
55
    public function cacheFiles(array $pathToFiles, int $configNumber, string $pathToCacheDir): void
56
    {
57
        $filesModifiedTimeIndexedByPathToFileAndConfigNumber = [[]];
58
59
        if (is_dir($pathToCacheDir) === false) {
60
            throw new CustomPathToCacheDirectoryNotFound(sprintf('Custom path to cache directory %s was not found', $pathToCacheDir));
61
        }
62
63
        if (file_exists($pathToCacheDir . self::CACHE_FILE_NAME)) {
64
            $filesModifiedTimeIndexedByPathToFileAndConfigNumber = unserialize(file_get_contents($pathToCacheDir . self::CACHE_FILE_NAME), ['allowed_classes' => false]);
65
        }
66
67
        foreach ($pathToFiles as $pathToFile) {
68
            $filesModifiedTimeIndexedByPathToFileAndConfigNumber[$configNumber][$pathToFile] = filemtime($pathToFile);
69
        }
70
71
        file_put_contents($pathToCacheDir . self::CACHE_FILE_NAME, serialize($filesModifiedTimeIndexedByPathToFileAndConfigNumber));
72
    }
73
74
    /**
75
     * @inheritDoc
76
     *
77
     * @throws \YamlStandards\Model\Component\Cache\Exception\CustomPathToCacheDirectoryNotFound
78
     */
79
    public function deleteCacheFileIfConfigFileWasChanged(string $pathToConfigFile, string $pathToCacheDir): void
80
    {
81
        if (count($this->getCachedPathToFiles([$pathToConfigFile], self::CONFIG_NUMBER_FOR_CONFIG_FILE, $pathToCacheDir)) > 0) {
82
            unlink($pathToCacheDir . self::CACHE_FILE_NAME);
83
        } else {
84
            $this->cacheFiles([$pathToConfigFile], self::CONFIG_NUMBER_FOR_CONFIG_FILE, $pathToCacheDir);
85
        }
86
    }
87
}
88