SubscribableObject   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 120
c 0
b 0
f 0
ccs 29
cts 29
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getSubscribers() 0 12 3
A addSubscriber() 0 22 3
A removeSubscriber() 0 22 5
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
 * A class representing a Pulse object which supports subscribers
14
 *
15
 * @internal
16
 * @package allejo\DaPulse\Objects
17
 * @since   0.1.0
18
 */
19
abstract class SubscribableObject extends ApiObject
20
{
21
    /**
22
     * @var PulseUser[]
23
     */
24
    protected $subscribers;
25
26
    /**
27
     * The objects's unique identifier.
28
     *
29
     * @return int
30
     */
31 52
    public function getId ()
32
    {
33 52
        return $this->id;
34
    }
35
36
    /**
37
     * Get the users who are subscribed to this object
38
     *
39
     * ```
40
     * array['page']     int - Page offset to fetch
41
     *      ['per_page'] int - Number of results to return per page
42
     *      ['offset']   int - Pad a number of results
43
     * ```
44
     *
45
     * @api
46
     *
47
     * @param  array $params     GET parameters passed to the URL (see above)
48
     * @param  bool  $forceFetch When set to true, this will force an API call to retrieve the subscribers. Otherwise,
49
     *                           this'll return the cached subscribers.
50
     *
51
     * @since  0.1.0
52
     *
53
     * @return PulseUser[]
54
     */
55 5
    public function getSubscribers ($params = [], $forceFetch = false)
56
    {
57 5
        if (is_null($this->subscribers) || $forceFetch)
58
        {
59 5
            $url               = sprintf("%s/%d/subscribers.json", $this::apiEndpoint(), $this->getId());
60 5
            $this->subscribers = self::fetchAndCastToObjectArray($url, "PulseUser", $params);
61
        }
62
63 5
        self::lazyCastAll($this->subscribers, 'PulseUser');
64
65 5
        return $this->subscribers;
66
    }
67
68
    /**
69
     * Subscriber a user to a object
70
     *
71
     * @api
72
     *
73
     * @param  int|PulseUser $userId  The user that will be subscribed to the board
74
     * @param  bool|null     $asAdmin Set to true if the user will be an admin of the board
75
     *
76
     * @since  0.3.0 A PulseUser object of the newly added subscriber is returned
77
     * @since  0.1.0
78
     *
79
     * @return PulseUser
80
     */
81 1
    public function addSubscriber ($userId, $asAdmin = null)
82
    {
83 1
        $userId = ($userId instanceof PulseUser) ? $userId->getId() : $userId;
84 1
        $url    = sprintf("%s/%d/subscribers.json", self::apiEndpoint(), $this->getId());
85
        $params = [
86 1
            "user_id" => $userId
87
        ];
88
89 1
        self::setIfNotNullOrEmpty($params, "as_admin", $asAdmin);
90 1
        $newSubscriber = self::sendPut($url, $params);
91
92
        // Save the user to the local cache
93 1
        if (is_null($this->subscribers))
94
        {
95 1
            $this->getSubscribers();
96
        }
97
98 1
        $user                = new PulseUser($newSubscriber);
99 1
        $this->subscribers[] = $user;
100
101 1
        return $user;
102
    }
103
104
    /**
105
     * Unsubscribe a user from this object
106
     *
107
     * @api
108
     *
109
     * @param  int|PulseUser $userId The user that will be unsubscribed from the board
110
     *
111
     * @since  0.3.0 A PulseUser object of the removed subscriber is returned
112
     * @since  0.1.0
113
     *
114
     * @return PulseUser
115
     */
116 1
    public function removeSubscriber ($userId)
117
    {
118 1
        $userId = ($userId instanceof PulseUser) ? $userId->getId() : $userId;
119 1
        $url    = sprintf("%s/%d/subscribers/%d.json", self::apiEndpoint(), $this->getId(), $userId);
120 1
        $result = self::sendDelete($url);
121
122
        // Remove the user from the local cache
123 1
        if (is_null($this->subscribers))
124
        {
125 1
            $this->getSubscribers();
126
        }
127
128 1
        foreach ($this->subscribers as $key => $subscriber)
0 ignored issues
show
Bug introduced by
The expression $this->subscribers of type null|array<integer,objec...ejo\DaPulse\PulseUser>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
129
        {
130 1
            if ($subscriber->getId() == $result['id'])
131
            {
132 1
                unset($this->subscribers[$key]);
133
            }
134
        }
135
136 1
        return (new PulseUser($result));
137
    }
138
}