laravel-websockets/src/LaravelEcho/Pusher/Channels/ChannelManager.php

34 lines
871 B
PHP
Raw Normal View History

2018-11-21 11:13:40 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels;
class ChannelManager
{
protected $channels = [];
2018-11-21 20:23:25 +00:00
public function findOrCreate(string $channelId): Channel
2018-11-21 11:13:40 +00:00
{
if (! isset($this->channels[$channelId])) {
$channelClass = $this->detectChannelClass($channelId);
$this->channels[$channelId] = new $channelClass($channelId);
}
return $this->channels[$channelId];
}
2018-11-21 14:42:04 +00:00
public function find(string $channelId)
{
return $this->channels[$channelId] ?? null;
}
2018-11-21 11:13:40 +00:00
protected function detectChannelClass($channelId) : string
{
if (starts_with($channelId, 'private-')) {
return PrivateChannel::class;
} elseif(starts_with($channelId, 'presence-')) {
return PresenceChannel::class;
}
return Channel::class;
}
}