GroupConfigurator::configureEditableOptions()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Khusseini\PimcoreRadBrickBundle\Configurator;
4
5
use Khusseini\PimcoreRadBrickBundle\RenderArgument;
6
use Khusseini\PimcoreRadBrickBundle\RenderArgumentEmitter;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class GroupConfigurator extends AbstractConfigurator
10
{
11 1
    public function configureEditableOptions(OptionsResolver $or): void
12
    {
13 1
        $or->setDefault('group', null);
14
        // @codeCoverageIgnoreStart
15
        $or->setAllowedValues(
16
            'group',
17
            function ($value) {
18
                if (null === $value) {
19
                    return true;
20
                }
21
22
                return preg_match('/[_a-z]+/i', $value);
23
            }
24
        );
25
        // @codeCoverageIgnoreEnd
26 1
    }
27
28 2
    public function supportsEditable(string $editableName, array $config): bool
29
    {
30 2
        return isset($config['group']);
31
    }
32
33
    /**
34
     * @param array<string,mixed> $config
35
     *
36
     * @return array<string,mixed>
37
     */
38 3
    protected function resolveBrickConfig(array $config): array
39
    {
40 3
        $or = new OptionsResolver();
41 3
        $or->setDefined(array_keys($config));
42 3
        $or->setDefault('groups', []);
43 3
        $or->setDefault('editables', []);
44
45 3
        return $or->resolve($config);
46
    }
47
48 3
    public function preCreateEditables(string $brickName, ConfiguratorData $data): void
49
    {
50 3
        $config = $data->getConfig();
51 3
        $brick = $this->resolveBrickConfig($config['areabricks'][$brickName]);
52
53 3
        $groups = $brick['groups'];
54
55 3
        $editables = $brick['editables'];
56
57 3
        foreach ($editables as $name => $editable) {
58 3
            if (!isset($editable['group'])) {
59 1
                continue;
60
            }
61
62 2
            if (!isset($groups[$editable['group']])) {
63 1
                throw new \InvalidArgumentException("Group with name {$editable['group']} does not exist.");
64
            }
65 1
            $groupConfig = $groups[$editable['group']];
66 1
            $editable = array_merge($editable, $groupConfig);
67 1
            $editables[$name] = $editable;
68
        }
69
70 2
        $brick['editables'] = $editables;
71 2
        $config['areabricks'][$brickName] = $brick;
72 2
        $data->setConfig($config);
73 2
    }
74
75
    /**
76
     * @codeCoverageIgnore
77
     */
78
    public function doCreateEditables(RenderArgumentEmitter $emitter, string $name, ConfiguratorData $data): void
79
    {
80
        return;
81
    }
82
83 4
    public function postCreateEditables(string $brickName, ConfiguratorData $data, RenderArgumentEmitter $emitter): void
84
    {
85 4
        $config = $data->getConfig();
86
87 4
        if (!isset($config['groups'])) {
88 1
            return;
89
        }
90
91 3
        $groups = array_keys($config['groups']);
92
93 3
        $groupArguments = [];
94
95 3
        foreach ($config['editables'] as $name => $config) {
96
            if (
97 3
                !isset($config['group'])
98 3
                || !\in_array($config['group'], $groups)
99
            ) {
100 1
                continue;
101
            }
102
103 3
            $groupName = $config['group'];
104 3
            if (!isset($groupArguments[$groupName])) {
105 3
                $groupArguments[$groupName] = [];
106
            }
107 3
            $renderArg = $emitter->get($name);
108
109 3
            if ('collection' === $renderArg->getType()) {
110 1
                $values = $renderArg->getValue();
111
112 1
                foreach ($values as $key => $data) {
113 1
                    $referenceId = $name.'_'.$key;
114 1
                    $groupArguments[$groupName][$key][$name] = new RenderArgument('reference', $name, $referenceId);
115
                }
116
            } else {
117 2
                if (!isset($groupArguments[$groupName][0])) {
118 2
                    $groupArguments[$groupName][0] = [];
119
                }
120 2
                $groupArguments[$groupName][0][$renderArg->getName()] =
121 2
                    new RenderArgument(
122 2
                        'reference',
123 2
                        $renderArg->getName(),
124 2
                        $renderArg->getName()
125
                    );
126
            }
127
        }
128
129 3
        foreach ($groupArguments as $name => $data) {
130 3
            $argumentValue = [];
131 3
            foreach ($data as $key => $value) {
132 3
                $argumentValue[$key] = new RenderArgument('collection', $key, $value);
133
            }
134
135 3
            $argument = new RenderArgument('collection', $name, $argumentValue);
136 3
            $emitter->emitArgument($argument);
137
        }
138 3
    }
139
}
140