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

LinkConfiguration::configureOptions()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 8.551
c 0
b 0
f 0
cc 6
eloc 33
nc 1
nop 1
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
        parent::configureOptions($resolver);
18
19
        $resolver->setDefaults([
20
            'template' => $this
21
                ->applicationConfiguration
22
                ->getParameter('fields_template_mapping')[AbstractField::TYPE_LINK],
23
            'title' => '',
24
            'icon' => '',
25
            'target' => '_self',
26
            'route' => '',
27
            'parameters' => [],
28
            'url' => '',
29
            'text' => '',
30
            'admin' => null,
31
            'action' => null,
32
            'class' => '',
33
        ]);
34
        $resolver->setAllowedTypes('route', 'string');
35
        $resolver->setAllowedTypes('parameters', 'array');
36
        $resolver->setAllowedTypes('length', 'integer');
37
        $resolver->setAllowedTypes('url', 'string');
38
        $resolver->setAllowedValues('target', [
39
            '_self',
40
            '_blank',
41
        ]);
42
        $resolver->setNormalizer('route', function(Options $options, $value) {
43
            // route or url should be defined
44
            if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) {
45
                throw new InvalidOptionsException(
46
                    'You must set either an url or a route for the property'
47
                );
48
            }
49
50
            return $value;
51
        });
52
        $resolver->setNormalizer('admin', function(Options $options, $value) {
53
            // if a Admin is defined, an Action should be defined too
54
            if ($value && !$options->offsetGet('action')) {
55
                throw new InvalidOptionsException(
56
                    'An Action should be provided if an Admin is provided'
57
                );
58
            }
59
60
            return $value;
61
        });
62
    }
63
}
64