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

PulseColumnPersonValue::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\PulseUser;
11
12
/**
13
 * Class PulseColumnTextValue
14
 *
15
 * @package allejo\DaPulse\Objects
16
 * @since   0.1.0
17
 */
18
class PulseColumnPersonValue extends PulseColumnValue
19
{
20
    /**
21
     * Get the person assigned listed in the person column
22
     *
23
     * @api
24
     *
25
     * @since  0.1.0
26
     *
27
     * @return PulseUser|null Null is returned when no person is listed in this person column
28
     */
29 4
    public function getValue ()
30
    {
31 4
        return parent::getValue();
32
    }
33
34
    /**
35
     * Update the person in a person column
36
     *
37
     * @api
38
     *
39
     * @param int|PulseUser $user The new user that will be assigned to the person column
40
     *
41
     * @since 0.3.0 \InvalidArgumentException is now thrown
42
     * @since 0.1.0
43
     *
44
     * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object
45
     */
46 7
    public function updateValue ($user)
47
    {
48 7
        $user       = PulseUser::_castToInt($user);
49 2
        $url        = sprintf("%s/%d/columns/%s/person.json", self::apiEndpoint(), $this->board_id, $this->column_id);
50
        $postParams = [
51 2
            "pulse_id" => $this->pulse_id,
52 2
            "user_id"  => $user
53
        ];
54
55 2
        $result = self::sendPut($url, $postParams);
56 2
        $this->setValue($result);
57 2
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 4
    protected function isNullValue ()
63
    {
64
        // Thank you very much, DaPulse. Changing the return type of an API endpoint clearly does not break any existing
65
        // code. Check to see if an invalid user is returned to be able to return null
66
67 4
        return parent::isNullValue() ||
68 3
               (is_array($this->jsonResponse) &&
69 3
                isset($this->jsonResponse['value']) && is_array($this->jsonResponse['value']) &&
70 4
                isset($this->jsonResponse['value']['id']) && $this->jsonResponse['value']['id'] === 0);
71
    }
72
73 3
    protected function setValue ($response)
74
    {
75 3
        $this->column_value = new PulseUser($response["value"]["id"]);
76
    }
77
}