Issues (112)

src/Models/MediaProperties/MediaProperty.php (10 issues)

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|Record 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...
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->getAttributeFromArray('custom_properties'), true);
0 ignored issues
show
It seems like $this->getAttributeFromArray('custom_properties') can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        $properties = json_decode(/** @scrutinizer ignore-type */ $this->getAttributeFromArray('custom_properties'), true);
Loading history...
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
The condition $value == null is always true.
Loading history...
121
            $model = $this->getModel();
122
            $default = $model ? $model->getAttribute('default_image') : null;
0 ignored issues
show
It seems like getAttribute() 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

122
            $default = $model ? $model->/** @scrutinizer ignore-call */ getAttribute('default_image') : null;
Loading history...
123
            return $this->getCustomProperty('defaultMedia', $default);
124
        }
125
126
        $this->setCustomPropery('defaultMedia', $value);
127
        return $value;
128
    }
129
130
    /**
131
     * @param $value
132
     */
133
    public function setDefaultMedia($value)
134
    {
135
        $this->defaultMedia($value);
136
        $this->save();
137
138
        $model = $this->getModel();
139
        if ($this->collection_name == 'images') {
140
            $model->setAttribute('default_image', $value);
0 ignored issues
show
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

140
            $model->/** @scrutinizer ignore-call */ 
141
                    setAttribute('default_image', $value);
Loading history...
141
            $model->update();
0 ignored issues
show
It seems like update() 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

141
            $model->/** @scrutinizer ignore-call */ 
142
                    update();
Loading history...
142
        }
143
    }
144
145
    /**
146
     * @param $value
147
     */
148
    public function setCustomPropertiesAttribute($value)
149
    {
150
        if (is_string($value)) {
151
            $this->customPropertiesArray = null;
152
        } else {
153
            $this->customPropertiesArray = $value;
154
            $value = json_encode($value);
155
        }
156
        $this->setPropertyValue('custom_properties', $value);
157
    }
158
}
159