Completed
Push — develop ( 3cf270...2733c9 )
by Vladimir
02:42
created

PulseColumnPersonValue::updateValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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