ManifestFileVersionStrategy::applyVersion()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
namespace AppBundle\Asset\VersionStrategy;
3
4
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
5
6
class ManifestFileVersionStrategy implements VersionStrategyInterface
7
{
8
    private $manifestFile;
9
10
    private $manifest;
11
12
    public function __construct($manifestFile)
13
    {
14
        $this->manifestFile = $manifestFile;
15
    }
16
17
    /**
18
     * Returns the asset version for an asset.
19
     *
20
     * @param string $path A path
21
     *
22
     * @return string The version string
23
     */
24
    public function getVersion($path)
25
    {
26
        if ($this->manifest === null) {
27
            $this->manifest = $this->loadManifest();
28
        }
29
30
        $parts = explode('/', $path);
31
        list($name, $type) = explode('.', array_pop($parts));
32
33
        return isset($this->manifest[$name][$type]) ? $this->manifest[$name][$type] : '';
34
    }
35
36
    /**
37
     * Applies version to the supplied path.
38
     *
39
     * @param string $path A path
40
     *
41
     * @return string The versionized path
42
     */
43
    public function applyVersion($path)
44
    {
45
        $version = $this->getVersion($path);
46
47
        if ($version === '') {
48
            return $path;
49
        }
50
51
        $parts = explode('/', $path);
52
        array_splice($parts, -1, 1, $version);
53
54
        return implode('/', $parts);
55
    }
56
57
    private function loadManifest()
58
    {
59
        return json_decode(file_get_contents($this->manifestFile), true);
60
    }
61
}
62