Generic::getColorClass()   A
last analyzed

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
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Properties\AbstractProperty;
4
5
use ByTIC\Models\SmartProperties\Workflow\State;
6
use Symfony\Component\Workflow\Definition;
7
use Symfony\Component\Workflow\Event\AnnounceEvent;
8
use Symfony\Component\Workflow\Marking;
9
use Symfony\Component\Workflow\Transition;
10
use Symfony\Component\Workflow\Workflow;
11
12
/**
13
 * Class Generic
14
 * @package ByTIC\Models\SmartProperties\Properties\AbstractProperty
15
 */
16
abstract class Generic extends State
17
{
18
    use Traits\HasItemTrait;
19
    use Traits\HasManagerTrait;
20
    use Traits\HasNameTrait;
21
    use Traits\HasTranslationTrait;
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $field;
27
28
    /**
29
     * @param $name
30
     * @return null
31
     */
32
    public function __get($name)
33
    {
34
        $method = 'get' . ucfirst($name);
35
        if (method_exists($this, $method)) {
36
            return $this->$method();
37
        }
38
39
        return null;
40
    }
41
42
    /**
43
     * @param bool $short
44
     * @return string
45
     */
46
    public function getLabelHTML($short = false)
47
    {
48
        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...
49
        style="' . $this->getColorCSS() . '">
50
            ' . $this->getIconHTML() . '
51
            ' . $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...
52
        </span>';
53
    }
54
55
    /**
56
     * get the label class name
57
     *
58
     * @return string
59
     */
60
    public function getLabelClasses(): string
61
    {
62
        return 'badge bg-' . $this->getColorClass() . ' badge-' . $this->getColorClass() . ' label label-' . $this->getColorClass();
63
    }
64
65
    /**
66
     * Get the color class
67
     *
68
     * @return string
69
     */
70
    public function getColorClass()
71
    {
72
        return 'dark';
73
    }
74
75
76
    /**
77
     * @return string
78
     */
79
    abstract protected function getLabelSlug();
80
81
    /**
82
     * @return boolean
83
     */
84
    protected function hasShortLabel()
85
    {
86
        return true;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getColorCSS()
93
    {
94
        $css = [];
95
        if ($this->getBGColor()) {
96
            $css[] = 'background-color: ' . $this->getBGColor().' !important';
97
        }
98
        if ($this->getFGColor()) {
99
            $css[] = 'color: ' . $this->getFGColor().' !important';
100
        }
101
102
        return implode(';', $css);
103
    }
104
105
    /**
106
     * @return bool|string
107
     */
108
    public function getBGColor()
109
    {
110
        return false;
111
    }
112
113
    /**
114
     * @return bool|string
115
     */
116
    public function getFGColor()
117
    {
118
        return false;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getIconHTML(): string
125
    {
126
        $icon = $this->getIcon();
127
        if (strpos($icon, 'glyphicon') !== false) {
0 ignored issues
show
Bug introduced by
$icon of type false is incompatible with the type string expected by parameter $haystack of strpos(). ( Ignorable by Annotation )

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

127
        if (strpos(/** @scrutinizer ignore-type */ $icon, 'glyphicon') !== false) {
Loading history...
128
            return '<span class="glyphicon glyphicon-white ' . $icon . '"></span> ';
0 ignored issues
show
Bug introduced by
Are you sure $icon of type false can be used in concatenation? ( Ignorable by Annotation )

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

128
            return '<span class="glyphicon glyphicon-white ' . /** @scrutinizer ignore-type */ $icon . '"></span> ';
Loading history...
129
        }
130
        if (strpos($icon, 'fas') === 0 || strpos($icon, 'far') === 0) {
131
            return '<i class="' . $icon . '"></i>';
132
        }
133
        if (strpos($icon, 'fa') === 0) {
134
            return '<i class="fas ' . $icon . '"></i>';
135
        }
136
137
        return '';
138
    }
139
140
    /**
141
     * @return bool|string
142
     */
143
    public function getIcon()
144
    {
145
        return false;
146
    }
147
148
    /**
149
     * @return bool|mixed
150
     */
151
    public function update()
152
    {
153
        $item = $this->getItem();
154
        if (!is_object($item)) {
155
            return false;
156
        }
157
        $this->preValueChange();
158
159
        $fromState = (string) $item->{$this->getField()};
160
        $toState = $this->getName();
161
        /** @noinspection PhpUndefinedFieldInspection */
162
        $item->{$this->getField()} = $toState;
163
        $this->preUpdate();
164
        $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...
Bug introduced by
It seems like saveRecord() 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

164
        /** @scrutinizer ignore-call */ 
165
        $return = $item->saveRecord();
Loading history...
165
        $this->postUpdate();
166
167
        $marking = new Marking();
168
        $initialTransition = new Transition('generic_transition', [$fromState], [$toState]);
169
        $workflow = new Workflow(new Definition([], []));
170
        $event = new AnnounceEvent($item, $marking, $initialTransition, $workflow, []);
171
172
        event($event);
173
        return $return;
174
    }
175
176
    public function preValueChange()
177
    {
178
    }
179
180
    /**
181
     * @return null|string
182
     */
183
    public function getField()
184
    {
185 1
        return $this->field;
186
    }
187 1
188 1
    /**
189 1
     * @param null|string $field
190
     */
191 1
    public function setField($field)
192 1
    {
193 1
        $this->field = $field;
194 1
    }
195
196 1
    public function preUpdate()
197
    {
198
    }
199
200
    public function postUpdate()
201
    {
202
    }
203
204
    /**
205 1
     * @return string
206
     */
207 1
    public function getMessageType()
208
    {
209
        return 'info';
210
    }
211
212
}
213