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

87 lines
2.5 KiB
PHP
Raw Normal View History

2018-12-03 13:44:26 +00:00
<?php
2018-12-03 13:45:50 +00:00
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
2018-12-03 13:44:26 +00:00
2018-12-03 22:19:46 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
2018-12-03 13:45:50 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
2018-12-03 13:44:26 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
2018-12-03 20:06:49 +00:00
use Clue\React\Buzz\Browser;
2018-12-03 20:41:41 +00:00
use function GuzzleHttp\Psr7\stream_for;
2018-12-03 13:44:26 +00:00
use Ratchet\ConnectionInterface;
class HttpStatisticsLogger implements StatisticsLogger
{
2018-12-03 22:45:18 +00:00
/** @var \BeyondCode\LaravelWebSockets\Statistics\Statistic[] */
2018-12-03 13:44:26 +00:00
protected $statistics = [];
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
protected $channelManager;
2018-12-03 22:45:18 +00:00
/** @var \Clue\React\Buzz\Browser */
2018-12-03 20:06:49 +00:00
protected $browser;
public function __construct(ChannelManager $channelManager, Browser $browser)
2018-12-03 13:44:26 +00:00
{
$this->channelManager = $channelManager;
2018-12-03 20:06:49 +00:00
$this->browser = $browser;
2018-12-03 13:44:26 +00:00
}
2018-12-03 15:23:20 +00:00
public function webSocketMessage(ConnectionInterface $connection)
2018-12-03 13:44:26 +00:00
{
2018-12-03 22:45:18 +00:00
$this
->findOrMakeStatisticForAppId($connection->app->id)
->webSocketMessage();
2018-12-03 13:44:26 +00:00
}
2018-12-03 15:23:20 +00:00
public function apiMessage($appId)
2018-12-03 13:44:26 +00:00
{
2018-12-03 22:45:18 +00:00
$this
->findOrMakeStatisticForAppId($appId)
->apiMessage();
2018-12-03 13:44:26 +00:00
}
2018-12-03 15:23:20 +00:00
public function connection(ConnectionInterface $connection)
2018-12-03 13:44:26 +00:00
{
2018-12-03 22:45:18 +00:00
$this
->findOrMakeStatisticForAppId($connection->app->id)
->connection();
2018-12-03 13:44:26 +00:00
}
2018-12-03 15:23:20 +00:00
public function disconnection(ConnectionInterface $connection)
2018-12-03 13:44:26 +00:00
{
2018-12-03 22:45:18 +00:00
$this
->findOrMakeStatisticForAppId($connection->app->id)
->disconnection();
2018-12-03 13:44:26 +00:00
}
2018-12-03 22:45:18 +00:00
protected function findOrMakeStatisticForAppId($appId): Statistic
2018-12-03 13:44:26 +00:00
{
2018-12-03 22:52:39 +00:00
if (!isset($this->statistics[$appId])) {
2018-12-03 22:45:18 +00:00
$this->statistics[$appId] = new Statistic($appId);
2018-12-03 13:44:26 +00:00
}
2018-12-03 22:45:18 +00:00
return $this->statistics[$appId];
2018-12-03 13:44:26 +00:00
}
public function save()
{
foreach ($this->statistics as $appId => $statistic) {
2018-12-03 20:41:41 +00:00
2018-12-03 22:52:39 +00:00
if (!$statistic->isEnabled()) {
2018-12-03 13:44:26 +00:00
continue;
}
2018-12-03 22:52:39 +00:00
$this
->browser
2018-12-03 20:06:49 +00:00
->post(
2018-12-03 22:19:46 +00:00
action([WebSocketStatisticsEntriesController::class, 'store']),
2018-12-03 20:41:41 +00:00
['Content-Type' => 'application/json'],
stream_for(json_encode($statistic->toArray()))
);
2018-12-03 13:44:26 +00:00
2018-12-03 22:52:39 +00:00
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
2018-12-03 13:44:26 +00:00
$statistic->reset($currentConnectionCount);
}
}
}