Passed
Push — master ( bb67c8...cd2c1d )
by Arnaud
14:35 queued 11:07
created

LinkField   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 32
c 3
b 0
f 1
dl 0
loc 54
ccs 16
cts 22
cp 0.7272
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getParent() 0 3 1
B configureOptions() 0 45 8
1
<?php
2
3
namespace LAG\AdminBundle\Field;
4
5
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
6
use Symfony\Component\OptionsResolver\Options;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class LinkField extends AbstractField implements ApplicationAwareInterface
10
{
11
    use ApplicationAware;
12
13
    public function getParent(): ?string
14
    {
15
        return StringField::class;
16
    }
17
18 2
    public function configureOptions(OptionsResolver $resolver): void
19
    {
20 2
        $appConfig = $this->getApplicationConfiguration();
21
22
        $resolver
23 2
            ->setDefaults([
24 2
                'admin' => null,
25
                'action' => null,
26
                'default' => '~',
27
                'icon' => null,
28
                'mapped' => true,
29
                'route' => null,
30
                'route_parameters' => [],
31
                'target' => '_self',
32
                'template' => '@LAGAdmin/fields/link.html.twig',
33
                'text' => '',
34
                'title' => null,
35
                'url' => '',
36
            ])
37 2
            ->setNormalizer('route', function (Options $options, $value) use ($appConfig) {
38
                // A route, an url or an admin should be defined
39 2
                if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) {
40
                    throw new InvalidOptionsException('Either an url or a route should be defined');
41
                }
42
43 2
                if ($options->offsetGet('admin')) {
44
                    $value = $appConfig->getRouteName($options->offsetGet('admin'), $options->offsetGet('action'));
45
                }
46
47 2
                return $value;
48 2
            })
49 2
            ->setNormalizer('admin', function (Options $options, $value) {
50
                // if a Admin is defined, an Action should be defined too
51 2
                if ($value && !$options->offsetGet('action')) {
52
                    throw new InvalidOptionsException('An Action should be provided if an Admin is provided');
53
                }
54
55 2
                return $value;
56 2
            })
57 2
            ->setNormalizer('text', function (Options $options, $value) {
58 2
                if ($value) {
59
                    return $value;
60
                }
61
62 2
                return ucfirst($options->offsetGet('route'));
63 2
            })
64
        ;
65 2
    }
66
}
67