Completed
Pull Request — 2.1 (#289)
by Piotr
02:46
created

GenericCRUDElement::createForm()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 2
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 FSi\Component\DataGrid\DataGridFactoryInterface;
15
use FSi\Component\DataGrid\DataGridInterface;
16
use FSi\Component\DataSource\DataSourceFactoryInterface;
17
use FSi\Component\DataSource\DataSourceInterface;
18
use Symfony\Component\Form\FormFactoryInterface;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\OptionsResolver\Options;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
23
abstract class GenericCRUDElement extends AbstractElement implements CRUDElement
24
{
25
    /**
26
     * @var \FSi\Component\DataSource\DataSourceFactoryInterface
27
     */
28
    protected $datasourceFactory;
29
30
    /**
31
     * @var \FSi\Component\DataGrid\DataGridFactoryInterface
32
     */
33
    protected $datagridFactory;
34
35
    /**
36
     * @var \Symfony\Component\Form\FormFactoryInterface
37
     */
38
    protected $formFactory;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getRoute()
44
    {
45
        return 'fsi_admin_list';
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getSuccessRoute()
52
    {
53
        return $this->getRoute();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getSuccessRouteParameters()
60
    {
61
        return $this->getRouteParameters();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function setDefaultOptions(OptionsResolver $resolver)
68
    {
69
        $resolver->setDefaults([
70
            'allow_delete' => true,
71
            'allow_add' => true,
72
            'template_crud_list' => null,
73
            'template_crud_create' => null,
74
            'template_crud_edit' => null,
75
            'template_list' => function (Options $options) {
76
                return $options['template_crud_list'];
77
            },
78
            'template_form' => function (Options $options) {
79
                return $options['template_crud_edit'];
80
            }
81
        ]);
82
83
        $resolver->setNormalizer('template_crud_create', function (Options $options, $value) {
84
            if ($value !== $options['template_crud_edit']) {
85
                throw new RuntimeException(
86
                    'CRUD admin element options "template_crud_create" and "template_crud_edit" have both to have the same value'
87
                );
88
            }
89
90
            return $value;
91
        });
92
93
        $resolver->setAllowedTypes('allow_delete', 'bool');
94
        $resolver->setAllowedTypes('allow_add', 'bool');
95
        $resolver->setAllowedTypes('template_crud_list', ['null', 'string']);
96
        $resolver->setAllowedTypes('template_crud_create', ['null', 'string']);
97
        $resolver->setAllowedTypes('template_crud_edit', ['null', 'string']);
98
        $resolver->setAllowedTypes('template_list', ['null', 'string']);
99
        $resolver->setAllowedTypes('template_form', ['null', 'string']);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function configureOptions(OptionsResolver $resolver)
106
    {
107
        $this->setDefaultOptions($resolver);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function apply($object)
114
    {
115
        $this->delete($object);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function setDataGridFactory(DataGridFactoryInterface $factory)
122
    {
123
        $this->datagridFactory = $factory;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function setDataSourceFactory(DataSourceFactoryInterface $factory)
130
    {
131
        $this->datasourceFactory = $factory;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function setFormFactory(FormFactoryInterface $factory)
138
    {
139
        $this->formFactory = $factory;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function createDataGrid()
146
    {
147
        $datagrid = $this->initDataGrid($this->datagridFactory);
148
149
        if (!is_object($datagrid) || !$datagrid instanceof DataGridInterface) {
150
            throw new RuntimeException('initDataGrid should return instanceof FSi\\Component\\DataGrid\\DataGridInterface');
151
        }
152
153
        if ($this->getOption('allow_delete')) {
154
            if (!$datagrid->hasColumnType('batch')) {
155
                $datagrid->addColumn('batch', 'batch', [
156
                    'actions' => [
157
                        'delete' => [
158
                            'route_name' => 'fsi_admin_batch',
159
                            'additional_parameters' => ['element' => $this->getId()],
160
                            'label' => 'crud.list.batch.delete'
161
                        ]
162
                    ],
163
                    'display_order' => -1000
164
                ]);
165
            }
166
        }
167
168
        return $datagrid;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function createDataSource()
175
    {
176
        $datasource = $this->initDataSource($this->datasourceFactory);
177
178
        if (!is_object($datasource) || !$datasource instanceof DataSourceInterface) {
179
            throw new RuntimeException('initDataSource should return instanceof FSi\\Component\\DataSource\\DataSourceInterface');
180
        }
181
182
        return $datasource;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function createForm($data = null)
189
    {
190
        $form = $this->initForm($this->formFactory, $data);
191
192
        if (!is_object($form) || !$form instanceof FormInterface) {
193
            throw new RuntimeException('initForm should return instanceof Symfony\\Component\\Form\\FormInterface');
194
        }
195
196
        return $form;
197
    }
198
199
    /**
200
     * Initialize DataGrid.
201
     *
202
     * @param \FSi\Component\DataGrid\DataGridFactoryInterface $factory
203
     * @return \FSi\Component\DataGrid\DataGridInterface
204
     */
205
    abstract protected function initDataGrid(DataGridFactoryInterface $factory);
206
207
    /**
208
     * Initialize DataSource.
209
     *
210
     * @param \FSi\Component\DataSource\DataSourceFactoryInterface $factory
211
     * @return \FSi\Component\DataSource\DataSourceInterface
212
     */
213
    abstract protected function initDataSource(DataSourceFactoryInterface $factory);
214
215
    /**
216
     * Initialize create Form. This form will be used in createAction in CRUDController.
217
     *
218
     * @param \Symfony\Component\Form\FormFactoryInterface $factory
219
     * @param mixed $data
220
     * @return \Symfony\Component\Form\FormInterface
221
     */
222
    abstract protected function initForm(FormFactoryInterface $factory, $data = null);
223
}
224