Completed
Pull Request — master (#90)
by Arnaud
02:00
created

ActionCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 87
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setEntity() 0 4 1
A getConfigurationClass() 0 4 1
A render() 0 23 3
A getType() 0 4 1
A setTwig() 0 4 1
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\AbstractField;
6
use LAG\AdminBundle\Field\Configuration\ActionCollectionConfiguration;
7
use LAG\AdminBundle\Field\EntityAwareInterface;
8
use LAG\AdminBundle\Field\TwigAwareInterface;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
use Twig_Environment;
11
12
class ActionCollection extends AbstractField implements TwigAwareInterface, EntityAwareInterface
13
{
14
    /**
15
     * @var Twig_Environment
16
     */
17
    protected $twig;
18
    
19
    /**
20
     * @var mixed
21
     */
22
    protected $entity;
23
    
24
    /**
25
     * Render value of the field.
26
     *
27
     * @param mixed $value Value to render
28
     *
29
     * @return mixed
30
     */
31
    public function render($value)
32
    {
33
        $actions = $this->options['actions'];
34
        $normalizedActions = [];
35
        
36
        foreach ($actions as $action => $options) {
37
            $accessor = PropertyAccess::createPropertyAccessor();
38
            $normalizedParameters = [];
39
    
40
            foreach ($options['parameters'] as $parameter => $parameterOptions) {
41
                $normalizedParameters[$parameter] = $accessor->getValue($this->entity, $parameter);
42
            }
43
            $options['parameters'] = $normalizedParameters;
44
            $normalizedActions[$action] = $options;
45
        }
46
        
47
        return $this
48
            ->twig
49
            ->render('@LAGAdmin/Field/actionCollection.html.twig', [
50
                'actions' => $normalizedActions,
51
            ])
52
        ;
53
    }
54
    
55
    /**
56
     * Return field type.
57
     *
58
     * @return string
59
     */
60
    public function getType()
61
    {
62
        return AbstractField::TYPE_ACTION_COLLECTION;
63
    }
64
    
65
    /**
66
     * Defines entity for field.
67
     *
68
     * @param $entity
69
     *
70
     * @return void
71
     */
72
    public function setEntity($entity)
73
    {
74
        $this->entity = $entity;
75
    }
76
    
77
    /**
78
     * Define twig environment.
79
     *
80
     * @param Twig_Environment $twig
81
     *
82
     * @return void
83
     */
84
    public function setTwig(Twig_Environment $twig)
85
    {
86
        $this->twig = $twig;
87
    }
88
    
89
    /**
90
     * Return the Field's configuration class.
91
     *
92
     * @return string
93
     */
94
    public function getConfigurationClass()
95
    {
96
        return ActionCollectionConfiguration::class;
97
    }
98
}
99