Completed
Pull Request — dev (#9)
by Arnaud
03:07
created

Link   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 90.57%

Importance

Changes 3
Bugs 3 Features 1
Metric Value
wmc 9
c 3
b 3
f 1
lcom 2
cbo 9
dl 0
loc 101
ccs 48
cts 53
cp 0.9057
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 25 4
B configureOptions() 0 35 3
A getType() 0 4 1
A setEntity() 0 4 1
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 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 1
            if (!$fieldName) {
34
                $fieldName = $parameterName;
35
            }
36 1
            $parameters[$parameterName] = $accessor->getValue($this->entity, $fieldName);
37 1
        }
38
39 1
        $render = $this->twig->render($this->options->get('template'), [
40 1
            'text' => $text,
41 1
            'route' => $this->options->get('route'),
42 1
            'parameters' => $this->options->get('parameters'),
43 1
            'target' => $this->options->get('target'),
44 1
            'url' => $this->options->get('url'),
45 1
            'title' => $this->options->get('title'),
46 1
            'icon' => $this->options->get('icon'),
47 1
        ]);
48
49 1
        return $render;
50
    }
51
52
    /**
53
     * Configure options resolver.
54
     *
55
     * @param OptionsResolver $resolver
56
     * @return void
57
     */
58 1
    public function configureOptions(OptionsResolver $resolver)
59
    {
60
        // inherit parent's option
61 1
        parent::configureOptions($resolver);
62
63 1
        $resolver->setDefaults([
64 1
            'length' => $this->applicationConfiguration->getParameter('string_length'),
65 1
            'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'),
66 1
            'template' => $this->applicationConfiguration->getParameter('fields_template_mapping')[Field::TYPE_LINK],
67 1
            'title' => '',
68 1
            'icon' => '',
69 1
            'target' => '_self',
70 1
            'route' => '',
71 1
            'parameters' => [],
72 1
            'url' => '',
73 1
            'text' => '',
74 1
        ]);
75 1
        $resolver->setAllowedTypes('route', 'string');
76 1
        $resolver->setAllowedTypes('parameters', 'array');
77 1
        $resolver->setAllowedTypes('length', 'integer');
78 1
        $resolver->setAllowedTypes('url', 'string');
79 1
        $resolver->setAllowedValues('target', [
80 1
            '_self',
81 1
            '_blank',
82 1
        ]);
83 1
        $resolver->setNormalizer('route', function(Options $options, $value) {
84
85
            // route or url should be defined
86 1
            if ($value === null && $options->offsetGet('url') === null) {
87
                throw new InvalidOptionsException('You must set either an url or a route');
88
            }
89
90 1
            return $value;
91 1
        });
92 1
    }
93
94
    /**
95
     * Define field type.
96
     *
97
     * @return string
98
     */
99
    public function getType()
100
    {
101
        return 'link';
102
    }
103
104
    /**
105
     * Define entity. It will be use to fill parameters with properties values.
106
     *
107
     * @param $entity
108
     */
109 1
    public function setEntity($entity)
110
    {
111 1
        $this->entity = $entity;
112 1
    }
113
}
114