Passed
Push — master ( 416dc9...0e85a7 )
by Gabriel
03:04
created

Generic::getBGColor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Properties\AbstractProperty;
4
5
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Traits\HasManagerTrait;
6
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Traits\HasNameTrait;
7
use Nip\Records\Record as Record;
8
use ReflectionClass;
9
10
/**
11
 * Class Generic
12
 * @package ByTIC\Models\SmartProperties\Properties\AbstractProperty
13
 */
14
abstract class Generic
15
{
16
    use HasManagerTrait;
17
    use HasNameTrait;
18
19
    protected $label = null;
20
21
    protected $label_short = null;
22
23
    /**
24
     * @var null|Record
25
     */
26
    protected $item;
27
28
    /**
29
     * @var null|string
30
     */
31
    protected $field;
32
33
    /**
34
     * @param $name
35
     * @return null
36
     */
37
    public function __get($name)
38
    {
39
        $method = 'get' . ucfirst($name);
40
        if (method_exists($this, $method)) {
41
            return $this->$method();
42
        }
43
44
        return null;
45
    }
46
47
    /**
48
     * @param bool $short
49
     * @return string
50
     */
51
    public function getLabelHTML($short = false)
52
    {
53
        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...
54
        style="' . $this->getColorCSS() . '">
55
            ' . $this->getIconHTML() . '
56
            ' . $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...
57
        </span>';
58
    }
59
60
    /**
61
     * get the label class name
62
     *
63
     * @return string
64
     */
65
    public function getLabelClasses()
66
    {
67
        return 'label label-' . $this->getColorClass();
68
    }
69
70
    /**
71
     * Get the color class
72
     *
73
     * @return string
74
     */
75
    public function getColorClass()
76
    {
77
        return 'default';
78
    }
79
80
    /**
81
     * Get Property label
82
     *
83
     * @param bool $short short flag
84
     *
85
     * @return null
86
     */
87
    public function getLabel($short = false)
88
    {
89
        if (!$this->label) {
90
            $this->label = $this->generateLabel();
91
            if ($this->hasShortLabel()) {
92
                $this->label_short = $this->generateLabelShort();
93
            }
94
        }
95
96
        return $short ? $this->label_short : $this->label;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    protected function generateLabel()
103
    {
104
        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...
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    abstract protected function getLabelSlug();
111
112
    /**
113
     * @return boolean
114
     */
115
    protected function hasShortLabel()
116
    {
117
        return true;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    protected function generateLabelShort()
124
    {
125
        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...
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getColorCSS()
132
    {
133
        $css = [];
134
        if ($this->getBGColor()) {
135
            $css[] = 'background-color: ' . $this->getBGColor();
136
        }
137
        if ($this->getFGColor()) {
138
            $css[] = 'color: ' . $this->getFGColor();
139
        }
140
141
        return implode(';', $css);
142
    }
143
144
    /**
145
     * @return bool|string
146
     */
147
    public function getBGColor()
148
    {
149
        return false;
150
    }
151
152
    /**
153
     * @return bool|string
154
     */
155
    public function getFGColor()
156
    {
157
        return false;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getIconHTML()
164
    {
165
        $icon = $this->getIcon();
166
        $return = '';
167
        if ($icon) {
0 ignored issues
show
introduced by
The condition $icon is always false.
Loading history...
168
            $return .= '<span class="glyphicon glyphicon-white ' . $icon . '"></span> ';
169
        }
170
171
        return $return;
172
    }
173
174
    /**
175
     * @return bool|string
176
     */
177
    public function getIcon()
178
    {
179
        return false;
180
    }
181
182
    /**
183
     * @return bool|mixed
184
     */
185 1
    public function update()
186
    {
187 1
        $item = $this->getItem();
188 1
        if ($item) {
189 1
            $this->preValueChange();
190
            /** @noinspection PhpUndefinedFieldInspection */
191 1
            $item->{$this->getField()} = $this->getName();
192 1
            $this->preUpdate();
193 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...
194 1
            $this->postUpdate();
195
196 1
            return $return;
197
        }
198
199
        return false;
200
    }
201
202
    /**
203
     * @return Record|null
204
     */
205 1
    public function getItem()
206
    {
207 1
        return $this->item;
208
    }
209
210
    /**
211
     * @param $i
212
     * @return $this
213
     */
214 8
    public function setItem($i)
215
    {
216 8
        $this->item = $i;
217
218 8
        return $this;
219
    }
220
221 1
    public function preValueChange()
222
    {
223 1
    }
224
225
    /**
226
     * @return null|string
227
     */
228 1
    public function getField()
229
    {
230 1
        return $this->field;
231
    }
232
233
    /**
234
     * @param null|string $field
235
     */
236 14
    public function setField($field)
237
    {
238 14
        $this->field = $field;
239 14
    }
240
241 1
    public function preUpdate()
242
    {
243 1
    }
244
245 1
    public function postUpdate()
246
    {
247 1
    }
248
249
    /**
250
     * @return string
251
     */
252
    public function getMessageType()
253
    {
254
        return 'info';
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    public function __toString(): string
261
    {
262
        return $this->getName();
263
    }
264
}
265