dispatchOnChildren()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 2
crap 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
}