Passed
Push — develop ( e15501...ec0ba5 )
by Mathieu
02:23
created

AnnotationAdmin::configureDefaultSortValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\Admin;
6
7
use Exception;
8
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\AddRoute;
9
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\Admin;
10
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\RemoveRoute;
11
use Neimheadh\SonataAnnotationBundle\Reader\ActionButtonReader;
12
use Neimheadh\SonataAnnotationBundle\Reader\DashboardActionReader;
13
use Neimheadh\SonataAnnotationBundle\Reader\DatagridReader;
14
use Neimheadh\SonataAnnotationBundle\Reader\DatagridValuesReader;
15
use Neimheadh\SonataAnnotationBundle\Reader\ExportReader;
16
use Neimheadh\SonataAnnotationBundle\Reader\FormReader;
17
use Neimheadh\SonataAnnotationBundle\Reader\ListReader;
18
use Neimheadh\SonataAnnotationBundle\Reader\RouteReader;
19
use Neimheadh\SonataAnnotationBundle\Reader\ShowReader;
20
use ReflectionClass;
21
use ReflectionException;
22
use Sonata\AdminBundle\Admin\AbstractAdmin;
23
use Sonata\AdminBundle\Datagrid\DatagridInterface;
24
use Sonata\AdminBundle\Datagrid\DatagridMapper;
25
use Sonata\AdminBundle\Datagrid\ListMapper;
26
use Sonata\AdminBundle\Form\FormMapper;
27
use Sonata\AdminBundle\Route\RouteCollectionInterface;
28
use Sonata\AdminBundle\Show\ShowMapper;
29
30
/**
31
 * Auto-created admin class by annotations.
32
 *
33
 * @author Marko Kunic <[email protected]>
34
 * @author Mathieu Wambre <[email protected]>
35
 */
