Completed
Push — master ( 7f1c2b...4a1992 )
by Vladimir
03:46
created

PulseColumnStatusValue::getHexColors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
c 1
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/PhpPulse/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\DaPulse\Objects;
9
10
use allejo\DaPulse\Exceptions\ColumnNotFoundException;
11
12
/**
13
 * Class PulseColumnColorValue
14
 *
15
 * @package allejo\DaPulse\Objects
16
 * @since   0.1.0
17
 */
18
class PulseColumnStatusValue extends PulseColumnValue
19
{
20
    const DEFAULT_VALUE = self::Grey; // The default color for DaPulse columns
21
22
    /**
23
     * The index of the orange status
24
     */
25
    const Orange = 0;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected ORANGE).
Loading history...
26
27
    /**
28
     * The index of the light green status
29
     */
30
    const L_Green = 1;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected L_GREEN).
Loading history...
31
32
    /**
33
     * The index of the red status
34
     */
35
    const Red = 2;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected RED).
Loading history...
36
37
    /**
38
     * The index of the blue status
39
     */
40
    const Blue = 3;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected BLUE).
Loading history...
41
42
    /**
43
     * The index of the purple status
44
     */
45
    const Purple = 4;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected PURPLE).
Loading history...
46
47
    /**
48
     * The index of the grey status
49
     */
50
    const Grey = 5;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GREY).
Loading history...
51
    const Gray = self::Grey; // just an alias
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GRAY).
Loading history...
52
53
    /**
54
     * The index of the green status
55
     */
56
    const Green = 6;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GREEN).
Loading history...
57
58
    /**
59
     * The index of the light blue status
60
     */
61
    const L_Blue = 7;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected L_BLUE).
Loading history...
62
63
    /**
64
     * The index of the gold status
65
     */
66
    const Gold = 8;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GOLD).
Loading history...
67
68
    /**
69
     * The index of the yellow status
70
     */
71
    const Yellow = 9;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected YELLOW).
Loading history...
72
73
    /**
74
     * The index of the black status
75
     */
76
    const Black = 10;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected BLACK).
Loading history...
77
78
    /**
79
     * The lowest status value that exists
80
     */
81
    const MIN_VALUE = self::Orange;
82
83
    /**
84
     * The largest status value that exists
85
     */
86
    const MAX_VALUE = self::Black;
87
88
    /**
89
     * Get the numerical representation of the color that a status column is set to.
90
     *
91
     * @api
92
     *
93
     * @since  0.4.0 ColumnNotFoundException is now thrown
94
     * @since  0.1.0
95
     *
96
     * @throws ColumnNotFoundException The specified column ID does not exist for the parent Pulse
97
     *
98
     * @return int The color value of a column
99
     */
100 2
    public function getValue ()
101
    {
102 2
        return parent::getValue();
103
    }
104
105
    /**
106
     * Update the status of a status column
107
     *
108
     * It is highly recommended that you use the constants available in the **PulseColumnColorValue** class to match the
109
     * colors; keep in mind this value cannot be higher than 11.
110
     *
111
     * @api
112
     *
113
     * @param int $color The numerical value of the new color value
114
     *
115
     * @see   PulseColumnStatusValue::Orange  PulseColumnStatusValue::Orange
116
     * @see   PulseColumnStatusValue::L_Green PulseColumnStatusValue::L_Green
117
     * @see   PulseColumnStatusValue::Red     PulseColumnStatusValue::Red
118
     * @see   PulseColumnStatusValue::Blue    PulseColumnStatusValue::Blue
119
     * @see   PulseColumnStatusValue::Purple  PulseColumnStatusValue::Purple
120
     * @see   PulseColumnStatusValue::Grey    PulseColumnStatusValue::Grey
121
     * @see   PulseColumnStatusValue::Green   PulseColumnStatusValue::Green
122
     * @see   PulseColumnStatusValue::L_Blue  PulseColumnStatusValue::L_Blue
123
     * @see   PulseColumnStatusValue::Gold    PulseColumnStatusValue::Gold
124
     * @see   PulseColumnStatusValue::Yellow  PulseColumnStatusValue::Yellow
125
     * @see   PulseColumnStatusValue::Black   PulseColumnStatusValue::Black
126
     *
127
     * @since 0.1.0
128
     *
129
     * @throws \InvalidArgumentException if the $color is not an int or is not between 0-10
130
     */
131 5
    public function updateValue ($color)
132
    {
133 5
        if ($color < 0 || $color > 10 || !is_int($color))
134
        {
135 4
            throw new \InvalidArgumentException("DaPulse only has color indexes from 0-10");
136
        }
137
138 1
        $url        = sprintf("%s/%d/columns/%s/status.json", self::apiEndpoint(), $this->board_id, $this->column_id);
139
        $postParams = [
140 1
            "pulse_id"    => $this->pulse_id,
141 1
            "color_index" => $color
142
        ];
143
144 1
        $result = self::sendPut($url, $postParams);
145 1
        $this->jsonResponse = $result;
0 ignored issues
show
Documentation Bug introduced by
It seems like $result of type * is incompatible with the declared type array of property $jsonResponse.

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...
146 1
        $this->setValue($result);
147
    }
148
149
    /**
150
     * Get the hex value of the color used on DaPulse to represent the different statuses.
151
     *
152
     * @api
153
     *
154
     * @param  int $numericalValue The numerical value of the column
155
     *
156
     * @since  0.1.0
157
     *
158
     * @return string A hex value **without** the leading #
159
     */
160
    public static function getHexColor ($numericalValue)
161
    {
162
        $colorArray = self::getHexColors();
163
164
        return $colorArray[$numericalValue];
165
    }
166
167
    /**
168
     * Get an array of hex values for each of the statuses
169
     *
170
     * @api
171
     *
172
     * @since  0.3.1
173
     *
174
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
175
     */
176
    public static function getHexColors ()
177
    {
178 2
        return [
179
            self::Orange  => "fdab3d",
180 2
            self::L_Green => "00c875",
181
            self::Red     => "e2445c",
182
            self::Blue    => "0086c0",
183
            self::L_Blue  => "579bfc",
184 2
            self::Purple  => "a25ddc",
185 2
            self::Green   => "037f4c",
186
            self::Gold    => "CAB641",
187
            self::Yellow  => "FFCB00",
188
            self::Black   => "333333",
189
            self::Grey    => "c4c4c4"
190
        ];
191
    }
192
193
    protected function setValue ($response)
194
    {
195
        $value = $response['value'];
196
197
        // If the status column is set to 'Grey' or the default 'Just Assigned' value, DaPulse will evidently
198
        // return null... So let's set it to the Grey value to not confuse people
199
        $this->column_value = (is_array($value) && array_key_exists('index', $value)) ? $response["value"]["index"] : self::Grey;
200
    }
201
}
202