Test Failed
Push — master ( c9f7be...859573 )
by Gabriel
06:41
created

MediaProperty::checkCustomProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace ByTIC\MediaLibrary\Models\MediaProperties;
4
5
use Nip\Records\AbstractModels\Record;
6
7
/**
8
 * Class MediaProperty
9
 * @package ByTIC\MediaLibrary\Models\MediaRecords
10
 *
11
 * @property string $model
12
 * @property int $model_id
13
 * @property string $collection_name
14
 * @property string $custom_properties
15
 */
16
class MediaProperty extends Record
17
{
18
    /**
19
     * @var null|array
20
     */
21
    protected $customPropertiesArray = null;
22
23
    /**
24
     * @param Record $model
25
     */
26
    public function populateFromModel(Record $model)
27
    {
28
        $this->model = $model->getManager()->getMorphName();
0 ignored issues
show
Documentation Bug introduced by
It seems like $model->getManager()->getMorphName() of type Nip\Records\AbstractModels\RecordManager or Nip\Records\Collections\Collection or true is incompatible with the declared type string of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        $this->model_id = $model->getPrimaryKey();
30
    }
31
32
    /**
33
     * @param $collection
34
     */
35
    public function populateFromCollection($collection)
36
    {
37
        $name = is_object($collection) ? $collection->getName() : $collection;
38
        $this->collection_name = $name;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getCustomProperties()
45
    {
46
        $this->checkCustomProperties();
47
        return $this->customPropertiesArray;
48
    }
49
50
    protected function checkCustomProperties()
51
    {
52
        if ($this->customPropertiesArray == null) {
53
            $this->initCustomProperties();
54
        }
55
    }
56
57
58
    public function initCustomProperties()
59
    {
60
        $properties = json_decode($this->custom_properties, true);
61
        $properties = (is_array($properties)) ? $properties : [];
62
        $this->customPropertiesArray = $properties;
63
    }
64
65
    /**
66
     * @param $name
67
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
68
     * @return mixed
69
     */
70
    public function getCustomProperty($name, $default = null)
71
    {
72
        $this->checkCustomProperties();
73
        return isset($this->customPropertiesArray[$name]) ? $this->customPropertiesArray[$name] : $default;
74
    }
75
76
    /**
77
     * @param $name
78
     * @param $value
79
     */
80
    public function setCustomPropery($name, $value)
81
    {
82
        $this->checkCustomProperties();
83
        $this->customPropertiesArray[$name] = $value;
84
        $this->setCustomPropertiesAttribute($this->getCustomProperties());
85
    }
86
87
    /**
88
     * @param bool|null $value
89
     * @return bool
90
     */
91
    public function dbLoaded($value = null)
92
    {
93
        if ($value == null) {
94
            return $this->getCustomProperty('dbLoaded', false);
95
        }
96
        $value = ($value == true);
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
97
        $this->setCustomPropery('dbLoaded', $value);
98
        return $value;
99
    }
100
101
    /**
102
     * @param $value
103
     */
104
    public function setCustomPropertiesAttribute($value)
105
    {
106
        if (is_string($value)) {
107
            $this->customPropertiesArray = null;
108
        } else {
109
            $this->customPropertiesArray = $value;
110
            $value = json_encode($value);
111
        }
112
        $this->setDataValue('custom_properties', $value);
113
    }
114
}
115