wip docblocks

This commit is contained in:
Alex Renoki 2020-08-14 08:42:17 +03:00
parent 5838acad30
commit 5997dd4df8
5 changed files with 105 additions and 80 deletions

View File

@ -73,6 +73,7 @@ class LocalClient implements ReplicationInterface
* @param string $channel * @param string $channel
* @param string $socketId * @param string $socketId
* @param string $data * @param string $data
* @return void
*/ */
public function joinChannel(string $appId, string $channel, string $socketId, string $data) public function joinChannel(string $appId, string $channel, string $socketId, string $data)
{ {
@ -86,10 +87,12 @@ class LocalClient implements ReplicationInterface
* @param string $appId * @param string $appId
* @param string $channel * @param string $channel
* @param string $socketId * @param string $socketId
* @return void
*/ */
public function leaveChannel(string $appId, string $channel, string $socketId) public function leaveChannel(string $appId, string $channel, string $socketId)
{ {
unset($this->channelData["$appId:$channel"][$socketId]); unset($this->channelData["$appId:$channel"][$socketId]);
if (empty($this->channelData["$appId:$channel"])) { if (empty($this->channelData["$appId:$channel"])) {
unset($this->channelData["$appId:$channel"]); unset($this->channelData["$appId:$channel"]);
} }
@ -106,7 +109,6 @@ class LocalClient implements ReplicationInterface
{ {
$members = $this->channelData["$appId:$channel"] ?? []; $members = $this->channelData["$appId:$channel"] ?? [];
// The data is expected as objects, so we need to JSON decode
$members = array_map(function ($user) { $members = array_map(function ($user) {
return json_decode($user); return json_decode($user);
}, $members); }, $members);

View File

@ -79,6 +79,7 @@ class RedisClient implements ReplicationInterface
* *
* @param string $redisChannel * @param string $redisChannel
* @param string $payload * @param string $payload
* @return void
*/ */
protected function onMessage(string $redisChannel, string $payload) protected function onMessage(string $redisChannel, string $payload)
{ {
@ -192,6 +193,7 @@ class RedisClient implements ReplicationInterface
* @param string $channel * @param string $channel
* @param string $socketId * @param string $socketId
* @param string $data * @param string $data
* @return void
*/ */
public function joinChannel(string $appId, string $channel, string $socketId, string $data) public function joinChannel(string $appId, string $channel, string $socketId, string $data)
{ {
@ -205,6 +207,7 @@ class RedisClient implements ReplicationInterface
* @param string $appId * @param string $appId
* @param string $channel * @param string $channel
* @param string $socketId * @param string $socketId
* @return void
*/ */
public function leaveChannel(string $appId, string $channel, string $socketId) public function leaveChannel(string $appId, string $channel, string $socketId)
{ {

View File

@ -52,6 +52,7 @@ interface ReplicationInterface
* @param string $channel * @param string $channel
* @param string $socketId * @param string $socketId
* @param string $data * @param string $data
* @return void
*/ */
public function joinChannel(string $appId, string $channel, string $socketId, string $data); public function joinChannel(string $appId, string $channel, string $socketId, string $data);
@ -62,6 +63,7 @@ interface ReplicationInterface
* @param string $appId * @param string $appId
* @param string $channel * @param string $channel
* @param string $socketId * @param string $socketId
* @return void
*/ */
public function leaveChannel(string $appId, string $channel, string $socketId); public function leaveChannel(string $appId, string $channel, string $socketId);

View File

@ -67,20 +67,18 @@ class Channel
{ {
$this->saveConnection($connection); $this->saveConnection($connection);
// Subscribe to broadcasted messages from the pub/sub backend
$this->replicator->subscribe($connection->app->id, $this->channelName);
$connection->send(json_encode([ $connection->send(json_encode([
'event' => 'pusher_internal:subscription_succeeded', 'event' => 'pusher_internal:subscription_succeeded',
'channel' => $this->channelName, 'channel' => $this->channelName,
])); ]));
$this->replicator->subscribe($connection->app->id, $this->channelName);
} }
public function unsubscribe(ConnectionInterface $connection) public function unsubscribe(ConnectionInterface $connection)
{ {
unset($this->subscribedConnections[$connection->socketId]); unset($this->subscribedConnections[$connection->socketId]);
// Unsubscribe from the pub/sub backend
$this->replicator->unsubscribe($connection->app->id, $this->channelName); $this->replicator->unsubscribe($connection->app->id, $this->channelName);
if (! $this->hasConnections()) { if (! $this->hasConnections()) {

View File

@ -22,22 +22,24 @@ class PresenceChannel extends Channel
protected $users = []; protected $users = [];
/** /**
* Get the members in the presence channel.
*
* @param string $appId * @param string $appId
* @return PromiseInterface * @return PromiseInterface
*/ */
public function getUsers(string $appId) public function getUsers(string $appId)
{ {
// Get the members list from the replication backend return $this->replicator->channelMembers($appId, $this->channelName);
return $this->replicator
->channelMembers($appId, $this->channelName);
} }
/** /**
* @link https://pusher.com/docs/pusher_protocol#presence-channel-events * Subscribe the connection to the channel.
* *
* @param ConnectionInterface $connection * @param ConnectionInterface $connection
* @param stdClass $payload * @param stdClass $payload
* @return void
* @throws InvalidSignature * @throws InvalidSignature
* @see https://pusher.com/docs/pusher_protocol#presence-channel-events
*/ */
public function subscribe(ConnectionInterface $connection, stdClass $payload) public function subscribe(ConnectionInterface $connection, stdClass $payload)
{ {
@ -49,8 +51,7 @@ class PresenceChannel extends Channel
$this->users[$connection->socketId] = $channelData; $this->users[$connection->socketId] = $channelData;
// Add the connection as a member of the channel // Add the connection as a member of the channel
$this->replicator $this->replicator->joinChannel(
->joinChannel(
$connection->app->id, $connection->app->id,
$this->channelName, $this->channelName,
$connection->socketId, $connection->socketId,
@ -62,7 +63,6 @@ class PresenceChannel extends Channel
$this->replicator $this->replicator
->channelMembers($connection->app->id, $this->channelName) ->channelMembers($connection->app->id, $this->channelName)
->then(function ($users) use ($connection) { ->then(function ($users) use ($connection) {
// Send the success event
$connection->send(json_encode([ $connection->send(json_encode([
'event' => 'pusher_internal:subscription_succeeded', 'event' => 'pusher_internal:subscription_succeeded',
'channel' => $this->channelName, 'channel' => $this->channelName,
@ -77,6 +77,12 @@ class PresenceChannel extends Channel
]); ]);
} }
/**
* Unsubscribe the connection from the Presence channel.
*
* @param ConnectionInterface $connection
* @return void
*/
public function unsubscribe(ConnectionInterface $connection) public function unsubscribe(ConnectionInterface $connection)
{ {
parent::unsubscribe($connection); parent::unsubscribe($connection);
@ -105,6 +111,8 @@ class PresenceChannel extends Channel
} }
/** /**
* Get the Presence Channel to array.
*
* @param string|null $appId * @param string|null $appId
* @return PromiseInterface * @return PromiseInterface
*/ */
@ -119,6 +127,12 @@ class PresenceChannel extends Channel
}); });
} }
/**
* Get the Presence channel data.
*
* @param array $users
* @return array
*/
protected function getChannelData(array $users): array protected function getChannelData(array $users): array
{ {
return [ return [
@ -130,6 +144,12 @@ class PresenceChannel extends Channel
]; ];
} }
/**
* Get the Presence Channel's users.
*
* @param array $users
* @return array
*/
protected function getUserIds(array $users): array protected function getUserIds(array $users): array
{ {
$userIds = array_map(function ($channelData) { $userIds = array_map(function ($channelData) {