Passed
Push — master ( 960a24...ce8b6f )
by Gerhard
20:20 queued 10:00
created

ActionExtension::buildView()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 4
nc 5
nop 3
crap 20
1
<?php
2
3
namespace Enhavo\Bundle\FormBundle\Form\Extension;
4
5
use Enhavo\Bundle\AppBundle\Action\ActionManager;
6
use Symfony\Component\Form\AbstractTypeExtension;
7
use Symfony\Component\Form\Extension\Core\Type\FormType;
8
use Symfony\Component\Form\FormInterface;
9
use Symfony\Component\Form\FormView;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
class ActionExtension extends AbstractTypeExtension
13
{
14
    /** @var ActionManager */
15
    private $actionManager;
16
17
    /**
18
     * ActionExtension constructor.
19
     * @param ActionManager $actionManager
20
     */
21
    public function __construct(ActionManager $actionManager)
22
    {
23
        $this->actionManager = $actionManager;
24
    }
25
26
    /**
27
     * @param OptionsResolver $resolver
28
     */
29
    public function configureOptions(OptionsResolver $resolver)
30
    {
31
        $resolver->setDefaults([
32
            'actions' => []
33
        ]);
34
    }
35
36
    public function buildView(FormView $view, FormInterface$form, array $options)
37
    {
38
        $actions = $options['actions'];
39
        if(!empty($actions)){
40
            $data = $form->getParent() && $form->getParent()->getData() ? $form->getParent()->getData() : null;
41
            $actions = $this->createActionsViewData($actions, $data);
42
        }
43
44
        $view->vars['actions'] = $actions;
45
    }
46
47
    private function createActionsViewData(array $options, $resource)
48
    {
49
        return $this->actionManager->createActionsViewData($options, $resource);
50
    }
51
52
    public function getExtendedTypes(): iterable
53
    {
54
        return [FormType::class];
55
    }
56
}
57