Completed
Push — master ( 5d97bb...655aac )
by Vladimir
06:34 queued 02:32
created

PulseColumnStatusValue::setValue()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 3
nc 4
nop 1
crap 3
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
/**
11
 * Class PulseColumnColorValue
12
 *
13
 * @package allejo\DaPulse\Objects
14
 * @since   0.1.0
15
 */
16
class PulseColumnStatusValue extends PulseColumnValue
17
{
18
    const DEFAULT_VALUE = self::Grey; // The default color for DaPulse columns
19
20
    /**
21
     * The index of the orange status
22
     */
23
    const Orange = 0;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected ORANGE).
Loading history...
24
25
    /**
26
     * The index of the light green status
27
     */
28
    const L_Green = 1;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected L_GREEN).
Loading history...
29
30
    /**
31
     * The index of the red status
32
     */
33
    const Red = 2;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected RED).
Loading history...
34
35
    /**
36
     * The index of the blue status
37
     */
38
    const Blue = 3;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected BLUE).
Loading history...
39
40
    /**
41
     * The index of the purple status
42
     */
43
    const Purple = 4;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected PURPLE).
Loading history...
44
45
    /**
46
     * The index of the grey status
47
     */
48
    const Grey = 5;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GREY).
Loading history...
49
    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...
50
51
    /**
52
     * The index of the green status
53
     */
54
    const Green = 6;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GREEN).
Loading history...
55
56
    /**
57
     * The index of the light blue status
58
     */
59
    const L_Blue = 7;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected L_BLUE).
Loading history...
60
61
    /**
62
     * The index of the gold status
63
     */
64
    const Gold = 8;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GOLD).
Loading history...
65
66
    /**
67
     * The index of the yellow status
68
     */
69
    const Yellow = 9;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected YELLOW).
Loading history...
70
71
    /**
72
     * The index of the black status
73
     */
74
    const Black = 10;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected BLACK).
Loading history...
75
76
    /**
77
     * The lowest status value that exists
78
     */
79
    const MIN_VALUE = self::Orange;
80
81
    /**
82
     * The largest status value that exists
83
     */
84
    const MAX_VALUE = self::Black;
85
86
    /**
87
     * Get the numerical representation of the color that a status column is set to.
88
     *
89
     * @api
90
     *
91
     * @since  0.1.0
92
     *
93
     * @return int The color value of a column
94
     */
95 2
    public function getValue ()
96
    {
97 2
        return parent::getValue();
98
    }
99
100
    /**
101
     * Update the status of a status column
102
     *
103
     * It is highly recommended that you use the constants available in the **PulseColumnColorValue** class to match the
104
     * colors; keep in mind this value cannot be higher than 11.
105
     *
106
     * @api
107
     *
108
     * @param int $color The numerical value of the new color value
109
     *
110
     * @see   PulseColumnStatusValue::Orange  PulseColumnStatusValue::Orange
111
     * @see   PulseColumnStatusValue::L_Green PulseColumnStatusValue::L_Green
112
     * @see   PulseColumnStatusValue::Red     PulseColumnStatusValue::Red
113
     * @see   PulseColumnStatusValue::Blue    PulseColumnStatusValue::Blue
114
     * @see   PulseColumnStatusValue::Purple  PulseColumnStatusValue::Purple
115
     * @see   PulseColumnStatusValue::Grey    PulseColumnStatusValue::Grey
116
     * @see   PulseColumnStatusValue::Green   PulseColumnStatusValue::Green
117
     * @see   PulseColumnStatusValue::L_Blue  PulseColumnStatusValue::L_Blue
118
     * @see   PulseColumnStatusValue::Gold    PulseColumnStatusValue::Gold
119
     * @see   PulseColumnStatusValue::Yellow  PulseColumnStatusValue::Yellow
120
     * @see   PulseColumnStatusValue::Black   PulseColumnStatusValue::Black
121
     *
122
     * @since 0.1.0
123
     *
124
     * @throws \InvalidArgumentException if the $color is not an int or is not between 0-10
125
     */
126 5
    public function updateValue ($color)
127
    {
128 5
        if ($color < 0 || $color > 10 || !is_int($color))
129
        {
130 4
            throw new \InvalidArgumentException("DaPulse only has color indexes from 0-10");
131
        }
132
133 1
        $url        = sprintf("%s/%d/columns/%s/status.json", self::apiEndpoint(), $this->board_id, $this->column_id);
134
        $postParams = [
135 1
            "pulse_id"    => $this->pulse_id,
136 1
            "color_index" => $color
137
        ];
138
139 1
        $result = self::sendPut($url, $postParams);
140 1
        $this->setValue($result);
141 1
    }
142
143
    /**
144
     * Get the hex value of the color used on DaPulse to represent the different statuses.
145
     *
146
     * @api
147
     *
148
     * @param  int $numericalValue The numerical value of the column
149
     *
150
     * @since  0.1.0
151
     *
152
     * @return string A hex value **without** the leading #
153
     */
154
    public static function getHexColor ($numericalValue)
155
    {
156
        $colorArray = [
157
            self::Orange  => "fdab3d",
158
            self::L_Green => "00c875",
159
            self::Red     => "e2445c",
160
            self::Blue    => "0086c0",
161
            self::L_Blue  => "579bfc",
162
            self::Purple  => "a25ddc",
163
            self::Green   => "037f4c",
164
            self::Gold    => "CAB641",
165
            self::Yellow  => "FFCB00",
166
            self::Black   => "333333",
167
            self::Grey    => "c4c4c4"
168
        ];
169
170
        return $colorArray[$numericalValue];
171
    }
172
173 2
    protected function setValue ($response)
174
    {
175 2
        $value = $response['value'];
176
177
        // If the status column is set to 'Grey' or the default 'Just Assigned' value, DaPulse will evidently
178
        // return null... So let's set it to the Grey value to not confuse people
179 2
        $this->column_value = (is_array($value) && array_key_exists('index', $value)) ? $response["value"]["index"] : self::Grey;
180
    }
181
}