Completed
Push — master ( f26179...1e061c )
by Vladimir
04:47
created

PulseColumn   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 44%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 145
c 0
b 0
f 0
ccs 22
cts 50
cp 0.44
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getId() 0 4 1
A getTitle() 0 6 1
A getType() 0 12 2
A getLabels() 0 12 2
A getBoardId() 0 6 1
A editTitle() 0 4 1
A editLabels() 0 4 1
A deleteColumn() 0 8 1
A editField() 0 12 1
A getColumnsUrl() 0 4 1
A sanitizeLabels() 0 14 4
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";
26
    const Numeric  = "numeric";
27
    const Person   = "person";
28
    const Status   = "status";
29
    const Tag      = "tag";
30
    const Text     = "text";
31
    const Timeline = "timerange";
32
33
    protected $title;
34
    protected $type;
35
    protected $empty_text;
36
    protected $labels;
37
    protected $board_id;
38
39
    private $labelsSanityCheck;
40
41
    /**
42
     * PulseColumn constructor.
43
     *
44
     * @internal
45
     *
46
     * @param array $idOrArray
47
     */
48 40
    public function __construct ($idOrArray)
49
    {
50 40
        $this->arrayConstructionOnly = true;
51 40
        $this->labelsSanityCheck = false;
52
53 40
        parent::__construct($idOrArray);
54 40
    }
55
56
    public function getId ()
57
    {
58
        return $this->id;
59
    }
60
61
    public function getTitle ()
62
    {
63
        $this->lazyLoad();
64
65
        return $this->title;
66
    }
67
68 13
    public function getType ()
69
    {
70 13
        $this->lazyLoad();
71
72
        // @todo Workaround due to a bug in DaPulse's API see: https://github.com/allejo/PhpPulse/issues/5
73 13
        if ($this->type === "color")
74
        {
75 2
            $this->type = self::Status;
76
        }
77
78 13
        return $this->type;
79
    }
80
81
    /**
82
     * Get the labels for a Status column type
83
     *
84
     * @throws InvalidColumnException if the column is not a Status column
85
     *
86
     * @since  0.3.0 An InvalidColumnException is thrown when the given column is not a status column
87
     * @since  0.1.0
88
     *
89
     * @return string[]
90
     */
91 2
    public function getLabels ()
92
    {
93 2
        if ($this->getType() != PulseColumn::Status)
94
        {
95 1
            throw new InvalidColumnException('This column type does not contain labels');
96
        }
97
98 1
        $this->lazyLoad();
99 1
        $this->sanitizeLabels();
100
101 1
        return $this->labels;
102
    }
103
104
    public function getBoardId ()
105
    {
106
        $this->lazyLoad();
107
108
        return $this->board_id;
109
    }
110
111
    public function editTitle ($title)
112
    {
113
        $this->editField("title", $title);
114
    }
115
116
    public function editLabels ($labels)
117
    {
118
        $this->editField("labels", $labels);
119
    }
120
121
    public function deleteColumn ()
122
    {
123
        $this->checkInvalid();
124
125
        self::sendDelete($this->getColumnsUrl());
126
127
        $this->deletedObject = true;
128
    }
129
130
    private function editField ($field, $value)
131
    {
132
        $this->checkInvalid();
133
134
        $postParams = [
135
            $field => $value
136
        ];
137
138
        self::sendPut($this->getColumnsUrl(), $postParams);
139
140
        $this->$field = $value;
141
    }
142
143
    private function getColumnsUrl ()
144
    {
145
        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...
146
    }
147
148
    /**
149
     * DaPulse will only set labels if it has content
150
     */
151 1
    private function sanitizeLabels ()
152
    {
153 1
        if ($this->labelsSanityCheck)
154
        {
155
            return;
156
        }
157
158 1
        for ($i = StatusColumn::MIN_VALUE; $i <= StatusColumn::MAX_VALUE; $i++)
159
        {
160 1
            !isset($this->labels[$i]) && $this->labels[$i] = '';
161
        }
162
163 1
        $this->labelsSanityCheck = true;
164 1
    }
165
}
166