2018-11-21 11:13:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels;
|
|
|
|
|
|
|
|
|
|
|
2018-11-21 21:36:50 +00:00
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
|
2018-11-21 11:13:40 +00:00
|
|
|
class ChannelManager
|
|
|
|
|
{
|
2018-11-21 21:11:44 +00:00
|
|
|
/** @var array */
|
2018-11-21 11:13:40 +00:00
|
|
|
protected $channels = [];
|
|
|
|
|
|
2018-11-21 21:11:44 +00:00
|
|
|
/** @var string */
|
|
|
|
|
protected $appId;
|
|
|
|
|
|
|
|
|
|
public function findOrCreate(string $appId, string $channelId): Channel
|
2018-11-21 11:13:40 +00:00
|
|
|
{
|
2018-11-21 21:36:50 +00:00
|
|
|
if (!isset($this->channels[$appId][$channelId])) {
|
2018-11-21 21:11:44 +00:00
|
|
|
/**TODO: make this variable to go away */
|
2018-11-21 11:13:40 +00:00
|
|
|
$channelClass = $this->detectChannelClass($channelId);
|
2018-11-21 21:11:44 +00:00
|
|
|
|
2018-11-21 21:19:52 +00:00
|
|
|
$this->channels[$appId][$channelId] = new $channelClass($channelId);
|
2018-11-21 11:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-21 21:11:44 +00:00
|
|
|
return $this->channels[$appId][$channelId];
|
2018-11-21 11:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-21 21:11:44 +00:00
|
|
|
public function find(string $appId, string $channelId)
|
2018-11-21 14:42:04 +00:00
|
|
|
{
|
2018-11-21 21:11:44 +00:00
|
|
|
return $this->channels[$appId][$channelId] ?? null;
|
2018-11-21 14:42:04 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-21 21:36:50 +00:00
|
|
|
protected function detectChannelClass($channelId): string
|
2018-11-21 11:13:40 +00:00
|
|
|
{
|
|
|
|
|
if (starts_with($channelId, 'private-')) {
|
|
|
|
|
return PrivateChannel::class;
|
2018-11-21 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-21 21:36:50 +00:00
|
|
|
if (starts_with($channelId, 'presence-')) {
|
2018-11-21 11:13:40 +00:00
|
|
|
return PresenceChannel::class;
|
|
|
|
|
}
|
|
|
|
|
return Channel::class;
|
|
|
|
|
}
|
2018-11-21 21:36:50 +00:00
|
|
|
|
|
|
|
|
public function removeFromAllChannels(ConnectionInterface $connection)
|
|
|
|
|
{
|
|
|
|
|
collect($this->channels[$connection->appId])->each->unsubscribe($connection);
|
|
|
|
|
|
|
|
|
|
collect($this->channels[$connection->appId])
|
|
|
|
|
->reject->hasConnections()
|
|
|
|
|
->each(function (Channel $channel, string $channelId) use ($connection) {
|
|
|
|
|
unset($this->channels[$connection->appId][$channelId]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (count($this->channels[$connection->appId]) === 0) {
|
|
|
|
|
unset($this->channels[$connection->appId]);
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-11-21 11:13:40 +00:00
|
|
|
}
|