diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index 4b14d42..4985ed9 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -57,7 +57,7 @@ class HttpStatisticsLogger implements StatisticsLogger protected function findOrMakeStatisticForAppId($appId): Statistic { - if (! isset($this->statistics[$appId])) { + if (!isset($this->statistics[$appId])) { $this->statistics[$appId] = new Statistic($appId); } @@ -68,23 +68,19 @@ class HttpStatisticsLogger implements StatisticsLogger { foreach ($this->statistics as $appId => $statistic) { - if (! $statistic->isEnabled()) { + if (!$statistic->isEnabled()) { continue; } - $this->browser + $this + ->browser ->post( action([WebSocketStatisticsEntriesController::class, 'store']), ['Content-Type' => 'application/json'], stream_for(json_encode($statistic->toArray())) ); - // Reset connection and message count - $currentConnectionCount = collect($this->channelManager->getChannels($appId)) - ->sum(function ($channel) { - return count($channel->getSubscribedConnections()); - }); - + $currentConnectionCount = $this->channelManager->getConnectionCount($appId); $statistic->reset($currentConnectionCount); } } diff --git a/src/WebSockets/Channels/ChannelManager.php b/src/WebSockets/Channels/ChannelManager.php index fa77442..9c7c265 100644 --- a/src/WebSockets/Channels/ChannelManager.php +++ b/src/WebSockets/Channels/ChannelManager.php @@ -46,6 +46,12 @@ class ChannelManager return $this->channels[$appId] ?? []; } + public function getConnectionCount(string $appId): int + { + return collect($this->getChannels($appId)) + ->sum->getSubscribedConnections(); + } + public function removeFromAllChannels(ConnectionInterface $connection) { if (!isset($connection->app)) {