Completed
Push — master ( 712db0...565319 )
by Vladimir
05:12
created

PulseColumn::sanitizeLabels()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 6
nc 4
nop 0
crap 20
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;
9
10
use allejo\DaPulse\Exceptions\InvalidColumnException;
11
use allejo\DaPulse\Objects\ApiObject;
12
use allejo\DaPulse\Objects\PulseColumnStatusValue as StatusColumn;
13
14
/**
15
 * Class PulseColumn
16
 *
17
 * @api
18
 * @package allejo\DaPulse
19
 * @since   0.1.0
20
 */
21
class PulseColumn extends ApiObject
22
{
23
    const API_PREFIX = "boards";
24
25
    const Date     = "date";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected DATE).
Loading history...
26
    const Numeric  = "numeric";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected NUMERIC).
Loading history...
27
    const Person   = "person";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected PERSON).
Loading history...
28
    const Status   = "status";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected STATUS).
Loading history...
29
    const Text     = "text";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected TEXT).
Loading history...
30
    const Timeline = "timerange";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected TIMELINE).
Loading history...
31
32
    protected $title;
33
    protected $type;
34
    protected $empty_text;
35
    protected $labels;
36 38
    protected $board_id;
37
38 38
    private $labelsSanityCheck;
39
40 38
    public function __construct ($idOrArray)
41 38
    {
42
        $this->arrayConstructionOnly = true;
43
        $this->labelsSanityCheck = false;
44
45
        parent::__construct($idOrArray);
46
    }
47
48
    public function getId ()
49
    {
50
        return $this->id;
51
    }
52
53
    public function getTitle ()
54
    {
55 11
        $this->lazyLoad();
56
57 11
        return $this->title;
58
    }
59
60 11
    public function getType ()
61
    {
62 1
        $this->lazyLoad();
63
64
        // @todo Workaround due to a bug in DaPulse's API see: https://github.com/allejo/PhpPulse/issues/5
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
65 11
        if ($this->type === "color")
66
        {
67
            $this->type = self::Status;
68
        }
69
70
        return $this->type;
71
    }
72
73
    /**
74
     * @api
75
     * @todo Remove at 0.4.0 or next breaking release
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
76
     * @deprecated 0.3.0 This information is only set on the "Last Update" column and therefore shall be removed. This
77
     *                   value is still available by getting the JSON equivalent of the object if it's needed.
78
     * @since  0.1.0
79
     * @return string|null
80
     */
81
    public function getEmptyText ()
82
    {
83
        $this->lazyLoad();
84
85
        return $this->empty_text;
86
    }
87
88
    /**
89
     * Get the labels for a Status column type
90
     *
91
     * @throws InvalidColumnException if the column is not a Status column
92
     *
93
     * @since  0.3.0 An InvalidColumnException is thrown when the given column is not a status column
94
     * @since  0.1.0
95
     *
96
     * @return string[]
97
     */
98
    public function getLabels ()
99
    {
100
        if ($this->getType() != PulseColumn::Status)
101
        {
102
            throw new InvalidColumnException('This column type does not contain labels');
103
        }
104
105
        $this->lazyLoad();
106
        $this->sanitizeLabels();
107
108
        return $this->labels;
109
    }
110
111
    public function getBoardId ()
112
    {
113
        $this->lazyLoad();
114
115
        return $this->board_id;
116
    }
117
118
    public function editTitle ($title)
119
    {
120
        $this->editField("title", $title);
121
    }
122
123
    public function editLabels ($labels)
124
    {
125
        $this->editField("labels", $labels);
126
    }
127
128
    public function deleteColumn ()
129
    {
130
        $this->checkInvalid();
131
132
        self::sendDelete($this->getColumnsUrl());
133
134
        $this->deletedObject = true;
135
    }
136
137
    private function editField ($field, $value)
138
    {
139
        $this->checkInvalid();
140
141
        $postParams = [
142
            $field => $value
143
        ];
144
145
        self::sendPut($this->getColumnsUrl(), $postParams);
146
147
        $this->$field = $value;
148
    }
149
150
    private function getColumnsUrl ()
151
    {
152
        return sprintf("%s/%d/columns/%s.json", parent::apiEndpoint(), $this->getBoardId(), $this->getId());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (apiEndpoint() instead of getColumnsUrl()). Are you sure this is correct? If so, you might want to change this to $this->apiEndpoint().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
153
    }
154
155
    /**
156
     * DaPulse will only set labels if it has content
157
     */
158
    private function sanitizeLabels ()
159
    {
160
        if ($this->labelsSanityCheck)
161
        {
162
            return;
163
        }
164
165
        for ($i = StatusColumn::MIN_VALUE; $i <= StatusColumn::MAX_VALUE; $i++)
166
        {
167
            !isset($this->labels[$i]) && $this->labels[$i] = '';
168
        }
169
170
        $this->labelsSanityCheck = true;
171
    }
172
}