Passed
Push — master ( 9be6d3...56d82e )
by Gabriel
04:12
created

RecordsTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 87.23%

Importance

Changes 0
Metric Value
wmc 18
eloc 34
c 0
b 0
f 0
dl 0
loc 129
rs 10
ccs 41
cts 47
cp 0.8723

12 Methods

Rating   Name   Duplication   Size   Complexity  
A hasSmartPropertyDefinition() 0 6 2
A registerSmartProperties() 0 2 1
A getSmartPropertiesDefinitions() 0 5 1
A addSmartPropertyDefinition() 0 3 1
A getSmartPropertyDefinition() 0 7 2
A initSmartPropertiesDefinitions() 0 4 1
A newSmartPropertyDefinition() 0 6 1
A getSmartPropertyValues() 0 7 2
A checkSmartPropertiesDefinitions() 0 4 2
A getSmartPropertyItem() 0 7 2
A getSmartPropertyItems() 0 7 2
A registerSmartProperty() 0 5 1
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\RecordsTraits\HasSmartProperties;
4
5
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic as PropertyValue;
6
use ByTIC\Models\SmartProperties\Properties\Definitions\Definition;
7
use Exception;
8
9
/**
10
 * Class RecordsTrait
11
 * @package ByTIC\Models\SmartProperties\RecordsTraits\HasStatus
12
 */
13
trait RecordsTrait
14
{
15
    use \ByTIC\Models\SmartProperties\RecordsTraits\AbstractTrait\RecordsTrait;
16
17
    protected $smartPropertiesDefinitions = null;
18
19
    /**
20
     * @return array
21
     */
22 2
    public function getSmartPropertiesDefinitions()
23
    {
24 2
        $this->checkSmartPropertiesDefinitions();
25
26 2
        return $this->smartPropertiesDefinitions;
27
    }
28
29 15
    protected function checkSmartPropertiesDefinitions()
30
    {
31 15
        if ($this->smartPropertiesDefinitions === null) {
32 15
            $this->initSmartPropertiesDefinitions();
33
        }
34 15
    }
35
36 15
    protected function initSmartPropertiesDefinitions()
37
    {
38 15
        $this->smartPropertiesDefinitions = [];
39 15
        $this->registerSmartProperties();
40 15
    }
41
42
    /**
43
     * @param $name
44
     * @return PropertyValue[]|null
45
     * @throws Exception
46
     */
47 2
    public function getSmartPropertyItems($name)
48
    {
49 2
        $definition = $this->getSmartPropertyDefinition($name);
50 2
        if ($definition) {
51 2
            return $definition->getItems();
52
        }
53
        throw new Exception('invalid smart property ['.$name.']');
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return null|Definition
59
     */
60 13
    public function getSmartPropertyDefinition($name)
61
    {
62 13
        if ($this->hasSmartPropertyDefinition($name)) {
63 13
            return $this->smartPropertiesDefinitions[$name];
64
        }
65
66
        return null;
67
    }
68
69
    /**
70
     * @param string $name
71
     * @return bool
72
     */
73 13
    public function hasSmartPropertyDefinition($name)
74
    {
75 13
        $this->checkSmartPropertiesDefinitions();
76
77 13
        return isset($this->smartPropertiesDefinitions[$name])
78 13
        && $this->smartPropertiesDefinitions[$name] instanceof Definition;
79
    }
80
81
    /**
82
     * @param $name
83
     * @param $field
84
     * @return \ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic[]|null
85
     * @throws Exception
86
     */
87 2
    public function getSmartPropertyValues($name, $field)
88
    {
89 2
        $definition = $this->getSmartPropertyDefinition($name);
90 2
        if ($definition) {
91 2
            return $definition->getValues($field);
92
        }
93
        throw new Exception('invalid smart property ['.$name.']');
94
    }
95
96
    /**
97
     * @param string $property
98
     * @param string $value
99
     * @return PropertyValue
100
     * @throws Exception
101
     */
102 7
    public function getSmartPropertyItem($property, $value)
103
    {
104 7
        $definition = $this->getSmartPropertyDefinition($property);
105 7
        if ($definition) {
106 7
            return $definition->getItem($value);
107
        }
108
        throw new Exception('invalid smart property ['.$property.']');
109
    }
110
111
    protected function registerSmartProperties()
112
    {
113
    }
114
115
    /**
116
     * @param $field
117
     */
118 15
    protected function registerSmartProperty($field)
119
    {
120 15
        $definition = $this->newSmartPropertyDefinition();
121 15
        $definition->setField($field);
122 15
        $this->addSmartPropertyDefinition($definition);
123 15
    }
124
125
    /**
126
     * @return Definition
127
     */
128 15
    protected function newSmartPropertyDefinition()
129
    {
130 15
        $definition = new Definition();
131 15
        $definition->setManager($this);
132
133 15
        return $definition;
134
    }
135
136
    /**
137
     * @param Definition $definition
138
     */
139 15
    protected function addSmartPropertyDefinition($definition)
140
    {
141 15
        $this->smartPropertiesDefinitions[$definition->getName()] = $definition;
142 15
    }
143
}
144