36
class AnnotationAdmin extends AbstractAdmin
37
{
38
39
    /**
40
     * Action buttons annotation reader.
41
     *
42
     * @var ActionButtonReader
43
     */
44
    private ActionButtonReader $actionButtonReader;
45
46
    /**
47
     * Datagrid annotation reader.
48
     *
49
     * @var DatagridReader
50
     */
51
    private DatagridReader $datagridReader;
52
53
    /**
54
     * Datagrid values annotation reader.
55
     *
56
     * @var DatagridValuesReader
57
     * @todo Replace the previously buildDatagrid() to use datagrid values
58
     *       reader.
59
     */
60
    private DatagridValuesReader $datagridValuesReader;
61
62
    /**
63
     * Dashboard actions annotation reader.
64
     *
65
     * @var DashboardActionReader
66
     */
67
    private DashboardActionReader $dashboardActionReader;
68
69
    /**
70
     * Export annotation reader.
71
     *
72
     * @var ExportReader
73
     */
74
    private ExportReader $exportReader;
75
76
    /**
77
     * Form page annotation reader.
78
     *
79
     * @var FormReader
80
     */
81
    private FormReader $formReader;
82
83
    /**
84
     * List page annotation reader.
85
     *
86
     * @var ListReader
87
     */
88
    private ListReader $listReader;
89
90
    /**
91
     * Route configuration annotation reader.
92
     *
93
     * @var RouteReader
94
     */
95
    private RouteReader $routeReader;
96
97
    /**
98
     * Show page annotation reader.
99
     *
100
     * @var ShowReader
101
     */
102
    private ShowReader $showReader;
103
104
    /**
105
     * @param array                 $options               Admin options.
106
     * @param ActionButtonReader    $actionButtonReader    Action buttons
107
     *                                                     annotation reader.
108
     * @param DatagridReader        $datagridReader        Datagrid annotation
109
     *                                                     reader.
110
     * @param DatagridValuesReader  $datagridValuesReader  Datagrid values
111
     *                                                     annotation reader.
112
     * @param DashboardActionReader $dashboardActionReader Dashboard actions
113
     *                                                     annotation reader.
114
     * @param ExportReader          $exportReader          Export annotation
115
     *                                                     reader.
116
     * @param FormReader            $formReader            Form page annotation
117
     *                                                     reader.
118
     * @param ListReader            $listReader            List page annotation
119
     *                                                     reader.
120
     * @param RouteReader           $routeReader           Route configuration
121
     *                                                     annotation reader.
122
     * @param ShowReader            $showReader            Show page annotation
123
     *                                                     reader.
124
     */
125
    public function __construct(
126
        private array $options,
127
        ActionButtonReader $actionButtonReader,
128
        DatagridReader $datagridReader,
129
        DatagridValuesReader $datagridValuesReader,
130
        DashboardActionReader $dashboardActionReader,
131
        ExportReader $exportReader,
132
        FormReader $formReader,
133
        ListReader $listReader,
134
        RouteReader $routeReader,
135
        ShowReader $showReader
136
    ) {
137
        parent::__construct();
138
139
        $this->actionButtonReader = $actionButtonReader;
140
        $this->datagridReader = $datagridReader;
141
        $this->datagridValuesReader = $datagridValuesReader;
142
        $this->dashboardActionReader = $dashboardActionReader;
143
        $this->listReader = $listReader;
144
        $this->exportReader = $exportReader;
145
        $this->formReader = $formReader;
146
        $this->routeReader = $routeReader;
147
        $this->showReader = $showReader;
148
    }
149
150
    /**
151
     * Get the list of exported formats.
152
     *
153
     * @return array<string>
154
     * @throws ReflectionException
155
     */
156
    public function getExportFormats(): array
157
    {
158
        return $this->exportReader->getFormats(
159
            $this->getReflectionClass()
160
        ) ?: parent::getExportFormats();
161
    }
162
163
    /**
164
     * Configure action buttons.
165
     *
166
     * @param array       $buttonList Base button list.
167
     * @param string      $action     Current action.
168
     * @param object|null $object     Current object.
169
     *
170
     * @return array
171
     * @throws ReflectionException
172
     */
173
    protected function configureActionButtons(
174
        array $buttonList,
175
        string $action,
176
        ?object $object = null
177
    ): array {
178
        return $this->actionButtonReader
179
            ->getActions(
180
                $this->getReflectionClass(),
181
                parent::configureActionButtons(
182
                    $buttonList,
183
                    $action,
184
                    $object
185
                )
186
            );
187
    }
188
189
    /**
190
     * Configure dashboard actions.
191
     *
192
     * @param array<string, array<string, mixed>> $actions Base actions.
193
     *
194
     * @return array<string, array<string, mixed>>
195
     * @throws ReflectionException
196
     * @throws Exception
197
     */
198
    protected function configureDashboardActions(array $actions): array
199
    {
200
        return $this->dashboardActionReader
201
            ->getActions(
202
                $this->getReflectionClass(),
203
                parent::configureDashboardActions($actions)
204
            );
205
    }
206
207
    /**
208
     * Configure datagrid filters.
209
     *
210
     * @param DatagridMapper $filter Datagrid filter mapper.
211
     *
212
     * @return void
213
     * @throws ReflectionException
214
     */
215
    protected function configureDatagridFilters(DatagridMapper $filter): void
216
    {
217
        $this->datagridReader->configureFields(
218
            $this->getReflectionClass(),
219
            $filter
220
        );
221
    }
222
223
    /**
224
     * Configure default sorting values.
225
     *
226
     * @param array $sortValues Sorting values.
227
     *
228
     * @return void
229
     */
230
    protected function configureDefaultSortValues(array &$sortValues): void
231
    {
232
        $values = $this->options[Admin::OPTION_SORT_VALUES] ?? [];
233
        $fields = [
234
            DatagridInterface::PAGE,
235
            DatagridInterface::PER_PAGE,
236
            DatagridInterface::SORT_ORDER,
237
            DatagridInterface::SORT_BY,
238
        ];
239
240
        foreach ($fields as $field) {
241
            if (($values[$field] ?? null) !== null) {
242
                $sortValues[$field] = $values[$field];
243
            }
244
        }
245
    }
246
247
    /**
248
     * Get exported fields.
249
     *
250
     * @return array<string>
251
     * @throws ReflectionException
252
     */
253
    protected function configureExportFields(): array
254
    {
255
        return $this->exportReader->getFields(
256
            $this->getReflectionClass()
257
        ) ?: parent::getExportFields();
258
    }
259
260
    /**
261
     * Configure form page fields.
262
     *
263
     * @param FormMapper $form Form mapper.
264
     *
265
     * @return void
266
     * @throws ReflectionException
267
     */
268
    protected function configureFormFields(FormMapper $form): void
269
    {
270
        if ($this->getRequest()->get($this->getIdParameter())) {
271
            $this->formReader->configureEditFields(
272
                $this->getReflectionClass(),
273
                $form
274
            );
275
            return;
276
        }
277
278
        $this->formReader->configureCreateFields(
279
            $this->getReflectionClass(),
280
            $form
281
        );
282
    }
283
284
    /**
285
     * Configure list page fields.
286
     *
287
     * @param ListMapper $list List mapper.
288
     *
289
     * @return void
290
     * @throws ReflectionException
291
     */
292
    protected function configureListFields(ListMapper $list): void
293
    {
294
        $this->listReader
295
            ->configureFields(
296
                $this->getReflectionClass(),
297
                $list
298
            );
299
    }
300
301
    /**
302
     * Configure routes.
303
     *
304
     * @param RouteCollectionInterface $collection Route collection.
305
     *
306
     * @return void
307
     * @throws ReflectionException
308
     */
309
    protected function configureRoutes(
310
        RouteCollectionInterface $collection
311
    ): void {
312
        [$addRoutes, $removeRoutes] = $this->routeReader->getRoutes(
313
            $this->getReflectionClass()
314
        );
315
316
        /** @var AddRoute $route */
317
        foreach ($addRoutes as $route) {
318
            $collection->add(
319
                $route->name,
320
                $route->path ? $this->replaceIdParameterInRoutePath(
321
                    $route->path
322
                ) : $route->getName()
323
            );
324
        }
325
326
        /** @var RemoveRoute $route */
327
        foreach ($removeRoutes as $route) {
328
            $collection->remove($route->name);
329
        }
330
    }
331
332
    /**
333
     * Configure show page fields.
334
     *
335
     * @param ShowMapper $show Show mapper.
336
     *
337
     * @return void
338
     * @throws ReflectionException
339
     */
340
    protected function configureShowFields(ShowMapper $show): void
341
    {
342
        $this->showReader->configureFields(
343
            $this->getReflectionClass(),
344
            $show
345
        );
346
    }
347
348
    /**
349
     * Replace the '{id}' part in the given path with the currently managed
350
     * object id.
351
     *
352
     * @param string $path Path template.
353
     *
354
     * @return string
355
     */
356
    private function replaceIdParameterInRoutePath(string $path): string
357
    {
358
        return str_replace(
359
            AddRoute::ID_PARAMETER,
360
            $this->getRouterIdParameter(),
361
            $path
362
        );
363
    }
364
365
    /**
366
     * Get the reflection class of the current admin model.
367
     *
368
     * @return ReflectionClass
369
     * @throws ReflectionException
370
     */
371
    private function getReflectionClass(): ReflectionClass
372
    {
373
        return new ReflectionClass($this->getClass());
374
    }
375
376
}
377