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

AbstractField   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 100
ccs 9
cts 18
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A getConfiguration() 0 11 2
A setActionConfiguration() 0 4 1
A setConfiguration() 0 12 3
1
<?php
2
3
namespace LAG\AdminBundle\Field;
4
5
use Exception;
6
use JK\Configuration\Configuration;
7
use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
8
9
abstract class AbstractField implements FieldInterface
10
{
11
    const TYPE_STRING = 'string';
12
    const TYPE_LINK = 'link';
13
    const TYPE_ARRAY = 'array';
14
    const TYPE_DATE = 'date';
15
    const TYPE_COUNT = 'count';
16
    const TYPE_ACTION = 'action';
17
    const TYPE_COLLECTION = 'collection';
18
    const TYPE_BOOLEAN = 'boolean';
19
    const TYPE_MAPPED = 'mapped';
20
    const TYPE_ACTION_COLLECTION = 'action_collection';
21
    
22
    /**
23
     * Field's name
24
     *
25
     * @var string
26
     */
27
    protected $name;
28
    
29
    /**
30
     * @var Configuration
31
     */
32
    protected $configuration;
33
    
34
    /**
35
     * @var ActionConfiguration
36
     */
37
    protected $actionConfiguration;
38
    
39
    /**
40
     * @var array
41
     */
42
    protected $options = [];
43
    
44
    /**
45
     * Field constructor.
46 4
     *
47
     * @param string $name
48 4
     */
49 4
    public function __construct($name)
50
    {
51
        $this->name = $name;
52
    }
53
    
54
    /**
55
     * @return string
56
     */
57
    public function getName()
58
    {
59
        return $this->name;
60
    }
61
    
62
    /**
63
     * Return the value of a configuration parameter.
64
     *
65
     * @param string    $name
66
     *
67
     * @param mixed|null $default
68
     *
69
     * @return mixed
70
     */
71
    public function getConfiguration($name, $default = null)
72
    {
73
        if (!$this->configuration->hasParameter($name)) {
74
            return $default;
75
        }
76
        
77
        return $this
78
            ->configuration
79
            ->getParameter($name)
80 3
        ;
81
    }
82 3
    
83 3
    /**
84
     * @param ActionConfiguration $actionConfiguration
85
     */
86
    public function setActionConfiguration(ActionConfiguration $actionConfiguration)
87
    {
88
        $this->actionConfiguration = $actionConfiguration;
89
    }
90
    
91 2
    /**
92
     * @param Configuration $configuration
93 2
     *
94 2
     * @throws Exception
95
     */
96
    public function setConfiguration(Configuration $configuration)
97
    {
98
        if (!$configuration->isResolved()) {
99
            throw new Exception('The configuration must be resolved');
100
        }
101
102
        if (null !== $this->configuration) {
103
            throw new Exception('The configuration has already been defined');
104
        }
105
        $this->configuration = $configuration;
106
        $this->options = $configuration->getParameters();
107
    }
108
}
109