Completed
Push — develop ( a05ff5...d1fb89 )
by Vladimir
03:27
created

PulseColumnPersonValue::updateValue()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 8.8571
cc 6
eloc 10
nc 3
nop 1
crap 6
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
        if ((!is_int($user) || (is_int($user) && $user < 1)) && !($user instanceof PulseUser))
54
        {
55
            throw new \InvalidArgumentException('$user is expected to be a positive integer or a PulseUser object');
56
        }
57
58 2
        $user       = ($user instanceof PulseUser) ? $user->getId() : $user;
59 2
        $url        = sprintf("%s/%d/columns/%s/person.json", self::apiEndpoint(), $this->board_id, $this->column_id);
60
        $postParams = array(
61 2
            "pulse_id" => $this->pulse_id,
62 2
            "user_id"  => $user
63
        );
64
65 2
        $result = self::sendPut($url, $postParams);
66 2
        $this->setValue($result);
67 2
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4
    protected function isNullValue ()
73
    {
74
        // Thank you very much, DaPulse. Changing the return type of an API endpoint clearly does not break any existing
75
        // code. Check to see if an invalid user is returned to be able to return null
76
77 4
        return parent::isNullValue() ||
78 3
               (is_array($this->jsonResponse) &&
79 3
                array_key_exists('value', $this->jsonResponse) &&
80 3
                is_array($this->jsonResponse['value']) &&
81 3
                array_key_exists('id', $this->jsonResponse['value']) &&
82 4
                $this->jsonResponse['value']['id'] === 0);
83
    }
84
85 3
    protected function setValue ($response)
86
    {
87 3
        $this->column_value = new PulseUser($response["value"]["id"]);
88
    }
89
}