Passed
Push — develop ( b8535a...b19a66 )
by Mathieu
11:29
created

Admin::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 45
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 19

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\Annotation\Sonata;
6
7
use Attribute;
8
use InvalidArgumentException;
9
use Neimheadh\SonataAnnotationBundle\Admin\AnnotationAdmin;
10
use Neimheadh\SonataAnnotationBundle\Annotation\AbstractAnnotation;
11
use ReflectionException;
12
13
/**
14
 * Admin annotation.
15
 *
16
 * Auto-build the admin service of your model class.
17
 *
18
 * @Annotation
19
 * @Target("CLASS")
20
 *
21
 * @author Marko Kunic <[email protected]>
22
 * @author Mathieu Wambre <[email protected]>
23
 *
24
 * @property FormField[] $formFields
25
 */
26
#[Attribute(Attribute::TARGET_CLASS)]
27
final class Admin extends AbstractAnnotation
28
{
29
30
    /**
31
     * Admin service class.
32
     *
33
     * @var string
34
     */
35
    public string $admin = AnnotationAdmin::class;
36
37
    /**
38
     * Admin code.
39
     *
40
     * @var string|null
41
     */
42
    public ?string $code = null;
43
44
    /**
45
     * Admin controller.
46
     *
47
     * @var string|null
48
     */
49
    public ?string $controller = null;
50
51
    /**
52
     * Admin group.
53
     *
54
     * @var string|null
55
     */
56
    public ?string $group = null;
57
58
    /**
59
     * Admin link icon.
60
     *
61
     * @var string|null
62
     */
63
    public ?string $icon = null;
64
65
    /**
66
     * Is admin is kept open.
67
     *
68
     * @var bool
69
     */
70
    public bool $keepOpen = false;
71
72
    /**
73
     * Admin label.
74
     *
75
     * @var string|null
76
     */
77
    public ?string $label = null;
78
79
    /**
80
     * Admin label translation catalogue.
81
     *
82
     * @var string|null
83
     */
84
    public ?string $labelCatalogue = null;
85
86
    /**
87
     * Admin label translator strategy.
88
     *
89
     * @var string|null
90
     */
91
    public ?string $labelTranslatorStrategy = null;
92
93
    /**
94
     * Admin model manager type.
95
     *
96
     * @var string
97
     */
98
    public string $managerType = 'orm';
99
100
    /**
101
     * Is admin a top menu?
102
     *
103
     * This option put your admin link directly in the menu and not as a
104
     * sub-menu.
105
     *
106
     * @var bool
107
     */
108
    public bool $onTop = false;
109
110
    /**
111
     * Admin pager type.
112
     *
113
     * @var string|null
114
     */
115
    public ?string $pagerType = null;
116
117
    /**
118
     * Admin service id.
119
     *
120
     * @var string|null
121
     */
122
    public ?string $serviceId = null;
123
124
    /**
125
     * Is admin shown in dashboard?
126
     *
127
     * @var bool
128
     */
129
    public bool $showInDashboard = true;
130
131
    /**
132
     * Datagrid fields.
133
     *
134
     * @var array<DatagridField>
135
     */
136
    private array $datagridFields = [];
137
138
    /**
139
     * Export fields.
140
     *
141
     * @var array<ExportField>
142
     */
143
    private array $exportFields = [];
144
145
    /**
146
     * Form fields.
147
     *
148
     * @var array<FormField>
149
     */
150
    private array $formFields = [];
151
152
    /**
153
     * List fields.
154
     *
155
     * @var array<ListField>
156
     */
157
    private array $listFields = [];
158
159
    /**
160
     * Show fields.
161
     *
162
     * @var array<ShowField>
163
     */
164
    private array $showFields = [];
165
166
    /**
167
     * @param array|string|null $label                    Label or annotation
168
     *                                                    parameters.
169
     * @param string            $managerType              Model manager type.
170
     * @param string|null       $group                    Admin group.
171
     * @param bool              $showInDashboard          Show in dashboard?
172
     * @param bool              $keepOpen                 Keep open.
173
     * @param bool              $onTop                    Is admin a top menu?
174
     * @param string|null       $icon                     Admin link icon.
175
     * @param string|null       $labelTranslatorStrategy  Label translator
176
     *                                                    strategy.
177
     * @param string|null       $labelCatalogue           Admin label
178
     *                                                    translation
179
     *                                                    catalogue.
180
     * @param string|null       $pagerType                Pager type.
181
     * @param string|null       $controller               Controller.
182
     * @param string|null       $serviceId                Service id.
183
     * @param string            $admin                    Service class.
184
     * @param string|null       $code                     Code.
185
     * @param DatagridField[]   $datagridFields           Datagrid fields.
186
     * @param FormField[]       $formFields               Form fields.
187
     * @param ListField[]       $listFields               List fields.
188
     * @param ShowField[]       $showFields               Show fields.
189
     *
190
     * @throws ReflectionException
191
     */
192
    public function __construct(
193
        array|string $label = null,
194
        string $managerType = 'orm',
195
        ?string $group = null,
196
        bool $showInDashboard = true,
197
        bool $keepOpen = false,
198
        bool $onTop = false,
199
        ?string $icon = null,
200
        ?string $labelTranslatorStrategy = null,
201
        ?string $labelCatalogue = null,
202
        ?string $pagerType = null,
203
        ?string $controller = null,
204
        ?string $serviceId = null,
205
        string $admin = AnnotationAdmin::class,
206
        ?string $code = null,
207
        array $datagridFields = [],
208
        array $formFields = [],
209
        array $listFields = [],
210
        array $showFields = [],
211
        array $exportFields = [],
212
    ) {
213
        $this->managerType = $managerType;
214
        $this->group = $group;
215
        $this->showInDashboard = $showInDashboard;
216
        $this->keepOpen = $keepOpen;
217
        $this->onTop = $onTop;
218
        $this->icon = $icon;
219
        $this->labelTranslatorStrategy = $labelTranslatorStrategy;
220
        $this->labelCatalogue = $labelCatalogue;
221
        $this->pagerType = $pagerType;
222
        $this->controller = $controller;
223
        $this->serviceId = $serviceId;
224
        $this->admin = $admin;
225
        $this->code = $code;
226
227
        $this->setDatagridFields($datagridFields)
228
            ->setFormFields($formFields)
229
            ->setListFields($listFields)
230
            ->setShowFields($showFields)
231
            ->setExportFields($exportFields);
232
233
        if (is_array($label)) {
234
            $this->initAnnotation($label);
235
        } else {
236
            $this->label = $label;
237
        }
238
    }
239
240
    /**
241
     * Get service "sonata.admin" tag options.
242
     *
243
     * @return array<string|bool>
244
     */
245
    public function getTagOptions(): array
246
    {
247
        return [
248
            'code' => $this->code,
249
            'controller' => $this->controller,
250
            'manager_type' => $this->managerType,
251
            'group' => $this->group,
252
            'label' => $this->label,
253
            'show_in_dashboard' => $this->showInDashboard,
254
            'keep_open' => $this->keepOpen,
255
            'on_top' => $this->onTop,
256
            'icon' => $this->icon,
257
            'label_translator_strategy' => $this->labelTranslatorStrategy,
258
            'label_catalogue' => $this->labelCatalogue,
259
            'pager_type' => $this->pagerType,
260
        ];
261
    }
262
263
    /**
264
     * Get datagrid fields.
265
     *
266
     * @return array<DatagridField>
267
     */
268
    public function getDatagridFields(): array
269
    {
270
        return $this->datagridFields;
271
    }
272
273
    /**
274
     * Get export fields.
275
     *
276
     * @return array<ExportField>
277
     */
278
    public function getExportFields(): array
279
    {
280
        return $this->exportFields;
281
    }
282
283
    /**
284
     * Get form fields.
285
     *
286
     * @return array<FormField>
287
     */
288
    public function getFormFields(): array
289
    {
290
        return $this->formFields;
291
    }
292
293
    /**
294
     * Get list fields.
295
     *
296
     * @return array<ListField>
297
     */
298
    public function getListFields(): array
299
    {
300
        return $this->listFields;
301
    }
302
303
    /**
304
     * Get show fields.
305
     *
306
     * @return array<ShowField>
307
     */
308
    public function getShowFields(): array
309
    {
310
        return $this->showFields;
311
    }
312
313
    /**
314
     * Set datagrid fields.
315
     *
316
     * @param array<DatagridField> $datagridFields Datagrid fields.
317
     *
318
     * @return $this
319
     */
320
    public function setDatagridFields(array $datagridFields): self
321
    {
322
        ($e = $this->getInvalidArrayException(
323
            DatagridField::class,
324
            $datagridFields
325
        )) && throw $e;
326
327
        $this->datagridFields = $datagridFields;
328
329
        return $this;
330
    }
331
332
    /**
333
     * Set export fields.
334
     *
335
     * @param array<ExportField> $exportFields Export fields.
336
     *
337
     * @return $this
338
     */
339
    public function setExportFields(array $exportFields): self
340
    {
341
        ($e = $this->getInvalidArrayException(
342
            ExportField::class,
343
            $exportFields
344
        )) && throw $e;
345
346
        $this->exportFields = $exportFields;
347
348
        return $this;
349
    }
350
351
    /**
352
     * Set form fields.
353
     *
354
     * @param array<FormField> $formFields Form fields.
355
     *
356
     * @return $this
357
     */
358
    public function setFormFields(array $formFields): self
359
    {
360
        ($e = $this->getInvalidArrayException(
361
            FormField::class,
362
            $formFields
363
        )) && throw $e;
364
365
        $this->formFields = $formFields;
366
367
        return $this;
368
    }
369
370
    /**
371
     * Set list fields.
372
     *
373
     * @param array<ListField> $listFields List fields.
374
     *
375
     * @return $this
376
     */
377
    public function setListFields(array $listFields): self
378
    {
379
        ($e = $this->getInvalidArrayException(
380
            ListField::class,
381
            $listFields
382
        )) && throw $e;
383
384
        $this->listFields = $listFields;
385
386
        return $this;
387
    }
388
389
    /**
390
     * Set show fields.
391
     *
392
     * @param array<ShowField> $showFields Show fields.
393
     *
394
     * @return $this
395
     */
396
    public function setShowFields(array $showFields): self
397
    {
398
        ($e = $this->getInvalidArrayException(
399
            ShowField::class,
400
            $showFields
401
        )) && throw $e;
402
403
        $this->showFields = $showFields;
404
405
        return $this;
406
    }
407
408
    /**
409
     * Get invalid argument exception for typed array.
410
     *
411
     * @param string $class Excepted class.
412
     * @param array  $array Given array.
413
     *
414
     * @return InvalidArgumentException|null
415
     */
416
    private function getInvalidArrayException(
417
        string $class,
418
        array $array
419
    ): ?InvalidArgumentException {
420
        foreach ($array as $entry) {
421
            if (!is_object($entry) || !$entry instanceof $class) {
422
                return new InvalidArgumentException(
423
                    sprintf(
424
                        'Array of %s expected, array contains an %s element.',
425
                        $class,
426
                        is_object($entry) ? $entry::class : gettype($entry)
427
                    )
428
                );
429
            }
430
        }
431
432
        return null;
433
    }
434
435
}
436