diff --git a/src/Facades/StatisticsLogger.php b/src/Facades/StatisticsLogger.php index debb6a0..b3f5749 100644 --- a/src/Facades/StatisticsLogger.php +++ b/src/Facades/StatisticsLogger.php @@ -2,13 +2,19 @@ namespace BeyondCode\LaravelWebSockets\Facades; +use BeyondCode\LaravelWebSockets\Statistics\Logging\FakeStatisticsLogger; use Illuminate\Support\Facades\Facade; -/** @see \BeyondCode\LaravelWebSockets\Statistics\Logging\StatisticsLogger */ +/** @see \BeyondCode\LaravelWebSockets\Statistics\Logging\HttpStatisticsLogger */ class StatisticsLogger extends Facade { protected static function getFacadeAccessor() { return 'websockets.statisticslogger'; } + + public static function fake() + { + static::swap(new FakeStatisticsLogger()); + } } diff --git a/src/Statistics/Logging/FakeStatisticsLogger.php b/src/Statistics/Logging/FakeStatisticsLogger.php new file mode 100644 index 0000000..859c6d2 --- /dev/null +++ b/src/Statistics/Logging/FakeStatisticsLogger.php @@ -0,0 +1,42 @@ +channelManager = $channelManager; + + $this->client = $client; + } + + public function logWebSocketMessage(ConnectionInterface $connection) + { + $this->initializeStatistics($connection->app->id); + + $this->statistics[$connection->app->id]->logWebSocketMessage(); + } + + public function logApiMessage($appId) + { + $this->initializeStatistics($appId); + + $this->statistics[$appId]->logApiMessage(); + } + + public function logConnection(ConnectionInterface $connection) + { + $this->initializeStatistics($connection->app->id); + + $this->statistics[$connection->app->id]->logConnection(); + } + + public function logDisconnection(ConnectionInterface $connection) + { + $this->initializeStatistics($connection->app->id); + + $this->statistics[$connection->app->id]->logDisconnection(); + } + + protected function initializeStatistics($id) + { + if (!isset($this->statistics[$id])) { + $this->statistics[$id] = new Statistic($id); + } + } + + public function save() + { + foreach ($this->statistics as $appId => $statistic) { + if (! $statistic->isEnabled()) { + continue; + } + + $this->client->postAsync( + action([WebsocketStatisticsEntriesController::class, 'store']), + $statistic->toArray() + ); + + // Reset connection and message count + $currentConnectionCount = collect($this->channelManager->getChannels($appId)) + ->sum(function ($channel) { + return count($channel->getSubscribedConnections()); + }); + + $statistic->reset($currentConnectionCount); + } + } +} \ No newline at end of file diff --git a/src/Statistics/Logging/StatisticsLogger.php b/src/Statistics/Logging/StatisticsLogger.php index 82a186a..0ef8704 100644 --- a/src/Statistics/Logging/StatisticsLogger.php +++ b/src/Statistics/Logging/StatisticsLogger.php @@ -2,80 +2,17 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logging; -use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; -use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; -use GuzzleHttp\Client; use Ratchet\ConnectionInterface; -class StatisticsLogger +interface StatisticsLogger { - /** @var Statistic[] */ - protected $statistics = []; + public function logWebSocketMessage(ConnectionInterface $connection); - /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ - protected $channelManager; + public function logApiMessage($appId); - public function __construct(ChannelManager $channelManager, Client $client) - { - $this->channelManager = $channelManager; + public function logConnection(ConnectionInterface $connection); - $this->client = $client; - } + public function logDisconnection(ConnectionInterface $connection); - public function logWebSocketMessage(ConnectionInterface $connection) - { - $this->initializeStatistics($connection->app->id); - - $this->statistics[$connection->app->id]->logWebSocketMessage(); - } - - public function logApiMessage($appId) - { - $this->initializeStatistics($appId); - - $this->statistics[$appId]->logApiMessage(); - } - - public function logConnection(ConnectionInterface $connection) - { - $this->initializeStatistics($connection->app->id); - - $this->statistics[$connection->app->id]->logConnection(); - } - - public function logDisconnection(ConnectionInterface $connection) - { - $this->initializeStatistics($connection->app->id); - - $this->statistics[$connection->app->id]->logDisconnection(); - } - - protected function initializeStatistics($id) - { - if (!isset($this->statistics[$id])) { - $this->statistics[$id] = new Statistic($id); - } - } - - public function save() - { - foreach ($this->statistics as $appId => $statistic) { - if (! $statistic->isEnabled()) { - continue; - } - - $this->client->postAsync( - action([WebsocketStatisticsEntriesController::class, 'store']), - $statistic->toArray() - ); - - // Reset connection and message count - $currentConnectionCount = collect($this->channelManager->getChannels($appId)) - ->sum(function ($channel) { - return count($channel->getSubscribedConnections()); - }); - - $statistic->reset($currentConnectionCount); - } - } + public function save(); } \ No newline at end of file diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 06190ed..1f17a9d 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -8,7 +8,7 @@ use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize; use BeyondCode\LaravelWebSockets\Server\Router; use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; -use BeyondCode\LaravelWebSockets\Statistics\Logging\StatisticsLogger; +use BeyondCode\LaravelWebSockets\Statistics\Logging\HttpStatisticsLogger; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; use BeyondCode\LaravelWebSockets\Apps\AppProvider; diff --git a/tests/TestCase.php b/tests/TestCase.php index c6c1983..c6bada9 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Tests; +use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; @@ -29,6 +30,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase $this->channelManager = app(ChannelManager::class); Route::webSockets(); + + StatisticsLogger::fake(); } protected function getPackageProviders($app)