Passed
Push — master ( f112c9...a480d7 )
by Gabriel
02:57
created

Generic::preValueChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
rs 10
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Properties\AbstractProperty;
4
5
use Nip\Records\Record as Record;
6
7
/**
8
 * Class Generic
9
 * @package ByTIC\Models\SmartProperties\Properties\AbstractProperty
10
 */
11
abstract class Generic
12
{
13
    use Traits\HasItemTrait;
14
    use Traits\HasManagerTrait;
15
    use Traits\HasNameTrait;
16
17
    protected $label = null;
18
19
    protected $label_short = null;
20
21
22
    /**
23
     * @var null|string
24
     */
25
    protected $field;
26
27
    /**
28
     * @param $name
29
     * @return null
30
     */
31
    public function __get($name)
32
    {
33
        $method = 'get' . ucfirst($name);
34
        if (method_exists($this, $method)) {
35
            return $this->$method();
36
        }
37
38
        return null;
39
    }
40
41
    /**
42
     * @param bool $short
43
     * @return string
44
     */
45
    public function getLabelHTML($short = false)
46
    {
47
        return '<span class="' . $this->getLabelClasses() . '" rel="tooltip" title="' . $this->getLabel() . '"  
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getLabel() targeting ByTIC\Models\SmartProper...rty\Generic::getLabel() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
        style="' . $this->getColorCSS() . '">
49
            ' . $this->getIconHTML() . '
50
            ' . $this->getLabel($short) . '
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getLabel($short) targeting ByTIC\Models\SmartProper...rty\Generic::getLabel() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
51
        </span>';
52
    }
53
54
    /**
55
     * get the label class name
56
     *
57
     * @return string
58
     */
59
    public function getLabelClasses()
60
    {
61
        return 'label label-' . $this->getColorClass();
62
    }
63
64
    /**
65
     * Get the color class
66
     *
67
     * @return string
68
     */
69
    public function getColorClass()
70
    {
71
        return 'default';
72
    }
73
74
    /**
75
     * Get Property label
76
     *
77
     * @param bool $short short flag
78
     *
79
     * @return null
80
     */
81
    public function getLabel($short = false)
82
    {
83
        if (!$this->label) {
84
            $this->label = $this->generateLabel();
85
            if ($this->hasShortLabel()) {
86
                $this->label_short = $this->generateLabelShort();
87
            }
88
        }
89
90
        return $short ? $this->label_short : $this->label;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    protected function generateLabel()
97
    {
98
        return $this->getManager()->translate($this->getLabelSlug() . '.' . $this->getName());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getManager...'.' . $this->getName()) returns the type Nip\Records\Collections\...ords\RecordManager|true which is incompatible with the documented return type string.
Loading history...
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    abstract protected function getLabelSlug();
105
106
    /**
107
     * @return boolean
108
     */
109
    protected function hasShortLabel()
110
    {
111
        return true;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    protected function generateLabelShort()
118
    {
119
        return $this->getManager()->translate($this->getLabelSlug() . '.' . $this->getName() . '.short');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getManager...->getName() . '.short') returns the type Nip\Records\Collections\...ords\RecordManager|true which is incompatible with the documented return type string.
Loading history...
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getColorCSS()
126
    {
127
        $css = [];
128
        if ($this->getBGColor()) {
129
            $css[] = 'background-color: ' . $this->getBGColor();
130
        }
131
        if ($this->getFGColor()) {
132
            $css[] = 'color: ' . $this->getFGColor();
133
        }
134
135
        return implode(';', $css);
136
    }
137
138
    /**
139
     * @return bool|string
140
     */
141
    public function getBGColor()
142
    {
143
        return false;
144
    }
145
146
    /**
147
     * @return bool|string
148
     */
149
    public function getFGColor()
150
    {
151
        return false;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getIconHTML()
158
    {
159
        $icon = $this->getIcon();
160
        $return = '';
161
        if ($icon) {
0 ignored issues
show
introduced by
The condition $icon is always false.
Loading history...
162
            $return .= '<span class="glyphicon glyphicon-white ' . $icon . '"></span> ';
163
        }
164
165
        return $return;
166
    }
167
168
    /**
169
     * @return bool|string
170
     */
171
    public function getIcon()
172
    {
173
        return false;
174
    }
175
176
    /**
177
     * @return bool|mixed
178
     */
179
    public function update()
180
    {
181
        $item = $this->getItem();
182
        if ($item) {
183
            $this->preValueChange();
184
            /** @noinspection PhpUndefinedFieldInspection */
185 1
            $item->{$this->getField()} = $this->getName();
186
            $this->preUpdate();
187 1
            $return = $item->saveRecord();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $return is correct as $item->saveRecord() targeting Nip\Records\AbstractModels\Record::saveRecord() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
188 1
            $this->postUpdate();
189 1
190
            return $return;
191 1
        }
192 1
193 1
        return false;
194 1
    }
195
196 1
    public function preValueChange()
197
    {
198
    }
199
200
    /**
201
     * @return null|string
202
     */
203
    public function getField()
204
    {
205 1
        return $this->field;
206
    }
207 1
208
    /**
209
     * @param null|string $field
210
     */
211
    public function setField($field)
212
    {
213
        $this->field = $field;
214 8
    }
215
216 8
    public function preUpdate()
217
    {
218 8
    }
219
220
    public function postUpdate()
221 1
    {
222
    }
223 1
224
    /**
225
     * @return string
226
     */
227
    public function getMessageType()
228 1
    {
229
        return 'info';
230 1
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function __toString(): string
236 14
    {
237
        return $this->getName();
238 14
    }
239
}
240