Completed
Branch master (b7eb35)
by Arnaud
02:59
created

Link::configureOptions()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 48
Code Lines 32

Duplication

Lines 20
Ratio 41.67 %

Code Coverage

Tests 28
CRAP Score 6.0106

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 20
loc 48
ccs 28
cts 30
cp 0.9333
rs 8.551
c 1
b 0
f 0
cc 6
eloc 32
nc 1
nop 1
crap 6.0106
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\EntityFieldInterface;
6
use LAG\AdminBundle\Field\Field;
7
use LAG\AdminBundle\Field\TwigFieldInterface;
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 EntityFieldInterface, TwigFieldInterface
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 2
    public function render($value)
27
    {
28 2
        $text = $this->options->get('text') ?: parent::render($value);
29 2
        $parameters = [];
30 2
        $accessor = PropertyAccess::createPropertyAccessor();
31
32 2
        foreach ($this->options->get('parameters') as $parameterName => $fieldName) {
33 1
            if (!$fieldName) {
34 1
                $fieldName = $parameterName;
35
            }
36 1
            $parameters[$parameterName] = $accessor->getValue($this->entity, $fieldName);
37
        }
38
39 2
        $render = $this->twig->render($this->options->get('template'), [
40 2
            'text' => $text,
41 2
            'route' => $this->options->get('route'),
42 2
            'parameters' => $parameters,
43 2
            'target' => $this->options->get('target'),
44 2
            'url' => $this->options->get('url'),
45 2
            'title' => $this->options->get('title'),
46 2
            'icon' => $this->options->get('icon'),
47
        ]);
48
49 2
        return $render;
50
    }
51
52
    /**
53
     * Configure options resolver.
54
     *
55
     * @param OptionsResolver $resolver
56
     * @return void
57
     */
58 2
    public function configureOptions(OptionsResolver $resolver)
59
    {
60
        // inherit parent's option
61 2
        parent::configureOptions($resolver);
62
63 2
        $resolver->setDefaults([
64 2
            'length' => $this->applicationConfiguration->getParameter('string_length'),
65 2
            'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'),
66 2
            'template' => $this->applicationConfiguration->getParameter('fields_template_mapping')[Field::TYPE_LINK],
67 2
            'title' => '',
68 2
            'icon' => '',
69 2
            'target' => '_self',
70 2
            'route' => '',
71
            'parameters' => [],
72 2
            'url' => '',
73 2
            'text' => '',
74
            'admin' => null,
75
            'action' => null
76
        ]);
77 2
        $resolver->setAllowedTypes('route', 'string');
78 2
        $resolver->setAllowedTypes('parameters', 'array');
79 2
        $resolver->setAllowedTypes('length', 'integer');
80 2
        $resolver->setAllowedTypes('url', 'string');
81 2
        $resolver->setAllowedValues('target', [
82 2
            '_self',
83
            '_blank',
84
        ]);
85 View Code Duplication
        $resolver->setNormalizer('route', function(Options $options, $value) {
86
            // route or url should be defined
87 2
            if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) {
88 1
                throw new InvalidOptionsException(
89 1
                    'You must set either an url or a route for the property "'.$this->name.'"'
90
                );
91
            }
92
93 2
            return $value;
94 2
        });
95 2 View Code Duplication
        $resolver->setNormalizer('admin', function(Options $options, $value) {
96
            // if a Admin is defined, an Action should be defined too
97 2
            if ($value && !$options->offsetGet('action')) {
98
                throw new InvalidOptionsException(
99
                    'An Action should be provided if an Admin is provided for property "'.$this->name.'"'
100
                );
101
            }
102
103 2
            return $value;
104 2
        });
105 2
    }
106
107
    /**
108
     * Define field type.
109
     *
110
     * @return string
111
     */
112
    public function getType()
113
    {
114
        return 'link';
115
    }
116
117
    /**
118
     * Define entity. It will be use to fill parameters with properties values.
119
     *
120
     * @param $entity
121
     */
122 1
    public function setEntity($entity)
123
    {
124 1
        $this->entity = $entity;
125 1
    }
126
}
127