Completed
Pull Request — master (#32)
by Paulo Rodrigues
07:49
created

CachedManifestLoader::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function __construct($cacheDir, $debug, ManifestLoaderInterface $loader)
27
    {
28
        $cachePath = $cacheDir.'/'.hash('sha1', $loader->getPath()).'.php.cache';
29
30
        $this->cache = new ConfigCache($cachePath, $debug);
31
        $this->loader = $loader;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function load()
38
    {
39
        if (!$this->cache->isFresh()) {
40
            $resource = new FileResource($this->getPath());
41
            $entries = $this->loader->load()->all();
42
43
            $this->cache->write(sprintf('<?php return %s;', var_export($entries, true)), [$resource]);
44
        } else {
45
            $entries = include $this->cache->getPath();
46
        }
47
48
        return new Manifest($entries);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getPath()
55
    {
56
        $this->loader->getPath();
57
    }
58
}
59