Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

BuildShippingMethodFormSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 96
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 7 1
A preSetData() 0 10 3
A preSubmit() 0 10 3
A addConfigurationFields() 0 19 2
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\ShippingBundle\Form\EventSubscriber;
13
14
use Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface;
15
use Sylius\Component\Registry\ServiceRegistryInterface;
16
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\Form\FormEvent;
19
use Symfony\Component\Form\FormEvents;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\Form\FormInterface;
22
23
/**
24
 * @internal
25
 *
26
 * @author Paweł Jędrzejewski <[email protected]>
27
 */
28
class BuildShippingMethodFormSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var ServiceRegistryInterface
32
     */
33
    protected $calculatorRegistry;
34
35
    /**
36
     * @var FormFactoryInterface
37
     */
38
    protected $factory;
39
40
    /**
41
     * @var FormTypeRegistryInterface
42
     */
43
    private $formTypeRegistry;
44
45
    /**
46
     * @param ServiceRegistryInterface $calculatorRegistry
47
     * @param FormFactoryInterface $factory
48
     * @param FormTypeRegistryInterface $formTypeRegistry
49
     */
50
    public function __construct(
51
        ServiceRegistryInterface $calculatorRegistry,
52
        FormFactoryInterface $factory,
53
        FormTypeRegistryInterface $formTypeRegistry
54
    ) {
55
        $this->calculatorRegistry = $calculatorRegistry;
56
        $this->factory = $factory;
57
        $this->formTypeRegistry = $formTypeRegistry;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public static function getSubscribedEvents()
64
    {
65
        return [
66
            FormEvents::PRE_SET_DATA => 'preSetData',
67
            FormEvents::PRE_SUBMIT => 'preSubmit',
68
        ];
69
    }
70
71
    /**
72
     * @param FormEvent $event
73
     */
74
    public function preSetData(FormEvent $event)
75
    {
76
        $method = $event->getData();
77
78
        if (null === $method || null === $method->getId()) {
79
            return;
80
        }
81
82
        $this->addConfigurationFields($event->getForm(), $method->getCalculator(), $method->getConfiguration());
83
    }
84
85
    /**
86
     * @param FormEvent $event
87
     */
88
    public function preSubmit(FormEvent $event)
89
    {
90
        $data = $event->getData();
91
92
        if (empty($data) || !array_key_exists('calculator', $data)) {
93
            return;
94
        }
95
96
        $this->addConfigurationFields($event->getForm(), $data['calculator']);
97
    }
98
99
    /**
100
     * @param FormInterface $form
101
     * @param string $calculatorName
102
     * @param array $data
103
     */
104
    protected function addConfigurationFields(FormInterface $form, $calculatorName, array $data = [])
105
    {
106
        /** @var CalculatorInterface $calculator */
107
        $calculator = $this->calculatorRegistry->get($calculatorName);
108
109
        $calculatorType = $calculator->getType();
110
        if (!$this->formTypeRegistry->has($calculatorType, 'default')) {
111
            return;
112
        }
113
114
        $configurationField = $this->factory->createNamed(
115
            'configuration',
116
            $this->formTypeRegistry->get($calculatorType, 'default'),
117
            $data,
118
            ['auto_initialize' => false]
119
        );
120
121
        $form->add($configurationField);
122
    }
123
}
124