PropagateValidFormEventSubscriber   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 11
c 1
b 0
f 0
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A onFormValid() 0 4 2
A dispatchOnChildren() 0 13 3
1
<?php
2
3
namespace Braunstetter\ValidFormEvent\Form\EventSubscriber;
4
5
use Braunstetter\ValidFormEvent\Form\Event\ValidFormEvent;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\Form\ButtonBuilder;
8
use Symfony\Component\Form\FormEvent;
9
use Symfony\Component\Form\FormInterface;
10
11
class PropagateValidFormEventSubscriber implements EventSubscriberInterface
12
{
13
14
    /**
15
     * @inheritDoc
16
     */
17 2
    public static function getSubscribedEvents(): array
18
    {
19
        return array(
20 2
            ValidFormEvent::NAME => 'onFormValid',
21
        );
22
    }
23
24 2
    public function onFormValid(ValidFormEvent $event)
25
    {
26 2
        if ($event->getCurrentForm() === null) {
27 2
            $this->dispatchOnChildren($event->getForm(), $event);
28
        }
29
    }
30
31 2
    private function dispatchOnChildren(FormInterface $form, FormEvent $rootEvent)
32
    {
33 2
        foreach ($form as $child) {
34
35 2
            if ($child->getConfig() instanceof ButtonBuilder) {
36 1
                continue;
37
            }
38
39
            /** @var $child FormInterface */
40 2
            $eventDispatcher = $child->getConfig()->getEventDispatcher();
41 2
            $eventDispatcher->dispatch(new ValidFormEvent($rootEvent->getForm(), $rootEvent->getData(), $child), ValidFormEvent::NAME);
42
43 2
            $this->dispatchOnChildren($child, $rootEvent);
44
        }
45
    }
46
}