GenericCRUDElement::configureOptions()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 33
rs 9.568
c 0
b 0
f 0
cc 2
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
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Admin\CRUD;
13
14
use FSi\Bundle\AdminBundle\Admin\AbstractElement;
15
use FSi\Bundle\AdminBundle\Exception\RuntimeException;
16
use FSi\Component\DataGrid\DataGridFactoryInterface;
17
use FSi\Component\DataGrid\DataGridInterface;
18
use FSi\Component\DataGrid\Exception\DataGridColumnException;
19
use FSi\Component\DataSource\DataSourceFactoryInterface;
20
use FSi\Component\DataSource\DataSourceInterface;
21
use Symfony\Component\Form\FormFactoryInterface;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\OptionsResolver\Options;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
26
abstract class GenericCRUDElement extends AbstractElement implements CRUDElement
27
{
28
    /**
29
     * @var DataSourceFactoryInterface
30
     */
31
    protected $datasourceFactory;
32
33
    /**
34
     * @var DataGridFactoryInterface
35
     */
36
    protected $datagridFactory;
37
38
    /**
39
     * @var FormFactoryInterface
40
     */
41
    protected $formFactory;
42
43
    public function getRoute(): string
44
    {
45
        return 'fsi_admin_list';
46
    }
47
48
    public function getSuccessRoute(): string
49
    {
50
        return $this->getRoute();
51
    }
52
53
    public function getSuccessRouteParameters(): array
54
    {
55
        return $this->getRouteParameters();
56
    }
57
58
    public function configureOptions(OptionsResolver $resolver): void
59
    {
60
        $resolver->setDefaults([
61
            'allow_delete' => true,
62
            'allow_add' => true,
63
            'template_crud_list' => null,
64
            'template_crud_create' => null,
65
            'template_crud_edit' => null,
66
            'template_list' => function (Options $options) {
67
                return $options['template_crud_list'];
68
            },
69
            'template_form' => function (Options $options) {
70
                return $options['template_crud_edit'];
71
            }
72
        ]);
73
74
        $resolver->setNormalizer('template_crud_create', function (Options $options, $value) {
75
            if ($value !== $options['template_crud_edit']) {
76
                throw new RuntimeException(
77
                    'CRUD admin element options "template_crud_create" and "template_crud_edit" have both to have the same value'
78
                );
79
            }
80
81
            return $value;
82
        });
83
84
        $resolver->setAllowedTypes('allow_delete', 'bool');
85
        $resolver->setAllowedTypes('allow_add', 'bool');
86
        $resolver->setAllowedTypes('template_crud_list', ['null', 'string']);
87
        $resolver->setAllowedTypes('template_crud_create', ['null', 'string']);
88
        $resolver->setAllowedTypes('template_crud_edit', ['null', 'string']);
89
        $resolver->setAllowedTypes('template_list', ['null', 'string']);
90
        $resolver->setAllowedTypes('template_form', ['null', 'string']);
91
    }
92
93
    public function apply($object): void
94
    {
95
        $this->delete($object);
96
    }
97
98
    public function setDataGridFactory(DataGridFactoryInterface $factory): void
99
    {
100
        $this->datagridFactory = $factory;
101
    }
102
103
    public function setDataSourceFactory(DataSourceFactoryInterface $factory): void
104
    {
105
        $this->datasourceFactory = $factory;
106
    }
107
108
    public function setFormFactory(FormFactoryInterface $factory): void
109
    {
110
        $this->formFactory = $factory;
111
    }
112
113
    public function createDataGrid(): DataGridInterface
114
    {
115
        $datagrid = $this->initDataGrid($this->datagridFactory);
116
117
        if ($this->getOption('allow_delete') && !$datagrid->hasColumnType('batch')) {
118
            $datagrid->addColumn('batch', 'batch', [
119
                'actions' => [
120
                    'delete' => [
121
                        'route_name' => 'fsi_admin_batch',
122
                        'additional_parameters' => ['element' => $this->getId()],
123
                        'label' => 'crud.list.batch.delete'
124
                    ]
125
                ],
126
                'display_order' => -1000
127
            ]);
128
        }
129
130
        return $datagrid;
131
    }
132
133
    public function createDataSource(): DataSourceInterface
134
    {
135
        return $this->initDataSource($this->datasourceFactory);
136
    }
137
138
    public function createForm($data = null): FormInterface
139
    {
140
        return $this->initForm($this->formFactory, $data);
141
    }
142
143
    abstract protected function initDataGrid(DataGridFactoryInterface $factory): DataGridInterface;
144
145
    abstract protected function initDataSource(DataSourceFactoryInterface $factory): DataSourceInterface;
146
147
    /**
148
     * Initialize form. This form will be used in create and update actions.
149
     *
150
     * @param FormFactoryInterface $factory
151
     * @param mixed $data
152
     * @return FormInterface
153
     */
154
    abstract protected function initForm(FormFactoryInterface $factory, $data = null): FormInterface;
155
}
156