FormConfigurationListener::setActionOptions()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 8.8571
cc 5
eloc 11
nc 9
nop 1
1
<?php
2
3
namespace Pim\Bundle\CustomEntityBundle\EventListener\Action;
4
5
use Pim\Bundle\CustomEntityBundle\Action\AbstractFormAction;
6
use Pim\Bundle\CustomEntityBundle\Event\ActionEvents;
7
use Pim\Bundle\CustomEntityBundle\Event\ConfigurationEvent;
8
use Pim\Bundle\CustomEntityBundle\Event\ConfigurationEvents;
9
use Pim\Bundle\CustomEntityBundle\Event\ConfigureActionEvent;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * Form listener for actions
14
 *
15
 * @author    Antoine Guigan <[email protected]>
16
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
17
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
18
 */
19
class FormConfigurationListener implements EventSubscriberInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public static function getSubscribedEvents()
25
    {
26
        return [
27
            ConfigurationEvents::CONFIGURE => 'setConfigurationOptions',
28
            ActionEvents::CONFIGURE        => 'setActionOptions'
29
        ];
30
    }
31
32
    /**
33
     * Adds options to the actions
34
     *
35
     * @param ConfigureEvent $event
36
     */
37
    public function setConfigurationOptions(ConfigurationEvent $event)
38
    {
39
        $event->getOptionsResolver()->setDefined(
40
            [
41
                'form_type',
42
                'form_options',
43
                'form_template'
44
            ]
45
        );
46
    }
47
48
    /**
49
     * Adds options to the actions
50
     *
51
     * @param ConfigureActionEvent $event
52
     *
53
     * @return null
54
     */
55
    public function setActionOptions(ConfigureActionEvent $event)
56
    {
57
        if (!($event->getAction() instanceof AbstractFormAction)) {
58
            return;
59
        }
60
61
        $confOptions = $event->getAction()->getConfiguration()->getOptions();
62
        $resolver = $event->getOptionsResolver();
63
        if (isset($confOptions['form_type'])) {
64
            $resolver->setDefaults(['form_type' => $confOptions['form_type']]);
65
        }
66
        if (isset($confOptions['form_options'])) {
67
            $resolver->setDefaults(['form_options' => $confOptions['form_options']]);
68
        }
69
        if (isset($confOptions['form_template'])) {
70
            $resolver->setDefaults(['template' => $confOptions['form_template']]);
71
        }
72
    }
73
}
74