Completed
Pull Request — master (#90)
by Arnaud
16:42
created

LinkConfiguration::configureOptions()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 54
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 48
cp 0
rs 8.7449
c 0
b 0
f 0
cc 6
eloc 38
nc 1
nop 1
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LAG\AdminBundle\Field\Configuration;
4
5
use JK\Configuration\Configuration;
6
use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration;
7
use LAG\AdminBundle\Application\Configuration\ApplicationConfigurationAwareInterface;
8
use LAG\AdminBundle\Field\AbstractField;
9
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
10
use Symfony\Component\OptionsResolver\Options;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class LinkConfiguration extends Configuration implements ApplicationConfigurationAwareInterface
14
{
15
    /**
16
     * @var ApplicationConfiguration
17
     */
18
    protected $applicationConfiguration;
19
    
20
    public function configureOptions(OptionsResolver $resolver)
21
    {
22
        // inherit parent's option
23
        parent::configureOptions($resolver);
24
25
        $resolver->setDefaults([
26
            'length' => $this
27
                ->applicationConfiguration
28
                ->getParameter('string_length'),
29
            'replace' => $this
30
                ->applicationConfiguration
31
                ->getParameter('string_length_truncate'),
32
            'template' => $this
33
                ->applicationConfiguration
34
                ->getParameter('fields_template_mapping')[AbstractField::TYPE_LINK],
35
            'title' => '',
36
            'icon' => '',
37
            'target' => '_self',
38
            'route' => '',
39
            'parameters' => [],
40
            'url' => '',
41
            'text' => '',
42
            'admin' => null,
43
            'action' => null,
44
        ]);
45
        $resolver->setAllowedTypes('route', 'string');
46
        $resolver->setAllowedTypes('parameters', 'array');
47
        $resolver->setAllowedTypes('length', 'integer');
48
        $resolver->setAllowedTypes('url', 'string');
49
        $resolver->setAllowedValues('target', [
50
            '_self',
51
            '_blank',
52
        ]);
53
        $resolver->setNormalizer('route', function(Options $options, $value) {
54
            // route or url should be defined
55
            if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) {
56
                throw new InvalidOptionsException(
57
                    'You must set either an url or a route for the property'
58
                );
59
            }
60
61
            return $value;
62
        });
63
        $resolver->setNormalizer('admin', function(Options $options, $value) {
64
            // if a Admin is defined, an Action should be defined too
65
            if ($value && !$options->offsetGet('action')) {
66
                throw new InvalidOptionsException(
67
                    'An Action should be provided if an Admin is provided'
68
                );
69
            }
70
71
            return $value;
72
        });
73
    }
74
    
75
    /**
76
     * Define the application configuration.
77
     *
78
     * @param ApplicationConfiguration $configuration
79
     */
80
    public function setApplicationConfiguration(ApplicationConfiguration $configuration)
81
    {
82
        $this->applicationConfiguration = $configuration;
83
    }
84
}
85