AbstractManifestLoader::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
ccs 5
cts 6
cp 0.8333
cc 2
eloc 5
nc 2
nop 2
crap 2.0185
1
<?php
2
3
namespace Rj\FrontendBundle\Manifest\Loader;
4
5
use Rj\FrontendBundle\Manifest\Manifest;
6
7
abstract class AbstractManifestLoader implements ManifestLoaderInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $path;
13
14
    /**
15
     * @var null|string
16
     */
17
    private $rootKey;
18
19
    /**
20
     * @param string $path
21
     *
22
     * @return mixed
23
     */
24
    abstract protected function parse($path);
25
26
    /**
27
     * @param string      $path
28
     * @param null|string $rootKey
29
     */
30 8
    public function __construct($path, $rootKey = null)
31
    {
32 8
        if (!is_file($path)) {
33
            throw new \InvalidArgumentException("The manifest file '$path' could not be found");
34
        }
35
36 8
        $this->path = $path;
37 8
        $this->rootKey = $rootKey;
38 8
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 7
    public function load()
44
    {
45 7
        $entries = $this->parse($this->path);
46
47 7
        if (!empty($this->rootKey)) {
48 2
            if (!isset($entries[$this->rootKey])) {
49 1
                throw new \InvalidArgumentException('Manifest file contains no '.$this->rootKey.' key');
50
            }
51
52 1
            $entries = $entries[$this->rootKey];
53
        }
54
55 6
        return new Manifest($entries);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 4
    public function getPath()
62
    {
63 4
        return $this->path;
64
    }
65
}
66