RecordTrait::getSmartProperty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
ccs 2
cts 2
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\Properties\AbstractProperty\Generic as PropertyValue;
6
use ByTIC\Models\SmartProperties\Properties\Definitions\Definition;
7
use ByTIC\Models\SmartProperties\Properties\PropertiesRegistry;
8
use Nip\Records\RecordManager;
9
10
/**
11
 * Class RecordTrait
12
 * @package ByTIC\Models\SmartProperties\RecordsTraits\HasStatus
13
 *
14
 * @property string $status
15
 * @method RecordManager|RecordsTrait getManager()
16
 *
17
 */
18
trait RecordTrait
19
{
20
    use \ByTIC\Models\SmartProperties\RecordsTraits\AbstractTrait\RecordTrait;
21
22
    /**
23
     * @param $name
24
     * @return PropertyValue
25
     */
26
    public function getSmartProperty($name)
27
    {
28
        if ($this->getManager()->hasSmartPropertyDefinition($name) === false) {
29
            return null;
30 9
        }
31
        $definition = $this->getManager()->getSmartPropertyDefinition($name);
32 9
        return PropertiesRegistry::getWithInit($this, $definition, function () use ($definition) {
33
            return $this->getNewSmartPropertyFromDefinition($definition);
34 9
        });
35
    }
36
37
    /**
38
     * @param $name
39
     * @param $value
40 9
     * @return bool
41
     * @throws \Exception
42 9
     */
43 8
    public function updateSmartProperty($name, $value)
44
    {
45 9
        if (empty($value)) {
46
            return false;
47
        }
48
49
        $newStatus = $this->getNewSmartPropertyFromValue($name, $value);
50 8
        $return = $newStatus->update();
51
        $this->setSmartProperty($name, $newStatus);
52 8
        return $return;
53 8
    }
54 8
55 8
    /**
56
     * @param Definition $definition
57 8
     * @return PropertyValue
58
     * @internal Do not use
59
     */
60
    public function getNewSmartPropertyFromDefinition($definition)
61
    {
62
        $name = $definition->getName();
63 8
        $value = $this->getSmartPropertyValueFromDefinition($definition);
64
65 8
        return $this->getNewSmartPropertyFromValue($name, $value);
66 8
    }
67
68 8
    /**
69
     * @param Definition $definition
70
     * @return mixed
71
     * @internal Do not use
72
     */
73
    public function getSmartPropertyValueFromDefinition($definition)
74
    {
75 8
        $field = $definition->getField();
76
        $value = $this->getAttributeFromArray($field);
0 ignored issues
show
Bug introduced by
It seems like getAttributeFromArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-call */ 
77
        $value = $this->getAttributeFromArray($field);
Loading history...
77 8
        if (empty($value)) {
78 8
            $value = $definition->getDefaultValue();
79 8
            $this->setAttribute($field, $value);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

79
            $this->/** @scrutinizer ignore-call */ 
80
                   setAttribute($field, $value);
Loading history...
80 6
        }
81
82
        return $value;
83 8
    }
84
85
    /**
86
     * @param $name
87
     * @param $value
88
     * @return PropertyValue
89
     * @throws \Exception
90
     * @internal Do not use
91
     */
92 8
    public function getNewSmartPropertyFromValue($name, $value)
93
    {
94 8
        $object = clone $this->getManager()->getSmartPropertyItem($name, $value);
95 8
        $object->setItem($this);
0 ignored issues
show
Bug introduced by
The method setItem() does not exist on Nip\Records\Collections\Collection. Did you maybe mean setItems()? ( Ignorable by Annotation )

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

95
        $object->/** @scrutinizer ignore-call */ 
96
                 setItem($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like setItem() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

95
        $object->/** @scrutinizer ignore-call */ 
96
                 setItem($this);
Loading history...
96
97 8
        return $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object also could return the type ByTIC\Models\SmartProper...p\Records\RecordManager which is incompatible with the documented return type ByTIC\Models\SmartProper...bstractProperty\Generic.
Loading history...
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param PropertyValue|string $value
103
     * @throws \Exception
104
     */
105 9
    protected function setSmartProperty($name, $value)
106
    {
107 9
        if (empty($value)) {
108 9
            return;
109 9
        }
110 9
        $definition = $this->getManager()->getSmartPropertyDefinition($name);
111 9
        $field = $definition->getField();
0 ignored issues
show
Bug introduced by
The method getField() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

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

111
        /** @scrutinizer ignore-call */ 
112
        $field = $definition->getField();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like getField() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

111
        /** @scrutinizer ignore-call */ 
112
        $field = $definition->getField();
Loading history...
112 9
113
        if (is_string($value)) {
114 4
            $value = $this->getNewSmartPropertyFromValue($name, $value);
115 4
        }
116 4
117 1
        $this->setPropertyValue($field, $value->getName());
0 ignored issues
show
Bug introduced by
It seems like setPropertyValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

117
        $this->/** @scrutinizer ignore-call */ 
118
               setPropertyValue($field, $value->getName());
Loading history...
118
        
119 4
        $currentProperty = $this->getSmartProperty($name);
120 4
        if ($currentProperty->getName() === $value->getName()) {
121
            return;
122 4
        }
123
        PropertiesRegistry::set($this, $definition, $value);
124
    }
125
}
126