Passed
Push — master ( 177093...90a55c )
by Gabriel
02:35
created

RecordTrait::getNewSmartPropertyFromValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 2
crap 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 Nip\Records\RecordManager;
8
9
/**
10
 * Class RecordTrait
11
 * @package ByTIC\Models\SmartProperties\RecordsTraits\HasStatus
12
 *
13
 * @property string $status
14
 * @method RecordManager|RecordsTrait getManager()
15
 *
16
 */
17
trait RecordTrait
18
{
19
    use \ByTIC\Models\SmartProperties\RecordsTraits\AbstractTrait\RecordTrait;
20
21
    /**
22
     * @var PropertyValue[]
23
     */
24
    protected $smartProperties = [];
25
26
    /**
27
     * @param $name
28
     * @return PropertyValue
29
     */
30 9
    public function getSmartProperty($name)
31
    {
32 9
        $this->checkSmartProperty($name);
33
34 9
        return $this->smartProperties[$name];
35
    }
36
37
    /**
38
     * @param string $name
39
     */
40 9
    public function checkSmartProperty($name)
41
    {
42 9
        if (!isset($this->smartProperties[$name])) {
43 8
            $this->initSmartProperty($name);
44
        }
45 9
    }
46
47
    /**
48
     * @param string $name
49
     */
50 8
    public function initSmartProperty($name)
51
    {
52 8
        if ($this->getManager()->hasSmartPropertyDefinition($name)) {
53 8
            $definition = $this->getManager()->getSmartPropertyDefinition($name);
54 8
            $property = $this->getNewSmartPropertyFromDefinition($definition);
55 8
            $this->setSmartProperty($name, $property);
56
        }
57 8
    }
58
59
    /**
60
     * @param Definition $definition
61
     * @return PropertyValue
62
     */
63 8
    public function getNewSmartPropertyFromDefinition($definition)
64
    {
65 8
        $name = $definition->getName();
66 8
        $value = $this->getSmartPropertyValueFromDefinition($definition);
67
68 8
        return $this->getNewSmartPropertyFromValue($name, $value);
69
    }
70
71
    /**
72
     * @param Definition $definition
73
     * @return mixed
74
     */
75 8
    public function getSmartPropertyValueFromDefinition($definition)
76
    {
77 8
        $field = $definition->getField();
78 8
        $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

78
        /** @scrutinizer ignore-call */ 
79
        $value = $this->getAttributeFromArray($field);
Loading history...
79 8
        if ($value === null) {
80 6
            $value = $definition->getDefaultValue();
81
        }
82
83 8
        return $value;
84
    }
85
86
    /**
87
     * @param $name
88
     * @param $value
89
     * @return PropertyValue
90
     * @throws \Exception
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
        $definition = $this->getManager()->getSmartPropertyDefinition($name);
108 9
        $field = $definition->getField();
0 ignored issues
show
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

108
        /** @scrutinizer ignore-call */ 
109
        $field = $definition->getField();
Loading history...
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

108
        /** @scrutinizer ignore-call */ 
109
        $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...
109 9
        if ($value instanceof PropertyValue) {
110 9
            $this->setDataValue($field, $value->getName());
0 ignored issues
show
Bug introduced by
It seems like setDataValue() 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

110
            $this->/** @scrutinizer ignore-call */ 
111
                   setDataValue($field, $value->getName());
Loading history...
111 9
            $this->smartProperties[$name] = $value;
112 9
            return;
113
        }
114 4
        if (!empty($value)) {
115 4
            $currentProperty = $this->getSmartProperty($name);
116 4
            if (is_object($currentProperty) && $currentProperty->getName() === $value) {
117 3
                return;
118
            }
119 1
            $this->setDataValue($field, $value);
120 1
            $this->smartProperties[$name] = $this->getNewSmartPropertyFromValue($name, $value);
121
        }
122 1
    }
123
124
    /**
125
     * @param $name
126
     * @param $value
127
     * @return bool
128
     * @throws \Exception
129
     */
130 1
    public function updateSmartProperty($name, $value)
131
    {
132 1
        if (!empty($value)) {
133 1
            $newStatus = $this->getNewSmartPropertyFromValue($name, $value);
134 1
            $this->setSmartProperty($name, $newStatus);
135 1
            $return = $newStatus->update();
136 1
            return $return;
137
        }
138
139
        return false;
140
    }
141
}
142