Passed
Push — master ( c4a808...c45372 )
by Gabriel
15:08
created

RecordsTrait::getSmartPropertyItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\RecordsTraits\HasSmartProperties;
4
5
use ByTIC\Models\SmartProperties\Definitions\Builders\FolderBuilders;
6
use ByTIC\Models\SmartProperties\Definitions\DefinitionRegistry;
7
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic as PropertyValue;
8
use ByTIC\Models\SmartProperties\Properties\Definitions\Definition;
9
use Exception;
10
11
/**
12
 * Class RecordsTrait
13
 * @package ByTIC\Models\SmartProperties\RecordsTraits\HasStatus
14
 */
15
trait RecordsTrait
16
{
17
    use \ByTIC\Models\SmartProperties\RecordsTraits\AbstractTrait\RecordsTrait;
18
19
    /**
20
     * @return array
21
     */
22 2
    public function getSmartPropertiesDefinitions()
23
    {
24 2
        $this->checkSmartPropertiesDefinitions();
25
26 2
        return $this->getSmartPropertyDefinitionRegistry()->getAll($this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getSmartPr...gistry()->getAll($this) also could return the type ByTIC\Models\SmartProper...\Definitions\Definition which is incompatible with the documented return type array.
Loading history...
27
    }
28
29 18
    /**
30
     * @param $name
31 18
     * @return PropertyValue[]|null
32 18
     * @throws Exception
33
     */
34 18
    public function getSmartPropertyItems($name)
35
    {
36 18
        $definition = $this->getSmartPropertyDefinition($name);
37
        if ($definition) {
38 18
            return $definition->getItems();
39 18
        }
40 18
        throw new Exception('invalid smart property [' . $name . ']');
41
    }
42
43
    /**
44
     * @param string $name
45
     * @return null|Definition
46
     */
47 2
    public function getSmartPropertyDefinition($name)
48
    {
49 2
        if (!$this->hasSmartPropertyDefinition($name)) {
50 2
            return null;
51 2
        }
52
53
        return $this->getSmartPropertyDefinitionRegistry()->get($this, $name);
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return bool
59
     */
60 16
    public function hasSmartPropertyDefinition($name)
61
    {
62 16
        $this->checkSmartPropertiesDefinitions();
63 16
64
        return $this->getSmartPropertyDefinitionRegistry()->has($this, $name);
65
    }
66
67
    /**
68
     * @param $name
69
     * @param $field
70
     * @return \ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic[]|null
71
     * @throws Exception
72
     */
73 16
    public function getSmartPropertyValues($name, $field)
74
    {
75 16
        $definition = $this->getSmartPropertyDefinition($name);
76
        if ($definition) {
77 16
            return $definition->getValues($field);
78 16
        }
79
        throw new Exception('invalid smart property [' . $name . ']');
80
    }
81
82
    /**
83
     * @param string $property
84
     * @param string $value
85
     * @return PropertyValue
86
     * @throws Exception
87 2
     */
88
    public function getSmartPropertyItem($property, $value)
89 2
    {
90 2
        $definition = $this->getSmartPropertyDefinition($property);
91 2
        if ($definition) {
92
            return $definition->getItem($value);
93
        }
94
        throw new Exception('invalid smart property [' . $property . ']');
95
    }
96
97
    protected function checkSmartPropertiesDefinitions()
98
    {
99
        $this->getSmartPropertyDefinitionRegistry()->checkDefinitionToBuild($this, function () {
100
            $this->registerSmartProperties();
101
        });
102 10
    }
103
104 10
    abstract protected function registerSmartProperties();
105 10
106 10
    /**
107
     * @param $field
108
     */
109
    protected function registerSmartProperty($field, $name = null)
110
    {
111
        $definition = FolderBuilders::create($this, $field, $name);
0 ignored issues
show
Bug introduced by
$this of type ByTIC\Models\SmartProper...Properties\RecordsTrait is incompatible with the type Nip\Records\AbstractModels\RecordManager expected by parameter $manager of ByTIC\Models\SmartProper...olderBuilders::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        $definition = FolderBuilders::create(/** @scrutinizer ignore-type */ $this, $field, $name);
Loading history...
112
        $this->addSmartPropertyDefinition($definition);
113
    }
114
115
116
    /**
117
     * @param Definition $definition
118 18
     */
119
    protected function addSmartPropertyDefinition($definition)
120 18
    {
121 18
        $this->getSmartPropertyDefinitionRegistry()->set($this, $definition);
122 18
    }
123 18
124
    /**
125
     * @return DefinitionRegistry
126
     */
127
    protected function getSmartPropertyDefinitionRegistry(): DefinitionRegistry
128 18
    {
129
        return DefinitionRegistry::instance();
130 18
    }
131
}
132