Completed
Push — master ( 95cc27...d32772 )
by Vladimir
05:08 queued 03:05
created

PulseColumnDateValue::setValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
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 PulseColumnTextValue
12
 *
13
 * @package allejo\DaPulse\Objects
14
 * @since   0.1.0
15
 */
16
class PulseColumnDateValue extends PulseColumnValue
17
{
18
    /**
19
     * Get the date listed in the date column
20
     *
21
     * @api
22
     *
23
     * @since  0.1.0
24
     *
25
     * @return \DateTime|null Null is returned if there is no value set for this column
26
     */
27 3
    public function getValue ()
28
    {
29 3
        return parent::getValue();
30
    }
31
32
    /**
33
     * Update the date of the date column.
34
     *
35
     * The specific time of the DateTime object will be ignored.
36
     *
37
     * @api
38
     *
39
     * @param \DateTime $dateTime The new date
40
     *
41
     * @since 0.3.0 \InvalidArgumentException is now thrown
42
     * @since 0.1.0
43
     *
44
     * @throws \InvalidArgumentException if $dateTime is not a \DateTime
45
     */
46 5 View Code Duplication
    public function updateValue ($dateTime)
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...
47
    {
48 5
        if (!($dateTime instanceof \DateTime))
49
        {
50 4
            throw new \InvalidArgumentException('$dateTime is expected to be of type \\DateTime');
51
        }
52
53 1
        $url        = sprintf("%s/%d/columns/%s/date.json", self::apiEndpoint(), $this->board_id, $this->column_id);
54
        $postParams = [
55 1
            "pulse_id" => $this->pulse_id,
56 1
            "date_str" => date_format($dateTime, "Y-m-d")
57
        ];
58
59 1
        $result = self::sendPut($url, $postParams);
60 1
        $this->setValue($result);
61 1
    }
62
63 2
    protected function setValue ($response)
64
    {
65
        // Another DaPulse API inconsistency. The data returned from a PUT request has the date in an array instead of
66
        // of a string.
67 2
        if (is_array($response['value']) && isset($response['value']['date']))
68
        {
69 1
            $this->column_value = new \DateTime($response['value']['date']);
70
71 1
            return;
72
        }
73
74 1
        $this->column_value = new \DateTime($response['value']);
75
    }
76
}