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) |
|
|
|
|
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
|
|
|
} |
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.