Completed
Push — master ( 31741c...acf815 )
by Benjamin
11:01 queued 06:10
created

AssetPackager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Obblm\Core\Helper;
4
5
use Obblm\Core\Exception\NotFoundEntrypointException;
6
use Symfony\Component\Asset\Package;
7
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
8
9
class AssetPackager
10
{
11
    private $entrypoints = [];
12
13 4
    public function __construct()
14
    {
15 4
        $this->addDirectory(dirname(__DIR__) . '/Resources/public/build');
16 4
    }
17
18 4
    public function addDirectory($directory)
19
    {
20 4
        $this->load($directory);
21 4
    }
22
23 4
    private function load($directory)
24
    {
25 4
        $manifestPath = $directory.'/manifest.json';
26 4
        $entrypointsPath = $directory.'/entrypoints.json';
27
28 4
        $this->package = new Package(new JsonManifestVersionStrategy($manifestPath));
0 ignored issues
show
Bug Best Practice introduced by
The property package does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
30 4
        if (!is_file($entrypointsPath)) {
31 2
            throw new \RuntimeException(sprintf('Entrypoints file "%s" does not exist.', $entrypointsPath));
32
        }
33 4
        $json = file_get_contents($entrypointsPath);
34
35 4
        $loadedEntrypoints = json_decode($json, true);
36
37 4
        $this->entrypoints = array_merge($this->entrypoints, $loadedEntrypoints['entrypoints']);
38 4
    }
39
40 1
    public function getCssEntry(string $entrypoint)
41
    {
42 1
        $entrypoint = $this->getEntryPoint($entrypoint);
43 1
        return isset($entrypoint['css']) ? $entrypoint['css'] : [];
44
    }
45
46 1
    public function getJsEntry(string $entrypoint)
47
    {
48 1
        $entrypoint = $this->getEntryPoint($entrypoint);
49 1
        return isset($entrypoint['js']) ? $entrypoint['js'] : [];
50
    }
51
52 2
    public function getEntryPoint(string $entrypoint)
53
    {
54 2
        if (!isset($this->entrypoints[$entrypoint])) {
55 1
            throw new NotFoundEntrypointException($entrypoint, self::class);
56
        }
57
58 1
        return $this->entrypoints[$entrypoint];
59
    }
60
}
61