Completed
Push — master ( 4e3396...61149e )
by Piotr
12s
created

GenericFormElement::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Admin\CRUD;
11
12
use FSi\Bundle\AdminBundle\Admin\AbstractElement;
13
use FSi\Bundle\AdminBundle\Exception\RuntimeException;
14
use Symfony\Component\Form\FormFactoryInterface;
15
use Symfony\Component\Form\FormInterface;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
abstract class GenericFormElement extends AbstractElement implements FormElement
19
{
20
    /**
21
     * @var \Symfony\Component\Form\FormFactoryInterface
22
     */
23
    protected $formFactory;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getRoute()
29
    {
30
        return 'fsi_admin_form';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function configureOptions(OptionsResolver $resolver)
37
    {
38
        $resolver->setDefaults([
39
            'template_form' => null,
40
            'allow_add' => true
41
        ]);
42
43
        $resolver->setAllowedTypes('template_form', ['null', 'string']);
44
        $resolver->setAllowedTypes('allow_add', 'bool');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function setFormFactory(FormFactoryInterface $factory)
51
    {
52
        $this->formFactory = $factory;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function createForm($data = null)
59
    {
60
        $form = $this->initForm($this->formFactory, $data);
61
62
        if (!is_object($form) || !$form instanceof FormInterface) {
63
            throw new RuntimeException('initForm should return instanceof Symfony\\Component\\Form\\FormInterface');
64
        }
65
66
        return $form;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getSuccessRoute()
73
    {
74
        return $this->getRoute();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getSuccessRouteParameters()
81
    {
82
        return $this->getRouteParameters();
83
    }
84
85
    /**
86
     * Initialize create Form. This form will be used in createAction in FormController.
87
     *
88
     * @param \Symfony\Component\Form\FormFactoryInterface $factory
89
     * @param mixed $data
90
     * @return \Symfony\Component\Form\FormInterface
91
     */
92
    abstract protected function initForm(FormFactoryInterface $factory, $data = null);
93
}
94