Passed
Pull Request — master (#150)
by Arnaud
07:48
created

AssetsExtension   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerScript() 0 17 5
A getExtendedTypes() 0 4 1
B finishView() 0 16 8
A __construct() 0 3 1
1
<?php
2
3
namespace LAG\AdminBundle\Form\Extension;
4
5
use Exception;
6
use LAG\AdminBundle\Assets\Registry\ScriptRegistryInterface;
7
use Symfony\Component\Form\AbstractTypeExtension;
8
use Symfony\Component\Form\Extension\Core\Type\FormType;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\Form\FormView;
11
12
class AssetsExtension extends AbstractTypeExtension
13
{
14
    /**
15
     * @var ScriptRegistryInterface
16
     */
17
    private $scriptRegistry;
18
19
    /**
20
     * AssetsExtension constructor.
21
     */
22
    public function __construct(ScriptRegistryInterface $scriptRegistry)
23
    {
24
        $this->scriptRegistry = $scriptRegistry;
25
    }
26
27
    public static function getExtendedTypes(): iterable
28
    {
29
        return [
30
            FormType::class,
31
        ];
32
    }
33
34
    /**
35
     * Register the configured scripts using the FormView variables.
36
     *
37
     * @param FormView      $view
38
     * @param FormInterface $form
39
     * @param array         $options
40
     *
41
     * @throws Exception
42
     */
43
    public function finishView(FormView $view, FormInterface $form, array $options)
44
    {
45
        if (!array_key_exists('scripts', $view->vars) || !is_array($view->vars['scripts'])) {
46
            return;
47
        }
48
        foreach ($view->vars['scripts'] as $location => $scripts) {
49
            if (!is_array($scripts)) {
50
                throw new Exception('Assets configuration for location '.$location.' should be an array in form '.$form->getName());
51
            }
52
53
            foreach ($scripts as $name => $script) {
54
                // provide a script name if none is provided
55
                if (is_array($script) && !array_key_exists('script', $script)) {
56
                    $script['script'] = $name;
57
                }
58
                $this->registerScript($location, $script);
59
            }
60
        }
61
    }
62
63
    /**
64
     * Register a script for a location.
65
     *
66
     * @param string $location
67
     * @param string $script
68
     */
69
    private function registerScript($location, $script)
70
    {
71
        if (is_string($script)) {
0 ignored issues
show
introduced by
The condition is_string($script) is always true.
Loading history...
72
            $this
73
                ->scriptRegistry
74
                ->register($location, $script)
75
            ;
76
        } elseif (is_array($script)) {
77
            if (!array_key_exists('template', $script)) {
78
                $script['template'] = null;
79
            }
80
            if (!array_key_exists('context', $script)) {
81
                $script['context'] = [];
82
            }
83
            $this
84
                ->scriptRegistry
85
                ->register($location, $script['script'], $script['template'], $script['context'])
86
            ;
87
        }
88
    }
89
}
90