Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

getFormDataClass()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 15
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 9
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\MetadataBundle\DependencyInjection\Compiler;
13
14
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * @author Kamil Kokot <[email protected]>
22
 */
23
class DynamicFormsChoicesMapCompilerPass implements CompilerPassInterface
24
{
25
    /**
26
     * @var ContainerBuilder
27
     */
28
    private $container;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function process(ContainerBuilder $container)
34
    {
35
        $this->container = $container;
36
37
        if (!$container->hasDefinition('sylius.metadata.dynamic_forms_choices_map')) {
38
            return;
39
        }
40
41
        $definition = $container->getDefinition('sylius.metadata.dynamic_forms_choices_map');
42
43
        $taggedServices = $container->findTaggedServiceIds('sylius.metadata.dynamic_form_choice');
44
45
        foreach ($taggedServices as $id => $tags) {
46
            $formDefinition = $container->getDefinition($id);
47
48
            $dataClass = $this->getFormDataClass($formDefinition);
49
            $formName = $this->getFormName($formDefinition);
50
51
            $definition->addMethodCall('addForm', [
52
                $tags[0]['group'],
53
                $dataClass,
54
                $formName,
55
                isset($tags[0]['label']) ? $tags[0]['label'] : $formName
56
            ]);
57
        }
58
    }
59
60
    /**
61
     * @param Definition $formDefinition
62
     *
63
     * @return string Form data class
64
     */
65
    private function getFormDataClass(Definition $formDefinition)
66
    {
67
        $tags = $formDefinition->getTag('sylius.metadata.dynamic_form_choice');
68
        $class = isset($tags[0]['class']) ? $tags[0]['class'] : null;
69
70
        if (null === $class) {
71
            throw new \InvalidArgumentException(sprintf(
72
                'Definition "%s" tagged by "%s" should define class attribute in tag definition',
73
                $formDefinition->getClass(),
74
                'sylius.metadata.dynamic_form'
75
            ));
76
        }
77
78
        return $class;
79
    }
80
81
    /**
82
     * @param Definition $formDefinition
83
     *
84
     * @return string Form name
85
     */
86
    private function getFormName(Definition $formDefinition)
87
    {
88
        $tags = $formDefinition->getTag('form.type');
89
        $formName = isset($tags[0]['alias']) ? $tags[0]['alias'] : null;
90
91
        if (null === $formName) {
92
            throw new \InvalidArgumentException(sprintf(
93
                'Definition "%s" tagged by "%s" should also be tagged by "%s" with attribute "%s"',
94
                $formDefinition->getClass(),
95
                'sylius.metadata.dynamic_form',
96
                'form.type',
97
                'alias'
98
            ));
99
        }
100
101
        return $formName;
102
    }
103
}
104