CachedManifestLoader::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rj\FrontendBundle\Manifest\Loader;
4
5
use Rj\FrontendBundle\Manifest\Manifest;
6
use Symfony\Component\Config\ConfigCache;
7
use Symfony\Component\Config\Resource\FileResource;
8
9
class CachedManifestLoader implements ManifestLoaderInterface
10
{
11
    /**
12
     * @var ManifestLoaderInterface
13
     */
14
    private $loader;
15
16
    /**
17
     * @var ConfigCache
18
     */
19
    private $cache;
20
21
    /**
22
     * @param string                  $cacheDir
23
     * @param bool                    $debug
24
     * @param ManifestLoaderInterface $loader
25
     */
26 3
    public function __construct($cacheDir, $debug, ManifestLoaderInterface $loader)
27
    {
28 3
        $cachePath = $cacheDir.'/'.hash('sha1', $loader->getPath()).'.php.cache';
29
30 3
        $this->cache = new ConfigCache($cachePath, $debug);
31 3
        $this->loader = $loader;
32 3
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 3
    public function load()
38
    {
39 3
        if (!$this->cache->isFresh()) {
40 3
            $resource = new FileResource($this->getPath());
41 3
            $entries = $this->loader->load()->all();
42
43 3
            $this->cache->write(sprintf('<?php return %s;', var_export($entries, true)), [$resource]);
44
        } else {
45
            $entries = include $this->cache->getPath();
46
        }
47
48 3
        return new Manifest($entries);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getPath()
55
    {
56 3
        $this->loader->getPath();
57 3
    }
58
}
59