From c58027d8823cf7f1c7776f2b615d8fc8376ab372 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Wed, 28 Nov 2018 09:53:03 +0100 Subject: [PATCH] wip --- src/ClientProviders/ConfigClientProvider.php | 19 +++++++++++-------- src/WebSockets/Channels/ChannelManager.php | 6 +++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/ClientProviders/ConfigClientProvider.php b/src/ClientProviders/ConfigClientProvider.php index 26074a9..b69c052 100644 --- a/src/ClientProviders/ConfigClientProvider.php +++ b/src/ClientProviders/ConfigClientProvider.php @@ -6,10 +6,18 @@ use Illuminate\Support\Collection; class ConfigClientProvider implements ClientProvider { + /** @var Collection */ + protected $clients; + + public function __construct() + { + $this->clients = collect(config('websockets.clients')); + } + /** @return array[BeyondCode\LaravelWebSockets\ClientProviders\Client] */ public function all(): array { - return $this->allClients() + return $this->clients ->map(function ($client) { return $this->instanciate($client); }) @@ -18,7 +26,7 @@ class ConfigClientProvider implements ClientProvider public function findByAppId(int $appId): ?Client { $clientAttributes = $this - ->allClients() + ->clients ->firstWhere('app_id', $appId); return $this->instanciate($clientAttributes); @@ -27,17 +35,12 @@ class ConfigClientProvider implements ClientProvider public function findByAppKey(string $appKey): ?Client { $clientAttributes = $this - ->allClients() + ->clients ->firstWhere('app_key', $appKey); return $this->instanciate($clientAttributes); } - protected function allClients(): Collection - { - return collect(config('websockets.clients')); - } - protected function instanciate(?array $clientAttributes): ?Client { if (! $clientAttributes) { diff --git a/src/WebSockets/Channels/ChannelManager.php b/src/WebSockets/Channels/ChannelManager.php index 9f4e22f..f52a1bb 100644 --- a/src/WebSockets/Channels/ChannelManager.php +++ b/src/WebSockets/Channels/ChannelManager.php @@ -54,18 +54,18 @@ class ChannelManager /** * Remove the connection from all channels. */ - collect($this->channels[$connection->client->appId])->each->unsubscribe($connection); + collect(array_get($this->channels, $connection->client->appId, []))->each->unsubscribe($connection); /** * Unset all channels that have no connections so we don't leak memory. */ - collect($this->channels[$connection->client->appId]) + 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]); }); - if (count($this->channels[$connection->client->appId]) === 0) { + if (count(array_get($this->channels, $connection->client->appId, [])) === 0) { unset($this->channels[$connection->client->appId]); }; }