This commit is contained in:
Marcel Pociot 2018-11-28 09:53:03 +01:00
parent 37ce35eb2f
commit c58027d882
2 changed files with 14 additions and 11 deletions

View File

@ -6,10 +6,18 @@ use Illuminate\Support\Collection;
class ConfigClientProvider implements ClientProvider class ConfigClientProvider implements ClientProvider
{ {
/** @var Collection */
protected $clients;
public function __construct()
{
$this->clients = collect(config('websockets.clients'));
}
/** @return array[BeyondCode\LaravelWebSockets\ClientProviders\Client] */ /** @return array[BeyondCode\LaravelWebSockets\ClientProviders\Client] */
public function all(): array public function all(): array
{ {
return $this->allClients() return $this->clients
->map(function ($client) { ->map(function ($client) {
return $this->instanciate($client); return $this->instanciate($client);
}) })
@ -18,7 +26,7 @@ class ConfigClientProvider implements ClientProvider
public function findByAppId(int $appId): ?Client public function findByAppId(int $appId): ?Client
{ {
$clientAttributes = $this $clientAttributes = $this
->allClients() ->clients
->firstWhere('app_id', $appId); ->firstWhere('app_id', $appId);
return $this->instanciate($clientAttributes); return $this->instanciate($clientAttributes);
@ -27,17 +35,12 @@ class ConfigClientProvider implements ClientProvider
public function findByAppKey(string $appKey): ?Client public function findByAppKey(string $appKey): ?Client
{ {
$clientAttributes = $this $clientAttributes = $this
->allClients() ->clients
->firstWhere('app_key', $appKey); ->firstWhere('app_key', $appKey);
return $this->instanciate($clientAttributes); return $this->instanciate($clientAttributes);
} }
protected function allClients(): Collection
{
return collect(config('websockets.clients'));
}
protected function instanciate(?array $clientAttributes): ?Client protected function instanciate(?array $clientAttributes): ?Client
{ {
if (! $clientAttributes) { if (! $clientAttributes) {

View File

@ -54,18 +54,18 @@ class ChannelManager
/** /**
* Remove the connection from all channels. * 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. * 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() ->reject->hasConnections()
->each(function (Channel $channel, string $channelId) use ($connection) { ->each(function (Channel $channel, string $channelId) use ($connection) {
unset($this->channels[$connection->client->appId][$channelId]); 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]); unset($this->channels[$connection->client->appId]);
}; };
} }