Completed
Push — master ( 2611b0...ff83e7 )
by Vladimir
11:15
created

PulseColumnPersonValue   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 20.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 14
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 14 14 3
A updateValue() 0 13 2
A isNullValue() 0 11 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * **Warning** This function may return a null value so ensure the returned value is not null before calling any
19
     * functions that belong to a PulseUser object.
20
     *
21
     * @api
22
     *
23
     * @since  0.1.0
24
     *
25
     * @return PulseUser|null Null is returned when no person is listed in this person column
26
     */
27 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...
28
    {
29
        if ($this->isNullValue())
30
        {
31
            return null;
32
        }
33
34
        if (!isset($this->column_value))
35
        {
36
            $this->column_value = new PulseUser($this->jsonResponse["value"]["id"]);
37
        }
38
39
        return $this->column_value;
40
    }
41
42
    /**
43
     * Update the person in a person column
44
     *
45
     * **Warning** Currently, there is no way of removing a person from a column; only replacing them.
46
     *
47
     * @api
48
     *
49
     * @param int|PulseUser $user The new user that will be assigned to the person column
50
     *
51
     * @since 0.1.0
52
     */
53
    public function updateValue ($user)
54
    {
55
        $user       = ($user instanceof PulseUser) ? $user->getId() : $user;
56
        $url        = sprintf("%s/%d/columns/%s/person.json", self::apiEndpoint(), $this->board_id, $this->column_id);
57
        $postParams = array(
58
            "pulse_id" => $this->pulse_id,
59
            "user_id"  => $user
60
        );
61
62
        self::sendPut($url, $postParams);
63
64
        $this->column_value = new PulseUser($user);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function isNullValue ()
71
    {
72
        // Thank you very much, DaPulse. Changing the return type of an API endpoint clearly does not break any existing
73
        // code. Check to see if an invalid user is returned to be able to return null
74
75
        $isNullUser = array_key_exists('value', $this->jsonResponse) &&
76
                      array_key_exists('id', $this->jsonResponse['value']) &&
77
                      $this->jsonResponse['value']['id'] === 0;
78
79
        return (parent::isNullValue() || $isNullUser);
80
    }
81
}