Passed
Push — master ( 859573...a81ea2 )
by Gabriel
03:21
created

MediaProperty   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 31
c 1
b 0
f 1
dl 0
loc 106
ccs 0
cts 44
cp 0
rs 10
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A dbLoaded() 0 8 2
A setCustomPropertiesAttribute() 0 9 2
A initCustomProperties() 0 5 2
A populateFromCollection() 0 4 2
A getCustomProperty() 0 4 2
A setCustomPropery() 0 5 1
A populateFromModel() 0 4 1
A getCustomProperties() 0 4 1
A saveDbLoaded() 0 4 1
A checkCustomProperties() 0 4 2
1
<?php
2
3
namespace ByTIC\MediaLibrary\Models\MediaProperties;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use Nip\Records\AbstractModels\Record;
7
8
/**
9
 * Class MediaProperty
10
 * @package ByTIC\MediaLibrary\Models\MediaRecords
11
 *
12
 * @property string $model
13
 * @property int $model_id
14
 * @property string $collection_name
15
 * @property string $custom_properties
16
 */
17
class MediaProperty extends Record
18
{
19
    /**
20
     * @var null|array
21
     */
22
    protected $customPropertiesArray = null;
23
24
    /**
25
     * @param Record $model
26
     */
27
    public function populateFromModel(Record $model)
28
    {
29
        $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...
30
        $this->model_id = $model->getPrimaryKey();
31
    }
32
33
    /**
34
     * @param Collection|string $collection
35
     */
36
    public function populateFromCollection($collection)
37
    {
38
        $name = is_object($collection) ? $collection->getName() : $collection;
39
        $this->collection_name = $name;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getCustomProperties()
46
    {
47
        $this->checkCustomProperties();
48
        return $this->customPropertiesArray;
49
    }
50
51
    protected function checkCustomProperties()
52
    {
53
        if ($this->customPropertiesArray == null) {
54
            $this->initCustomProperties();
55
        }
56
    }
57
58
59
    public function initCustomProperties()
60
    {
61
        $properties = json_decode($this->custom_properties, true);
62
        $properties = (is_array($properties)) ? $properties : [];
63
        $this->customPropertiesArray = $properties;
64
    }
65
66
    /**
67
     * @param $name
68
     * @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...
69
     * @return mixed
70
     */
71
    public function getCustomProperty($name, $default = null)
72
    {
73
        $this->checkCustomProperties();
74
        return isset($this->customPropertiesArray[$name]) ? $this->customPropertiesArray[$name] : $default;
75
    }
76
77
    /**
78
     * @param $name
79
     * @param $value
80
     */
81
    public function setCustomPropery($name, $value)
82
    {
83
        $this->checkCustomProperties();
84
        $this->customPropertiesArray[$name] = $value;
85
        $this->setCustomPropertiesAttribute($this->getCustomProperties());
86
    }
87
88
    /**
89
     * @param bool $value
90
     */
91
    public function saveDbLoaded($value)
92
    {
93
        $this->dbLoaded($value);
94
        $this->save();
95
    }
96
97
    /**
98
     * @param bool|null $value
99
     * @return bool
100
     */
101
    public function dbLoaded($value = null)
102
    {
103
        if ($value == null) {
104
            return $this->getCustomProperty('dbLoaded', false);
105
        }
106
        $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...
107
        $this->setCustomPropery('dbLoaded', $value);
108
        return $value;
109
    }
110
111
    /**
112
     * @param $value
113
     */
114
    public function setCustomPropertiesAttribute($value)
115
    {
116
        if (is_string($value)) {
117
            $this->customPropertiesArray = null;
118
        } else {
119
            $this->customPropertiesArray = $value;
120
            $value = json_encode($value);
121
        }
122
        $this->setDataValue('custom_properties', $value);
123
    }
124
}
125