Passed
Pull Request — master (#155)
by Arnaud
36:59 queued 32:12
created

ScriptRegistry   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
dl 0
loc 69
ccs 28
cts 29
cp 0.9655
rs 10
c 1
b 0
f 1
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A unregister() 0 6 2
A register() 0 14 2
A hasLocation() 0 3 1
A dump() 0 13 3
A hasScript() 0 3 2
1
<?php
2
3
namespace LAG\AdminBundle\Assets\Registry;
4
5
use LAG\AdminBundle\Exception\Exception;
6
use Twig\Environment;
7
8
/**
9
 * Manage script to avoid adding javascript into the body, and allow dumping them into the head and footer section of
10
 * the html page.
11
 */
12
class ScriptRegistry implements ScriptRegistryInterface
13
{
14
    const DEFAULT_TEMPLATE = '@LAGAdmin/Scripts/scripts.html.twig';
15
16
    protected $scripts = [];
17
18
    /**
19
     * @var string
20
     */
21
    protected $template;
22
23
    /**
24
     * @var Environment
25
     */
26
    protected $environment;
27
28 2
    public function __construct(Environment $environment, string $template = self::DEFAULT_TEMPLATE)
29
    {
30 2
        $this->template = $template;
31 2
        $this->environment = $environment;
32 2
    }
33
34 2
    public function register(string $location, string $script, string $template = null, array $context = []): void
35
    {
36
        $asset = [
37 2
            'location' => $location,
38 2
            'template' => $template,
39 2
            'context' => $context,
40
        ];
41
42 2
        if (null === $template) {
43
            // if no template is provided, we use the default script template
44 2
            $asset['template'] = $this->template;
45 2
            $asset['context']['script'] = $script;
46
        }
47 2
        $this->scripts[$location][$script] = $asset;
48 2
    }
49
50 2
    public function unregister(string $location, string $script): void
51
    {
52 2
        if (!$this->hasScript($location, $script)) {
53 2
            throw new Exception('The script "'.$script.'" is not registered at the location "'.$location.'". It can be unregistered');
54
        }
55 2
        unset($this->scripts[$location][$script]);
56 2
    }
57
58 2
    public function dump(string $location): string
59
    {
60 2
        $content = '';
61
62 2
        if (!$this->hasLocation($location)) {
63
            return '';
64
        }
65
66 2
        foreach ($this->scripts[$location] as $script) {
67 2
            $content .= $this->environment->render($script['template'], $script['context']);
68
        }
69
70 2
        return $content;
71
    }
72
73 2
    public function hasLocation(string $location): bool
74
    {
75 2
        return key_exists($location, $this->scripts);
76
    }
77
78 2
    public function hasScript(string $location, string $script): bool
79
    {
80 2
        return $this->hasLocation($location) && key_exists($script, $this->scripts[$location]);
81
    }
82
}
83