AreabrickConfigurator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Khusseini\PimcoreRadBrickBundle;
4
5
use Khusseini\PimcoreRadBrickBundle\Configurator\ConfiguratorData;
6
use Khusseini\PimcoreRadBrickBundle\Configurator\IConfigurator;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class AreabrickConfigurator
10
{
11
    /**
12
     * @var array<mixed>
13
     */
14
    private $config = [];
15
16
    /**
17
     * @var IConfigurator[]
18
     */
19
    private $configurators = [];
20
21
    /**
22
     * @param array<mixed>    $config
23
     * @param IConfigurator[] $configurators
24
     */
25 9
    public function __construct(
26
        array $config,
27
        array $configurators = []
28
    ) {
29 9
        $or = new OptionsResolver();
30 9
        $or->setDefaults(
31
            [
32 9
            'areabricks' => [],
33
            'datasources' => [],
34
            ]
35
        );
36 9
        $this->config = $or->resolve($config);
37 9
        $this->configurators = $configurators;
38 9
    }
39
40
    /**
41
     * @param IConfigurator[] $configurators
42
     */
43 5
    public function setConfigurators(array $configurators): void
44
    {
45 5
        $this->configurators = $configurators;
46 5
    }
47
48
    /**
49
     * @param array<mixed> $context
50
     *
51
     * @return \Generator<RenderArgument>
52
     */
53 3
    public function compileAreaBrick(string $name, ContextInterface $context): \Generator
54
    {
55 3
        $data = new ConfiguratorData($context);
56 3
        $data->setConfig($this->resolveAreaBrickConfig($name));
57
58
        /*
59
         * @var IConfigurator
60
         */
61 3
        foreach ($this->configurators as $configurator) {
62 1
            $configurator->preCreateEditables($name, $data);
63
        }
64
65 3
        $this->config = $data->getConfig();
66
67 3
        return $this->createEditables($name, $context);
68
    }
69
70
    /**
71
     * @param array<string,mixed> $config
72
     */
73 5
    private function setAreabrickConfig(string $name, array $config): void
74
    {
75 5
        $this->config['areabricks'][$name] = $config;
76 5
    }
77
78 1
    public function hasAreabrickConfig(string $name): bool
79
    {
80 1
        return (bool) $this->getAreabrickConfig($name);
81
    }
82
83
    /**
84
     * @return array<string,mixed>
85
     */
86 9
    public function getAreabrickConfig(string $name): array
87
    {
88 9
        return isset($this->config['areabricks'][$name]) ? $this->config['areabricks'][$name] : [];
89
    }
90
91
    /**
92
     * @return array<array>
93
     * @codeCoverageIgnore
94
     */
95
    protected function getDatasourceConfig(string $name): array
96
    {
97
        return $this->config['datasources'][$name];
98
    }
99
100
    /**
101
     * @return array<mixed>
102
     */
103 5
    public function resolveAreaBrickConfig(string $name): array
104
    {
105 5
        $or = new OptionsResolver();
106 5
        $or->setDefaults(
107
            [
108 5
            'icon' => null,
109
            'label' => null,
110
            'open' => '',
111
            'close' => '',
112
            'use_edit' => false,
113
            ]
114
        );
115
116 5
        $config = $this->getAreabrickConfig($name);
117 5
        $or->setDefined(array_keys($config));
118
119 5
        $this->setAreabrickConfig($name, $or->resolve($config));
120
121 5
        return $this->config;
122
    }
123
124
    /**
125
     * @param array<array> $config
126
     *
127
     * @return array<string, array<string,mixed>>
128
     */
129 7
    protected function compileEditablesConfig(array $config): array
130
    {
131 7
        $editablesConfig = $config['editables'];
132 7
        $or = new OptionsResolver();
133 7
        $or->setDefaults(['options' => []]);
134 7
        $or->setRequired(['type']);
135
136 7
        foreach ($this->configurators as $configurator) {
137 2
            $configurator->configureEditableOptions($or);
138
        }
139
140 7
        $return = [];
141
142 7
        foreach ($editablesConfig as $name => $econfig) {
143 7
            $econfig = $or->resolve($econfig);
144 7
            $return[$name] = $econfig;
145
        }
146
147 7
        return $return;
148
    }
149
150
    /**
151
     * @return \Generator<RenderArgument>
152
     */
153 7
    public function createEditables(
154
        string $areabrick,
155
        ContextInterface $context
156
    ): \Generator {
157 7
        $editablesConfig = $this->compileEditablesConfig($this->config['areabricks'][$areabrick]);
158 7
        $areaBrickConfig = $this->getAreabrickConfig($areabrick);
159 7
        $emitter = new RenderArgumentEmitter();
160 7
        $data = new ConfiguratorData($context);
161
162 7
        foreach ($editablesConfig as $editableName => $editableConfig) {
163 7
            $argument = new RenderArgument(
164 7
                'editable',
165
                $editableName,
166 7
                ['type' => $editableConfig['type'], 'options' => $editableConfig['options']]
167
            );
168
169 7
            $emitter->emitArgument($argument);
170
171 7
            $data->setConfig($editableConfig);
172
173 7
            foreach ($this->configurators as $configurator) {
174 2
                if ($configurator->supportsEditable($editableName, $editableConfig)) {
175 2
                    $configurator->createEditables($emitter, $editableName, $data);
176
                }
177
            }
178
179 7
            yield from $emitter->emit();
180
        }
181
182 7
        $data->setConfig($areaBrickConfig);
183 7
        foreach ($this->configurators as $configurator) {
184 2
            $configurator->postCreateEditables($areabrick, $data, $emitter);
185
        }
186
187 7
        yield from $emitter->emit();
188 7
    }
189
}
190