ActionField::configureOptions()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 18
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 27
rs 9.3554
ccs 0
cts 14
cp 0
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Field;
6
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
/**
11
 * Display a link with button render and options.
12
 */
13
class ActionField extends AbstractField
14
{
15
    public function configureOptions(OptionsResolver $resolver): void
16
    {
17
        $resolver
18
            ->setDefaults([
19
                'template' => '@LAGAdmin/fields/action.html.twig',
20
                'translation' => true,
21
                'property_path' => null,
22
            ])
23
            ->addNormalizer('attr', function (Options $options, $value) {
24
                if (!empty($value['class'])) {
25
                    return $value;
26
                }
27
                $action = null;
28
29
                if ($options->offsetExists('action')) {
30
                    $action = $options->offsetGet('action');
31
                }
32
33
                if ('update' === $action) {
34
                    $value['class'] = 'btn btn-primary btn-sm';
35
                } elseif ('delete' === $action) {
36
                    $value['class'] = 'btn btn-danger btn-sm';
37
                } else {
38
                    $value['class'] = 'btn btn-secondary btn-sm';
39
                }
40
41
                return $value;
42
            })
43
        ;
44
    }
45
}
46