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
|
|
|
* @var PulseUser[] |
25
|
|
|
*/ |
26
|
|
|
protected $subscribers; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The objects's unique identifier. |
30
|
|
|
* |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
25 |
|
public function getId () |
34
|
|
|
{ |
35
|
25 |
|
return $this->id; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the users who are subscribed to this object |
40
|
|
|
* |
41
|
|
|
* @api |
42
|
|
|
* |
43
|
|
|
* @param array $params |
44
|
|
|
* @param bool $forceFetch When set to true, this will force an API call to retrieve the subscribers. Otherwise, |
45
|
|
|
* this'll return the cached subscribers. |
46
|
|
|
* |
47
|
|
|
* @since 0.1.0 |
48
|
|
|
* |
49
|
|
|
* @return PulseUser[] |
50
|
|
|
*/ |
51
|
|
|
public function getSubscribers ($params = array(), $forceFetch = false) |
52
|
|
|
{ |
53
|
|
|
if (is_null($this->subscribers) || $forceFetch) |
54
|
|
|
{ |
55
|
|
|
$url = sprintf("%s/%d/subscribers.json", $this::apiEndpoint(), $this->getId()); |
56
|
|
|
$this->subscribers = self::fetchAndCastToObjectArray($url, "PulseUser", $params); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
self::lazyCastAll($this->subscribers, 'PulseUser'); |
60
|
|
|
|
61
|
|
|
return $this->subscribers; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Subscriber a user to a object |
66
|
|
|
* |
67
|
|
|
* @api |
68
|
|
|
* |
69
|
|
|
* @param int|PulseUser $userId The user that will be subscribed to the board |
70
|
|
|
* @param bool|null $asAdmin Set to true if the user will be an admin of the board |
71
|
|
|
* |
72
|
|
|
* @since 0.1.0 |
73
|
|
|
*/ |
74
|
|
|
public function addSubscriber ($userId, $asAdmin = NULL) |
75
|
|
|
{ |
76
|
|
|
if ($userId instanceof PulseUser) |
77
|
|
|
{ |
78
|
|
|
$userId = $userId->getId(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$url = sprintf("%s/%d/subscribers.json", self::apiEndpoint(), $this->getId()); |
82
|
|
|
$params = array( |
83
|
|
|
"user_id" => $userId |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
self::setIfNotNullOrEmpty($params, "as_admin", $asAdmin); |
87
|
|
|
$newSubscriber = self::sendPut($url, $params); |
88
|
|
|
|
89
|
|
|
// Save the user to the local cache |
90
|
|
|
if (is_null($this->subscribers)) |
91
|
|
|
{ |
92
|
|
|
$this->getSubscribers(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->subscribers[] = new PulseUser($newSubscriber); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Unsubscribe a user from this object |
100
|
|
|
* |
101
|
|
|
* @api |
102
|
|
|
* |
103
|
|
|
* @param int|PulseUser $userId The user that will be unsubscribed from the board |
104
|
|
|
* |
105
|
|
|
* @since 0.1.0 |
106
|
|
|
*/ |
107
|
|
|
public function removeSubscriber ($userId) |
108
|
|
|
{ |
109
|
|
|
if ($userId instanceof PulseUser) |
110
|
|
|
{ |
111
|
|
|
$userId = $userId->getId(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$url = sprintf("%s/%d/subscribers/%d.json", self::apiEndpoint(), $this->getId(), $userId); |
115
|
|
|
|
116
|
|
|
self::sendDelete($url); |
117
|
|
|
|
118
|
|
|
// Remove the user from the local cache |
119
|
|
|
if (is_null($this->subscribers)) |
120
|
|
|
{ |
121
|
|
|
$this->getSubscribers(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
foreach ($this->subscribers as $key => $subscriber) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
if ($subscriber->getId() == $userId) |
127
|
|
|
{ |
128
|
|
|
unset($this->subscribers[$key]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.