Completed
Branch v1.x-dev (ef54be)
by Benjamin
03:52
created

AssetPackager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 19
c 1
b 0
f 0
dl 0
loc 50
ccs 26
cts 26
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addDirectory() 0 3 1
A __construct() 0 3 1
A getJsEntry() 0 4 2
A getCssEntry() 0 4 2
A getEntryPoint() 0 7 2
A load() 0 15 2
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