DeletableType::addDeleteButton()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
namespace Vivait\BootstrapBundle\Form\Type;
4
5
use Symfony\Component\Form\AbstractTypeExtension;
6
use Symfony\Component\Form\ClickableInterface;
7
use Symfony\Component\Form\Exception\OutOfBoundsException;
8
use Symfony\Component\Form\Extension\Core\Type\FormType;
9
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\Form\FormEvent;
12
use Symfony\Component\Form\FormEvents;
13
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
14
15
class DeletableType extends AbstractTypeExtension
16
{
17
    public function buildForm(FormBuilderInterface $builder, array $options)
18
    {
19
        if ($options['delete_button'] ?? false) {
20
            $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'addDeleteButton']);
21
22
            if (is_callable($options['delete_button'])) {
23
                $builder->addEventListener(
24
                  FormEvents::POST_SUBMIT,
25
                  function (FormEvent $event) use ($options) {
26
                      try {
27
                          /* @var ClickableInterface $button */
28
                          $button = $event->getForm()->get('delete');
29
30
                          if ($button->isClicked()) {
31
                              call_user_func($options['delete_button'], $event->getData());
32
                              $event->stopPropagation();
33
                          }
34
                      }
35
                      catch (OutOfBoundsException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Form\E...on\OutOfBoundsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
36
                          // Do nothing
37
                      }
38
                  },
39
                  900
40
                );
41
            }
42
        }
43
    }
44
45
    public function setDefaultOptions(OptionsResolverInterface $resolver)
46
    {
47
        $resolver->setDefaults(
48
          [
49
            'delete_button' => false
50
          ]
51
        );
52
    }
53
54
    public function getExtendedType()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
55
    {
56
        return FormType::class;
57
    }
58
59
    public function addDeleteButton(FormEvent $event)
60
    {
61
        $form = $event->getForm();
62
        $object = $event->getData();
63
64
        if ($object && $object->getId()) {
65
            $form->add(
66
              'delete',
67
              SubmitType::class,
68
              [
69
                'label' => 'Delete',
70
                'attr' => [
71
                  'class' => 'btn-danger'
72
                ]
73
              ]
74
            );
75
        }
76
    }
77
}
78