laravel-websockets/src/Statistics/Logging/Logger.php

70 lines
1.9 KiB
PHP
Raw Normal View History

2018-12-03 12:02:51 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface;
class Logger
{
/** @var Statistic[] */
protected $statistics = [];
/** @var ChannelManager */
protected $channelManager;
public function __construct(ChannelManager $channelManager)
{
$this->channelManager = $channelManager;
}
public function webSocketMessage(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->webSocketMessage();
}
public function apiMessage(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->apiMessage();
}
public function connection(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->connection();
}
public function disconnection(ConnectionInterface $connection)
{
$this->initializeStatistics($connection->app->id);
$this->statistics[$connection->app->id]->disconnection();
}
protected function initializeStatistics($id)
{
2018-12-03 12:27:45 +00:00
if (!isset($this->statistics[$id])) {
2018-12-03 12:02:51 +00:00
$this->statistics[$id] = new Statistic($id);
}
}
public function save()
{
foreach ($this->statistics as $appId => $statistic) {
// TODO: perform http request
// Reset connection and message count
$connections = Collection::make($this->channelManager->getChannels($appId))->sum(function ($channel) {
return count($channel->getSubscribedConnections());
});
$statistic->reset($connections);
}
}
}