Test Setup Failed
Pull Request — master (#247)
by Phecho
20:06 queued 25s
created

ColorModel   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 221
rs 10
c 1
b 1
f 0
wmc 15
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 14 4
A getColorProperties() 0 8 2
A getList() 0 12 3
A getDefaultColor() 0 4 1
A getDefaultColors() 0 4 1
A getCss() 0 14 2
A getBorderWidth() 0 6 1
A getBorderLeftColor() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Model;
13
14
use Jitamin\Foundation\Database\Model;
15
16
/**
17
 * Color model.
18
 */
19
class ColorModel extends Model
20
{
21
    /**
22
     * Default colors.
23
     *
24
     * @var array
25
     */
26
    private $default_colors = [
27
        'white' => [
28
            'name'              => 'White',
29
            'border-left-color' => '#CCCCCC',
30
            'border-width'      => '3px',
31
        ],
32
        'yellow' => [
33
            'name'              => 'Yellow',
34
            'border-left-color' => '#F5F7C4',
35
            'border-width'      => '3px',
36
        ],
37
        'blue' => [
38
            'name'              => 'Blue',
39
            'border-left-color' => '#DBEBFF',
40
            'border-width'      => '3px',
41
        ],
42
        'green' => [
43
            'name'              => 'Green',
44
            'border-left-color' => '#BDF4CB',
45
            'border-width'      => '3px',
46
        ],
47
        'purple' => [
48
            'name'              => 'Purple',
49
            'border-left-color' => '#DFB0FF',
50
            'border-width'      => '3px',
51
        ],
52
        'red' => [
53
            'name'              => 'Red',
54
            'border-left-color' => '#FFBBBB',
55
            'border-width'      => '3px',
56
        ],
57
        'orange' => [
58
            'name'              => 'Orange',
59
            'border-left-color' => '#FFD7B3',
60
            'border-width'      => '3px',
61
        ],
62
        'grey' => [
63
            'name'              => 'Grey',
64
            'border-left-color' => '#EEEEEE',
65
            'border-width'      => '3px',
66
        ],
67
        'brown' => [
68
            'name'              => 'Brown',
69
            'border-left-color' => '#D7CCC8',
70
            'border-width'      => '3px',
71
        ],
72
        'deep_orange' => [
73
            'name'              => 'Deep Orange',
74
            'border-left-color' => '#FFAB91',
75
            'border-width'      => '3px',
76
        ],
77
        'dark_grey' => [
78
            'name'              => 'Dark Grey',
79
            'border-left-color' => '#CFD8DC',
80
            'border-width'      => '3px',
81
        ],
82
        'pink' => [
83
            'name'              => 'Pink',
84
            'border-left-color' => '#F48FB1',
85
            'border-width'      => '3px',
86
        ],
87
        'teal' => [
88
            'name'              => 'Teal',
89
            'border-left-color' => '#80CBC4',
90
            'border-width'      => '3px',
91
        ],
92
        'cyan' => [
93
            'name'              => 'Cyan',
94
            'border-left-color' => '#B2EBF2',
95
            'border-width'      => '3px',
96
        ],
97
        'lime' => [
98
            'name'              => 'Lime',
99
            'border-left-color' => '#E6EE9C',
100
            'border-width'      => '3px',
101
        ],
102
        'light_green' => [
103
            'name'              => 'Light Green',
104
            'border-left-color' => '#DCEDC8',
105
            'border-width'      => '3px',
106
        ],
107
        'amber' => [
108
            'name'              => 'Amber',
109
            'border-left-color' => '#FFE082',
110
            'border-width'      => '3px',
111
        ],
112
    ];
113
114
    /**
115
     * Find a color id from the name or the id.
116
     *
117
     * @param string $color
118
     *
119
     * @return string
120
     */
121
    public function find($color)
122
    {
123
        $color = strtolower($color);
124
125
        foreach ($this->default_colors as $color_id => $params) {
126
            if ($color_id === $color) {
127
                return $color_id;
128
            } elseif ($color === strtolower($params['name'])) {
129
                return $color_id;
130
            }
131
        }
132
133
        return '';
134
    }
135
136
    /**
137
     * Get color properties.
138
     *
139
     * @param string $color_id
140
     *
141
     * @return array
142
     */
143
    public function getColorProperties($color_id)
144
    {
145
        if (isset($this->default_colors[$color_id])) {
146
            return $this->default_colors[$color_id];
147
        }
148
149
        return $this->default_colors[$this->getDefaultColor()];
150
    }
151
152
    /**
153
     * Get available colors.
154
     *
155
     * @param bool $prepend
156
     *
157
     * @return array
158
     */
159
    public function getList($prepend = false)
160
    {
161
        $listing = $prepend ? ['' => t('All colors')] : [];
162
163
        foreach ($this->default_colors as $color_id => $color) {
164
            $listing[$color_id] = t($color['name']);
165
        }
166
167
        $this->hook->reference('model:color:get-list', $listing);
0 ignored issues
show
Documentation introduced by
The property hook does not exist on object<Jitamin\Model\ColorModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
169
        return $listing;
170
    }
171
172
    /**
173
     * Get the default color.
174
     *
175
     * @return string
176
     */
177
    public function getDefaultColor()
178
    {
179
        return $this->settingModel->get('default_color', 'yellow');
0 ignored issues
show
Documentation introduced by
The property settingModel does not exist on object<Jitamin\Model\ColorModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
180
    }
181
182
    /**
183
     * Get the default colors.
184
     *
185
     * @return array
186
     */
187
    public function getDefaultColors()
188
    {
189
        return $this->default_colors;
190
    }
191
192
    /**
193
     * Get border width from string.
194
     *
195
     * @param string $color_id Color id
196
     *
197
     * @return string
198
     */
199
    public function getBorderWidth($color_id)
200
    {
201
        $color = $this->getColorProperties($color_id);
202
203
        return $color['border-width'];
204
    }
205
206
    /**
207
     * Get border left color from the color_id.
208
     *
209
     * @param string $color_id Color id
210
     *
211
     * @return string
212
     */
213
    public function getBorderLeftColor($color_id)
214
    {
215
        $color = $this->getColorProperties($color_id);
216
217
        return $color['border-left-color'];
218
    }
219
220
    /**
221
     * Get CSS stylesheet of all colors.
222
     *
223
     * @return string
224
     */
225
    public function getCss()
226
    {
227
        $buffer = '';
228
229
        foreach ($this->default_colors as $color => $values) {
230
            $buffer .= 'div.color-'.$color.' {';
231
            $buffer .= 'border-left-width: '.$values['border-width'].';';
232
            $buffer .= 'border-left-color: '.$values['border-left-color'];
233
            $buffer .= '}';
234
            $buffer .= 'td.color-'.$color.' { background-color: '.$values['border-left-color'].'}';
235
        }
236
237
        return $buffer;
238
    }
239
}
240