MapConfigurator::resolveMapOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 MapConfigurator extends AbstractConfigurator
10
{
11
    /**
12
     * @codeCoverageIgnore
13
     */
14
    public function configureEditableOptions(OptionsResolver $or): void
15
    {
16
        $or->setDefault('map', []);
17
    }
18
19 1
    public function supportsEditable(string $editableName, array $config): bool
20
    {
21 1
        return (bool) \count($config['map']);
22
    }
23
24
    /**
25
     * @param array<array> $options
26
     *
27
     * @return array<array>
28
     */
29 1
    private function resolveMapOptions(array $options): array
30
    {
31 1
        $or = new OptionsResolver();
32 1
        $or->setRequired(['source', 'target']);
33
34 1
        return $or->resolve($options);
35
    }
36
37 1
    public function doCreateEditables(RenderArgumentEmitter $emitter, string $name, ConfiguratorData $data): void
38
    {
39 1
        $argument = $emitter->get($name);
40 1
        $config = $data->getConfig();
41 1
        if ($this->supportsEditable($name, $config)) {
42 1
            $maps = $config['map'];
43 1
            foreach ($maps as $map) {
44
                /**
45
                 * @var array<string>
46
                 */
47 1
                $map = $this->resolveMapOptions($map);
48 1
                $source = $this->getExpressionWrapper()->evaluateExpression($map['source'], $data->getContext()->toArray());
49
                $config = $this
50 1
                    ->getExpressionWrapper()
51 1
                    ->setPropertyValue($config, $map['target'], $source);
52
            }
53
54 1
            $argument = new RenderArgument(
55 1
                $argument->getType(),
56 1
                $argument->getName(),
57 1
                $config['options']
58
            );
59
        }
60
61 1
        $emitter->emitArgument($argument);
62 1
    }
63
}
64