2018-11-21 11:13:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:11:44 +00:00
|
|
|
if (! isset($this->channels[$appId][$channelId])) {
|
|
|
|
|
/**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 11:13:40 +00:00
|
|
|
protected function detectChannelClass($channelId) : string
|
|
|
|
|
{
|
|
|
|
|
if (starts_with($channelId, 'private-')) {
|
|
|
|
|
return PrivateChannel::class;
|
2018-11-21 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(starts_with($channelId, 'presence-')) {
|
2018-11-21 11:13:40 +00:00
|
|
|
return PresenceChannel::class;
|
|
|
|
|
}
|
|
|
|
|
return Channel::class;
|
|
|
|
|
}
|
|
|
|
|
}
|