Completed
Push — master ( ac31e0...d01d70 )
by Jarek
10s
created

AbstractCRUD::setDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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