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

Link::render()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.1967

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 13
cp 0.7692
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 6
nop 1
crap 4.1967
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\Configuration\LinkConfiguration;
6
use LAG\AdminBundle\Field\EntityAwareInterface;
7
use LAG\AdminBundle\Field\TwigAwareInterface;
8
use Symfony\Component\PropertyAccess\PropertyAccess;
9
10
class Link extends StringField implements EntityAwareInterface, TwigAwareInterface
11
{
12
    /**
13
     * @var Object
14
     */
15
    protected $entity;
16
17
    /**
18
     * Render link template filled with configured options.
19
     *
20
     * @param mixed $value
21
     * @return string
22
     */
23 2
    public function render($value)
24
    {
25 2
        $text = $this->options['text'] ?: parent::render($value);
26 2
        $parameters = [];
27 2
        $accessor = PropertyAccess::createPropertyAccessor();
28
29 2
        foreach ($this->options['parameters'] as $parameterName => $fieldName) {
30
            if (!$fieldName) {
31
                $fieldName = $parameterName;
32
            }
33
            $parameters[$parameterName] = $accessor->getValue($this->entity, $fieldName);
34
        }
35
36 2
        $render = $this->twig->render($this->options['template'], [
37 2
            'text' => $text,
38 2
            'parameters' => $parameters,
39 2
            'options' => $this->options,
40
        ]);
41
42 2
        return $render;
43
    }
44
45
    /**
46
     * Define field type.
47
     *
48
     * @return string
49
     */
50
    public function getType()
51
    {
52
        return 'link';
53
    }
54
55
    /**
56
     * Define entity. It will be use to fill parameters with properties values.
57
     *
58
     * @param $entity
59
     */
60
    public function setEntity($entity)
61
    {
62
        $this->entity = $entity;
63
    }
64
    
65
    /**
66
     * Return the Field's configuration class.
67
     *
68
     * @return string
69
     */
70
    public function getConfigurationClass()
71
    {
72
        return LinkConfiguration::class;
73
    }
74
}
75