Passed
Push — master ( a2340d...9d7d2b )
by butschster
06:32
created

StemplerCache::isFresh()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7.456

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 18
ccs 4
cts 10
cp 0.4
crap 7.456
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Stempler;
6
7
use Spiral\Files\Files;
8
use Spiral\Files\FilesInterface;
9
10
final class StemplerCache
11
{
12 2
    public function __construct(
13
        private readonly string $directory,
14
        private readonly FilesInterface $files = new Files()
15
    ) {
16 2
    }
17
18
    /**
19
     * Store template into cache and write invalidation map file.
20
     */
21 1
    public function write(string $key, string $content, array $paths = []): void
22
    {
23
        // template content
24 1
        $this->files->write(
25 1
            $this->filename($key),
26 1
            $content,
27 1
            FilesInterface::RUNTIME,
28 1
            true
29 1
        );
30
31
        // map file
32 1
        $this->files->write(
33 1
            $this->mapFilename($key),
34 1
            \sprintf('<?php return %s;', \var_export($paths, true)),
35 1
            FilesInterface::RUNTIME,
36 1
            true
37 1
        );
38
    }
39
40
    /**
41
     * Check if template still fresh (no files used for generation has changed).
42
     */
43 1
    public function isFresh(string $key): bool
44
    {
45 1
        $mapFilename = $this->mapFilename($key);
46 1
        if (!$this->files->exists($mapFilename)) {
47 1
            return false;
48
        }
49
50
        $time = $this->files->time($this->filename($key));
51
52
        $files = (array)include $mapFilename;
53
        foreach ($files as $file) {
54
            if ($this->files->time($file) > $time) {
55
                // some partial has changed
56
                return false;
57
            }
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * Delete file from the cache.
65
     */
66
    public function delete(string $key): void
67
    {
68
        $filename = $this->filename($key);
69
        if ($this->files->exists($filename)) {
70
            $this->files->delete($filename);
71
        }
72
73
        $mapFilename = $this->mapFilename($key);
74
        if ($this->files->exists($mapFilename)) {
75
            $this->files->delete($mapFilename);
76
        }
77
    }
78
79
    /**
80
     * Load template content.
81
     */
82 1
    public function load(string $key): void
83
    {
84 1
        $filename = $this->filename($key);
85 1
        if ($this->files->exists($filename)) {
86 1
            include_once $filename;
87
        }
88
    }
89
90 1
    private function filename(string $key): string
91
    {
92 1
        return \sprintf('%s/%s.php', $this->directory, $key);
93
    }
94
95 1
    private function mapFilename(string $key): string
96
    {
97 1
        return \sprintf('%s/%s-map.php', $this->directory, $key);
98
    }
99
}
100