Completed
Push — master ( 39f807...391e9b )
by Vladimir
03:25
created

SubscribableObject   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getSubscribers() 0 6 1
A addSubscriber() 0 15 2
A removeSubscriber() 0 11 2
1
<?php
2
3
namespace allejo\DaPulse\Objects;
4
5
use allejo\DaPulse\PulseUser;
6
7
/**
8
 * A class representing a Pulse object which supports subscribers
9
 *
10
 * @internal
11
 * @package allejo\DaPulse\Objects
12
 * @since   0.1.0
13
 */
14
abstract class SubscribableObject extends ApiObject
15
{
16
    /**
17
     * The objects's unique identifier.
18
     *
19
     * @var int
20
     */
21
    protected $id;
22
23
    /**
24
     * The objects's unique identifier.
25
     *
26
     * @return int
27
     */
28
    public function getId ()
29
    {
30
        return $this->id;
31
    }
32
33
    /**
34
     * Get the users who are subscribed to this object
35
     *
36
     * @api
37
     *
38
     * @param  array $params
39
     *
40
     * @since  0.1.0
41
     *
42
     * @return PulseUser[]
43
     */
44
    public function getSubscribers ($params = array())
45
    {
46
        $url = sprintf("%s/%d/subscribers.json", $this::apiEndpoint(), $this->getId());
47
48
        return self::fetchJsonArrayToObjectArray($url, "PulseUser", $params);
49
    }
50
51
    /**
52
     * Subscriber a user to a object
53
     *
54
     * @api
55
     *
56
     * @param int|PulseUser $userId  The user that will be subscribed to the board
57
     * @param bool|null     $asAdmin Set to true if the user will be an admin of the board
58
     *
59
     * @since 0.1.0
60
     */
61
    public function addSubscriber ($userId, $asAdmin = NULL)
62
    {
63
        if ($userId instanceof PulseUser)
64
        {
65
            $userId = $userId->getId();
66
        }
67
68
        $url    = sprintf("%s/%d/subscribers.json", self::apiEndpoint(), $this->getId());
69
        $params = array(
70
            "user_id" => $userId
71
        );
72
73
        self::setIfNotNullOrEmpty($params, "as_admin", $asAdmin);
0 ignored issues
show
Documentation introduced by
$asAdmin is of type boolean|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
        self::sendPut($url, $params);
75
    }
76
77
    /**
78
     * Unsubscribe a user from this object
79
     *
80
     * @api
81
     *
82
     * @param int|PulseUser $userId The user that will be unsubscribed from the board
83
     *
84
     * @since 0.1.0
85
     */
86
    public function removeSubscriber ($userId)
87
    {
88
        if ($userId instanceof PulseUser)
89
        {
90
            $userId = $userId->getId();
91
        }
92
93
        $url = sprintf("%s/%d/subscribers/%d.json", self::apiEndpoint(), $this->getId(), $userId);
94
95
        self::sendDelete($url);
96
    }
97
}