Completed
Pull Request — master (#90)
by Arnaud
02:11
created

LinkConfiguration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 57
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B configureOptions() 0 51 6
1
<?php
2
3
namespace LAG\AdminBundle\Field\Configuration;
4
5
use LAG\AdminBundle\Field\AbstractField;
6
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class LinkConfiguration extends StringFieldConfiguration
11
{
12
    /**
13
     * @param OptionsResolver $resolver
14
     */
15
    public function configureOptions(OptionsResolver $resolver)
16
    {
17
        $resolver->setDefaults([
18
            'length' => $this
19
                ->applicationConfiguration
20
                ->getParameter('string_length'),
21
            'replace' => $this
22
                ->applicationConfiguration
23
                ->getParameter('string_length_truncate'),
24
            'template' => $this
25
                ->applicationConfiguration
26
                ->getParameter('fields_template_mapping')[AbstractField::TYPE_LINK],
27
            'title' => '',
28
            'icon' => '',
29
            'target' => '_self',
30
            'route' => '',
31
            'parameters' => [],
32
            'url' => '',
33
            'text' => '',
34
            'admin' => null,
35
            'action' => null,
36
        ]);
37
        $resolver->setAllowedTypes('route', 'string');
38
        $resolver->setAllowedTypes('parameters', 'array');
39
        $resolver->setAllowedTypes('length', 'integer');
40
        $resolver->setAllowedTypes('url', 'string');
41
        $resolver->setAllowedValues('target', [
42
            '_self',
43
            '_blank',
44
        ]);
45
        $resolver->setNormalizer('route', function(Options $options, $value) {
46
            // route or url should be defined
47
            if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) {
48
                throw new InvalidOptionsException(
49
                    'You must set either an url or a route for the property'
50
                );
51
            }
52
53
            return $value;
54
        });
55
        $resolver->setNormalizer('admin', function(Options $options, $value) {
56
            // if a Admin is defined, an Action should be defined too
57
            if ($value && !$options->offsetGet('action')) {
58
                throw new InvalidOptionsException(
59
                    'An Action should be provided if an Admin is provided'
60
                );
61
            }
62
63
            return $value;
64
        });
65
    }
66
}
67