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

StemplerCache   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 67.5%

Importance

Changes 0
Metric Value
wmc 13
eloc 31
dl 0
loc 88
ccs 27
cts 40
cp 0.675
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 5 2
A delete() 0 10 3
A mapFilename() 0 3 1
A filename() 0 3 1
A isFresh() 0 18 4
A __construct() 0 4 1
A write() 0 16 1
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