Completed
Pull Request — master (#90)
by Arnaud
03:09 queued 01:17
created

Link::configureOptions()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 54
Code Lines 38

Duplication

Lines 20
Ratio 37.04 %

Code Coverage

Tests 33
CRAP Score 6.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 20
loc 54
ccs 33
cts 35
cp 0.9429
rs 8.7449
c 1
b 0
f 0
cc 6
eloc 38
nc 1
nop 1
crap 6.0067

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\Field;
4
5
use LAG\AdminBundle\Field\AbstractField;
6
use LAG\AdminBundle\Field\EntityAwareInterface;
7
use LAG\AdminBundle\Field\TwigAwareInterface;
8
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Symfony\Component\PropertyAccess\PropertyAccess;
12
13
class Link extends StringField implements EntityAwareInterface, TwigAwareInterface
14
{
15
    /**
16
     * @var Object
17
     */
18
    protected $entity;
19
20
    /**
21
     * Render link template filled with configured options.
22
     *
23
     * @param mixed $value
24
     * @return string
25
     */
26 1
    public function render($value)
27
    {
28 1
        $text = $this->options->get('text') ?: parent::render($value);
29 1
        $parameters = [];
30 1
        $accessor = PropertyAccess::createPropertyAccessor();
31
32 1
        foreach ($this->options->get('parameters') as $parameterName => $fieldName) {
33
            if (!$fieldName) {
34
                $fieldName = $parameterName;
35
            }
36
            $parameters[$parameterName] = $accessor->getValue($this->entity, $fieldName);
37
        }
38
39 1
        $render = $this->twig->render($this->options->get('template'), [
40 1
            'text' => $text,
41 1
            'parameters' => $parameters,
42 1
            'options' => $this->options,
43
        ]);
44
45
        return $render;
46
    }
47
48
    /**
49
     * Configure options resolver.
50
     *
51
     * @param OptionsResolver $resolver
52
     * @return void
53
     */
54 1
    public function configureOptions(OptionsResolver $resolver)
55
    {
56
        // inherit parent's option
57 1
        parent::configureOptions($resolver);
58
59 1
        $resolver->setDefaults([
60 1
            'length' => $this
61 1
                ->applicationConfiguration
62 1
                ->getParameter('string_length'),
63
            'replace' => $this
64 1
                ->applicationConfiguration
65 1
                ->getParameter('string_length_truncate'),
66
            'template' => $this
67 1
                ->applicationConfiguration
68 1
                ->getParameter('fields_template_mapping')[AbstractField::TYPE_LINK],
69 1
            'title' => '',
70 1
            'icon' => '',
71 1
            'target' => '_self',
72 1
            'route' => '',
73
            'parameters' => [],
74 1
            'url' => '',
75 1
            'text' => '',
76
            'admin' => null,
77
            'action' => null,
78
        ]);
79 1
        $resolver->setAllowedTypes('route', 'string');
80 1
        $resolver->setAllowedTypes('parameters', 'array');
81 1
        $resolver->setAllowedTypes('length', 'integer');
82 1
        $resolver->setAllowedTypes('url', 'string');
83 1
        $resolver->setAllowedValues('target', [
84 1
            '_self',
85
            '_blank',
86
        ]);
87 1 View Code Duplication
        $resolver->setNormalizer('route', function(Options $options, $value) {
88
            // route or url should be defined
89 1
            if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) {
90 1
                throw new InvalidOptionsException(
91 1
                    'You must set either an url or a route for the property "'.$this->name.'"'
92
                );
93
            }
94
95 1
            return $value;
96 1
        });
97 1 View Code Duplication
        $resolver->setNormalizer('admin', function(Options $options, $value) {
98
            // if a Admin is defined, an Action should be defined too
99 1
            if ($value && !$options->offsetGet('action')) {
100
                throw new InvalidOptionsException(
101
                    'An Action should be provided if an Admin is provided for property "'.$this->name.'"'
102
                );
103
            }
104
105 1
            return $value;
106 1
        });
107 1
    }
108
109
    /**
110
     * Define field type.
111
     *
112
     * @return string
113
     */
114
    public function getType()
115
    {
116
        return 'link';
117
    }
118
119
    /**
120
     * Define entity. It will be use to fill parameters with properties values.
121
     *
122
     * @param $entity
123
     */
124
    public function setEntity($entity)
125
    {
126
        $this->entity = $entity;
127
    }
128
}
129