Completed
Push — master ( 8b4306...4d4668 )
by Vladimir
06:11
created

PulseColumnValue::lazyLoad()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 9
cts 9
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 0
crap 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\Objects;
9
10
use allejo\DaPulse\Exceptions\ColumnNotFoundException;
11
use allejo\DaPulse\Exceptions\HttpException;
12
use allejo\DaPulse\Exceptions\InvalidObjectException;
13
use allejo\DaPulse\PulseColumn;
14
15
/**
16
 * The base class used for column values belonging to a specified class
17
 *
18
 * @internal
19
 * @package allejo\DaPulse\Objects
20
 * @since   0.1.0
21
 */
22
abstract class PulseColumnValue extends ApiObject
23
{
24
    const API_PREFIX = "boards";
25
26
    /**
27
     * The default return value for getValue()
28
     *
29
     * @internal
30
     */
31
    const DEFAULT_VALUE = null;
32
33
    /**
34
     * The ID of the parent board that this column's Pulse belongs to.
35
     *
36
     * @var int
37
     */
38
    protected $board_id;
39
40
    /**
41
     * The ID of the current column. This is a unique identifier when accessing columns through the API.
42
     *
43
     * @var string
44
     */
45
    protected $column_id;
46
47
    /**
48
     * The ID of the Pulse this column value belongs to
49
     *
50
     * @var int
51
     */
52
    protected $pulse_id;
53
54
    /**
55
     * The value that this column has. The data type can be an integer, string, or DateTime depending on the column type
56
     *
57
     * @var mixed
58
     */
59
    protected $column_value;
60
61
    /**
62
     * This constructor only accepts an array of the data regarding a specific column
63
     *
64
     * @internal
65
     *
66
     * @param array $array An associative array containing information regarding a column's value
67
     *
68
     * @since 0.1.0
69
     *
70
     * @throws \InvalidArgumentException An ID is given when an instance of this object can only be created from an
71
     *                                   array of existing data
72
     */
73 39
    public function __construct ($array)
74
    {
75 39
        $this->arrayConstructionOnly = true;
76
77 39
        parent::__construct($array);
78 39
    }
79
80
    /**
81
     * This function must be overloaded by child classes solely calling this function to allow each implementation to
82
     * have its own phpDocs.
83
     */
84 18
    public function getValue ()
85
    {
86 18
        $this->lazyLoad();
87
88 17
        if ($this->isNullValue())
89
        {
90 7
            return static::DEFAULT_VALUE;
91
        }
92
93 13
        if (!isset($this->column_value))
94
        {
95 6
            $this->setValue($this->jsonResponse);
96
        }
97
98 13
        return $this->column_value;
99
    }
100
101
    /**
102
     * Create the appropriate object based on the type of column
103
     *
104
     * @internal
105
     *
106
     * @param string $type The type of column as specified by DaPulse's API; i.e. 'text', 'date', 'status', 'person'
107
     * @param array  $data An associative array containing data regarding the column
108
     *
109
     * @since 0.1.0
110
     *
111
     * @todo  Fix the hardcoded "color" case statement. See: https://github.com/allejo/PhpPulse/issues/5
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...
112
     *
113
     * @throws InvalidObjectException
114
     *
115
     * @return PulseColumnStatusValue|PulseColumnDateValue|PulseColumnNumericValue|PulseColumnPersonValue|PulseColumnTextValue|PulseColumnTimelineValue
116
     */
117 40
    public static function _createColumnType ($type, $data)
118
    {
119
        switch ($type)
120
        {
121 40
            case PulseColumn::Text:
122 4
                return (new PulseColumnTextValue($data));
123
124 36
            case "color":
125 36
            case PulseColumn::Status:
126 6
                return (new PulseColumnStatusValue($data));
127
128 30
            case PulseColumn::Numeric:
129 7
                return (new PulseColumnNumericValue($data));
130
131 23
            case PulseColumn::Person:
132 9
                return (new PulseColumnPersonValue($data));
133
134 14
            case PulseColumn::Date:
135 8
                return (new PulseColumnDateValue($data));
136
137 6
            case PulseColumn::Timeline:
138 5
                return (new PulseColumnTimelineValue($data));
139
        }
140
141 1
        throw new InvalidObjectException("'$type' is an unsupported column type to modify.");
142
    }
143
144
    /**
145
     * Check whether to return null because a column's value does not exist.
146
     *
147
     * @return bool True if the column does not have a value
148
     */
149 17
    protected function isNullValue ()
150
    {
151 17
        return (!isset($this->column_value) && is_null($this->jsonResponse["value"]));
152
    }
153
154 18
    protected function lazyLoad ()
155
    {
156 18
        if (!array_key_exists('value', $this->jsonResponse) && is_null($this->column_value))
157
        {
158 8
            $url    = sprintf("%s/%d/columns/%s/value.json", self::apiEndpoint("boards"), $this->board_id, $this->column_id);
159
            $params = [
160 8
                "pulse_id" => $this->pulse_id
161
            ];
162
163
            try
164
            {
165 8
                $results = self::sendGet($url, $params);
166
            }
167 1
            catch (HttpException $e)
168
            {
169 1
                throw new ColumnNotFoundException("The '$this->column_id' column could not be found");
170
            }
171
172
            // Store our value inside of jsonResponse so all of the respective objects can treat the data the same
173
            // as when accessed through a PulseBoard
174 7
            $this->jsonResponse['value'] = $results['value'];
175
        }
176 17
    }
177
178
    /**
179
     * Cast and set the appropriate value for this column
180
     *
181
     * @param $response
182
     */
183
    abstract protected function setValue ($response);
184
}