Completed
Push — develop ( a05ff5...d1fb89 )
by Vladimir
03:27
created

PulseColumnStatusValue   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 164
Duplicated Lines 8.54 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 1
dl 14
loc 164
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 8 3
A getValue() 14 14 3
A updateValue() 0 16 4
A getHexColor() 0 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace allejo\DaPulse\Objects;
4
5
/**
6
 * Class PulseColumnColorValue
7
 *
8
 * @package allejo\DaPulse\Objects
9
 * @since   0.1.0
10
 */
11
class PulseColumnStatusValue extends PulseColumnValue
12
{
13
    /**
14
     * The index of the orange status
15
     */
16
    const Orange = 0;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected ORANGE).
Loading history...
17
18
    /**
19
     * The index of the light green status
20
     */
21
    const L_Green = 1;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected L_GREEN).
Loading history...
22
23
    /**
24
     * The index of the red status
25
     */
26
    const Red = 2;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected RED).
Loading history...
27
28
    /**
29
     * The index of the blue status
30
     */
31
    const Blue = 3;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected BLUE).
Loading history...
32
33
    /**
34
     * The index of the purple status
35
     */
36
    const Purple = 4;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected PURPLE).
Loading history...
37
38
    /**
39
     * The index of the grey status
40
     */
41
    const Grey = 5;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GREY).
Loading history...
42
    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...
43
44
    /**
45
     * The index of the green status
46
     */
47
    const Green = 6;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GREEN).
Loading history...
48
49
    /**
50
     * The index of the light blue status
51
     */
52
    const L_Blue = 7;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected L_BLUE).
Loading history...
53
54
    /**
55
     * The index of the gold status
56
     */
57
    const Gold = 8;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected GOLD).
Loading history...
58
59
    /**
60
     * The index of the yellow status
61
     */
62
    const Yellow = 9;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected YELLOW).
Loading history...
63
64
    /**
65
     * The index of the black status
66
     */
67
    const Black = 10;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected BLACK).
Loading history...
68
69
    /**
70
     * Get the numerical representation of the color that a status column is set to.
71
     *
72
     * @api
73
     *
74
     * @since  0.1.0
75
     *
76
     * @return int The color value of a column
77
     */
78 2 View Code Duplication
    public function getValue ()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 2
        if ($this->isNullValue())
81
        {
82 1
            return self::Grey; // The default color for DaPulse columns
83
        }
84
85 2
        if (!isset($this->column_value))
86
        {
87 1
            $this->setValue($this->jsonResponse);
88
        }
89
90 2
        return $this->column_value;
91
    }
92
93
    /**
94
     * Update the status of a status column
95
     *
96
     * It is highly recommended that you use the constants available in the **PulseColumnColorValue** class to match the
97
     * colors; keep in mind this value cannot be higher than 11.
98
     *
99
     * @api
100
     *
101
     * @param int $color The numerical value of the new color value
102
     *
103
     * @see   PulseColumnStatusValue::Orange  PulseColumnStatusValue::Orange
104
     * @see   PulseColumnStatusValue::L_Green PulseColumnStatusValue::L_Green
105
     * @see   PulseColumnStatusValue::Red     PulseColumnStatusValue::Red
106
     * @see   PulseColumnStatusValue::Blue    PulseColumnStatusValue::Blue
107
     * @see   PulseColumnStatusValue::Purple  PulseColumnStatusValue::Purple
108
     * @see   PulseColumnStatusValue::Grey    PulseColumnStatusValue::Grey
109
     * @see   PulseColumnStatusValue::Green   PulseColumnStatusValue::Green
110
     * @see   PulseColumnStatusValue::L_Blue  PulseColumnStatusValue::L_Blue
111
     * @see   PulseColumnStatusValue::Gold    PulseColumnStatusValue::Gold
112
     * @see   PulseColumnStatusValue::Yellow  PulseColumnStatusValue::Yellow
113
     * @see   PulseColumnStatusValue::Black   PulseColumnStatusValue::Black
114
     *
115
     * @since 0.1.0
116
     *
117
     * @throws \InvalidArgumentException if the $color is not an int or is not between 0-10
118
     */
119 5
    public function updateValue ($color)
120
    {
121 5
        if ($color < 0 || $color > 10 || !is_int($color))
122
        {
123 4
            throw new \InvalidArgumentException("DaPulse only has color indexes from 0-10");
124
        }
125
126 1
        $url        = sprintf("%s/%d/columns/%s/status.json", self::apiEndpoint(), $this->board_id, $this->column_id);
127
        $postParams = array(
128 1
            "pulse_id"    => $this->pulse_id,
129 1
            "color_index" => $color
130
        );
131
132 1
        $result = self::sendPut($url, $postParams);
133 1
        $this->setValue($result);
134 1
    }
135
136
    /**
137
     * Get the hex value of the color used on DaPulse to represent the different statuses.
138
     *
139
     * @api
140
     *
141
     * @param  int $numericalValue The numerical value of the column
142
     *
143
     * @since  0.1.0
144
     *
145
     * @return string A hex value **without** the leading #
146
     */
147
    public static function getHexColor ($numericalValue)
148
    {
149
        $colorArray = array(
150
            self::Orange  => "fdab3d",
151
            self::L_Green => "00c875",
152
            self::Red     => "e2445c",
153
            self::Blue    => "0086c0",
154
            self::L_Blue  => "579bfc",
155
            self::Purple  => "a25ddc",
156
            self::Green   => "037f4c",
157
            self::Gold    => "CAB641",
158
            self::Yellow  => "FFCB00",
159
            self::Black   => "333333",
160
            self::Grey    => "c4c4c4"
161
        );
162
163
        return $colorArray[$numericalValue];
164
    }
165
166 2
    protected function setValue ($response)
167
    {
168 2
        $value = $response['value'];
169
170
        // If the status column is set to 'Grey' or the default 'Just Assigned' value, DaPulse will evidently
171
        // return null... So let's set it to the Grey value to not confuse people
172 2
        $this->column_value = (is_array($value) && array_key_exists('index', $value)) ? $response["value"]["index"] : self::Grey;
173
    }
174
}