Passed
Push — master ( 9e5266...b2c7e7 )
by Gabriel
03:23
created

MediaProperty   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 41
c 2
b 0
f 1
dl 0
loc 134
ccs 0
cts 45
cp 0
rs 10
wmc 20

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultMedia() 0 4 1
A dbLoaded() 0 8 2
A setCustomPropertiesAttribute() 0 9 2
A getCustomProperty() 0 4 2
A initCustomProperties() 0 5 2
A populateFromCollection() 0 4 2
A setCustomPropery() 0 5 1
A populateFromModel() 0 4 1
A getCustomProperties() 0 4 1
A defaultMedia() 0 13 3
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 ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
7
use Nip\Records\Record;
8
9
/**
10
 * Class MediaProperty
11
 * @package ByTIC\MediaLibrary\Models\MediaRecords
12
 *
13
 * @property string $model
14
 * @property int $model_id
15
 * @property string $collection_name
16
 * @property string $custom_properties
17
 *
18
 * @method HasMediaTrait getModel
19
 */
20
class MediaProperty extends Record
21
{
22
    /**
23
     * @var null|array
24
     */
25
    protected $customPropertiesArray = null;
26
27
    /**
28
     * @param Record $model
29
     */
30
    public function populateFromModel(Record $model)
31
    {
32
        $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 Nip\Records\Traits\Relat...asRelationsRecordsTrait 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...
Bug introduced by
It seems like getMorphName() 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

32
        $this->model = $model->getManager()->/** @scrutinizer ignore-call */ getMorphName();
Loading history...
33
        $this->model_id = $model->getPrimaryKey();
34
    }
35
36
    /**
37
     * @param Collection|string $collection
38
     */
39
    public function populateFromCollection($collection)
40
    {
41
        $name = is_object($collection) ? $collection->getName() : $collection;
42
        $this->collection_name = $name;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getCustomProperties()
49
    {
50
        $this->checkCustomProperties();
51
        return $this->customPropertiesArray;
52
    }
53
54
    protected function checkCustomProperties()
55
    {
56
        if ($this->customPropertiesArray == null) {
57
            $this->initCustomProperties();
58
        }
59
    }
60
61
62
    public function initCustomProperties()
63
    {
64
        $properties = json_decode($this->custom_properties, true);
65
        $properties = (is_array($properties)) ? $properties : [];
66
        $this->customPropertiesArray = $properties;
67
    }
68
69
    /**
70
     * @param $name
71
     * @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...
72
     * @return mixed
73
     */
74
    public function getCustomProperty($name, $default = null)
75
    {
76
        $this->checkCustomProperties();
77
        return isset($this->customPropertiesArray[$name]) ? $this->customPropertiesArray[$name] : $default;
78
    }
79
80
    /**
81
     * @param $name
82
     * @param $value
83
     */
84
    public function setCustomPropery($name, $value)
85
    {
86
        $this->checkCustomProperties();
87
        $this->customPropertiesArray[$name] = $value;
88
        $this->setCustomPropertiesAttribute($this->getCustomProperties());
89
    }
90
91
    /**
92
     * @param bool $value
93
     */
94
    public function saveDbLoaded($value)
95
    {
96
        $this->dbLoaded($value);
97
        $this->save();
98
    }
99
100
    /**
101
     * @param bool|null $value
102
     * @return bool
103
     */
104
    public function dbLoaded($value = null)
105
    {
106
        if ($value == null) {
107
            return $this->getCustomProperty('dbLoaded', false);
108
        }
109
        $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...
110
        $this->setCustomPropery('dbLoaded', $value);
111
        return $value;
112
    }
113
114
    /**
115
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
116
     * @return mixed|null
117
     */
118
    public function defaultMedia($value = null)
119
    {
120
        if ($value == null) {
0 ignored issues
show
introduced by
The condition $value == null is always true.
Loading history...
121
            return $this->getCustomProperty('defaultMedia', $this->getModel()->default_image);
122
        }
123
        $this->setCustomPropery('defaultMedia', $value);
124
        $this->save();
125
126
        if ($this->collection_name == 'images') {
127
            $this->getModel()->default_image = $value;
128
            $this->getModel()->update();
129
        }
130
        return $value;
131
    }
132
133
    /**
134
     * @param $value
135
     */
136
    public function setDefaultMedia($value)
137
    {
138
        $this->dbLoaded($value);
139
        $this->save();
140
    }
141
142
    /**
143
     * @param $value
144
     */
145
    public function setCustomPropertiesAttribute($value)
146
    {
147
        if (is_string($value)) {
148
            $this->customPropertiesArray = null;
149
        } else {
150
            $this->customPropertiesArray = $value;
151
            $value = json_encode($value);
152
        }
153
        $this->setDataValue('custom_properties', $value);
154
    }
155
}
156