From 84dad775e509a408bd78f05968ec2f4520a9a0b3 Mon Sep 17 00:00:00 2001 From: freek Date: Sat, 1 Dec 2018 12:26:08 +0100 Subject: [PATCH] replace channel id with channel name --- src/Dashboard/DashboardLogger.php | 18 +++++------ .../Controllers/TriggerEventController.php | 8 ++--- src/WebSockets/Channels/Channel.php | 6 ++-- src/WebSockets/Channels/ChannelManager.php | 30 +++++++++---------- tests/TestCase.php | 4 +-- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Dashboard/DashboardLogger.php b/src/Dashboard/DashboardLogger.php index 7816a44..39272d8 100644 --- a/src/Dashboard/DashboardLogger.php +++ b/src/Dashboard/DashboardLogger.php @@ -28,18 +28,18 @@ class DashboardLogger ]); } - public static function occupied(ConnectionInterface $connection, string $channelId) + public static function occupied(ConnectionInterface $connection, string $channelName) { static::log($connection->client->appId, static::TYPE_OCCUPIED, [ - 'details' => "Channel: {$channelId}", + 'details' => "Channel: {$channelName}", ]); } - public static function subscribed(ConnectionInterface $connection, string $channelId) + public static function subscribed(ConnectionInterface $connection, string $channelName) { static::log($connection->client->appId, static::TYPE_SUBSCRIBED, [ 'socketId' => $connection->socketId, - 'details' => "Channel: {$channelId}", + 'details' => "Channel: {$channelName}", ]); } @@ -59,10 +59,10 @@ class DashboardLogger ]); } - public static function vacated(ConnectionInterface $connection, string $channelId) + public static function vacated(ConnectionInterface $connection, string $channelName) { static::log($connection->client->appId, static::TYPE_VACATED, [ - 'details' => "Channel: {$channelId}", + 'details' => "Channel: {$channelName}", ]); } @@ -76,13 +76,13 @@ class DashboardLogger public static function log($appId, string $type, array $attributes = []) { - $channelId = static::LOG_CHANNEL_PREFIX . $type; + $channelName = static::LOG_CHANNEL_PREFIX . $type; - $channel = app(ChannelManager::class)->find($appId, $channelId); + $channel = app(ChannelManager::class)->find($appId, $channelName); optional($channel)->broadcast([ 'event' => 'log-message', - 'channel' => $channelId, + 'channel' => $channelName, 'data' => [ 'type' => $type, 'time' => strftime("%H:%M:%S") diff --git a/src/HttpApi/Controllers/TriggerEventController.php b/src/HttpApi/Controllers/TriggerEventController.php index ee57fd6..9a00176 100644 --- a/src/HttpApi/Controllers/TriggerEventController.php +++ b/src/HttpApi/Controllers/TriggerEventController.php @@ -11,18 +11,18 @@ class TriggerEventController extends Controller { $this->ensureValidSignature($request); - foreach ($request->json()->get('channels', []) as $channelId) { - $channel = $this->channelManager->find($request->appId, $channelId); + foreach ($request->json()->get('channels', []) as $channelName) { + $channel = $this->channelManager->find($request->appId, $channelName); optional($channel)->broadcastToEveryoneExcept([ - 'channel' => $channelId, + 'channel' => $channelName, 'event' => $request->json()->get('name'), 'data' => $request->json()->get('data'), ], $request->json()->get('socket_id')); DashboardLogger::apiMessage( $request->appId, - $channelId, + $channelName, $request->json()->get('name'), $request->json()->get('data') ); diff --git a/src/WebSockets/Channels/Channel.php b/src/WebSockets/Channels/Channel.php index 8ae7456..39da03c 100644 --- a/src/WebSockets/Channels/Channel.php +++ b/src/WebSockets/Channels/Channel.php @@ -11,14 +11,14 @@ use stdClass; class Channel { /** @var string */ - protected $channelId; + protected $channelName; /** @var \Ratchet\ConnectionInterface[] */ protected $subscriptions = []; - public function __construct(string $channelId) + public function __construct(string $channelName) { - $this->channelId = $channelId; + $this->channelId = $channelName; } public function hasConnections(): bool diff --git a/src/WebSockets/Channels/ChannelManager.php b/src/WebSockets/Channels/ChannelManager.php index b43ce41..b88dce1 100644 --- a/src/WebSockets/Channels/ChannelManager.php +++ b/src/WebSockets/Channels/ChannelManager.php @@ -12,29 +12,29 @@ class ChannelManager /** @var array */ protected $channels = []; - public function findOrCreate(string $appId, string $channelId): Channel - { - if (!isset($this->channels[$appId][$channelId])) { - $channelClass = $this->determineChannelClass($channelId); - - $this->channels[$appId][$channelId] = new $channelClass($channelId); - } +public function findOrCreate(string $appId, string $channelName): Channel +{ + if (!isset($this->channels[$appId][$channelName])) { + $channelClass = $this->determineChannelClass($channelName); - return $this->channels[$appId][$channelId]; + $this->channels[$appId][$channelName] = new $channelClass($channelName); } - public function find(string $appId, string $channelId): ?Channel + return $this->channels[$appId][$channelName]; +} + + public function find(string $appId, string $channelName): ?Channel { - return $this->channels[$appId][$channelId] ?? null; + return $this->channels[$appId][$channelName] ?? null; } - protected function determineChannelClass(string $channelId): string + protected function determineChannelClass(string $channelName): string { - if (starts_with($channelId, 'private-')) { + if (starts_with($channelName, 'private-')) { return PrivateChannel::class; } - if (starts_with($channelId, 'presence-')) { + if (starts_with($channelName, 'presence-')) { return PresenceChannel::class; } @@ -62,8 +62,8 @@ class ChannelManager */ collect(array_get($this->channels, $connection->client->appId, [])) ->reject->hasConnections() - ->each(function (Channel $channel, string $channelId) use ($connection) { - unset($this->channels[$connection->client->appId][$channelId]); + ->each(function (Channel $channel, string $channelName) use ($connection) { + unset($this->channels[$connection->client->appId][$channelName]); }); if (count(array_get($this->channels, $connection->client->appId, [])) === 0) { diff --git a/tests/TestCase.php b/tests/TestCase.php index 1f802ed..5b2188d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -76,9 +76,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase return $connection; } - protected function getChannel(ConnectionInterface $connection, string $channelId) + protected function getChannel(ConnectionInterface $connection, string $channelName) { - return $this->channelManager->findOrCreate($connection->client->appId, $channelId); + return $this->channelManager->findOrCreate($connection->client->appId, $channelName); } protected function markTestAsPassed()