This commit is contained in:
freek 2018-12-03 23:45:18 +01:00
parent f6c9add25d
commit 5c93909f31
1 changed files with 20 additions and 18 deletions

View File

@ -11,13 +11,13 @@ use Ratchet\ConnectionInterface;
class HttpStatisticsLogger implements StatisticsLogger class HttpStatisticsLogger implements StatisticsLogger
{ {
/** @var Statistic[] */ /** @var \BeyondCode\LaravelWebSockets\Statistics\Statistic[] */
protected $statistics = []; protected $statistics = [];
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
protected $channelManager; protected $channelManager;
/** @var Browser */ /** @var \Clue\React\Buzz\Browser */
protected $browser; protected $browser;
public function __construct(ChannelManager $channelManager, Browser $browser) public function __construct(ChannelManager $channelManager, Browser $browser)
@ -29,44 +29,46 @@ class HttpStatisticsLogger implements StatisticsLogger
public function webSocketMessage(ConnectionInterface $connection) public function webSocketMessage(ConnectionInterface $connection)
{ {
$this->initializeStatistics($connection->app->id); $this
->findOrMakeStatisticForAppId($connection->app->id)
$this->statistics[$connection->app->id]->webSocketMessage(); ->webSocketMessage();
} }
public function apiMessage($appId) public function apiMessage($appId)
{ {
$this->initializeStatistics($appId); $this
->findOrMakeStatisticForAppId($appId)
$this->statistics[$appId]->apiMessage(); ->apiMessage();
} }
public function connection(ConnectionInterface $connection) public function connection(ConnectionInterface $connection)
{ {
$this->initializeStatistics($connection->app->id); $this
->findOrMakeStatisticForAppId($connection->app->id)
$this->statistics[$connection->app->id]->connection(); ->connection();
} }
public function disconnection(ConnectionInterface $connection) public function disconnection(ConnectionInterface $connection)
{ {
$this->initializeStatistics($connection->app->id); $this
->findOrMakeStatisticForAppId($connection->app->id)
$this->statistics[$connection->app->id]->disconnection(); ->disconnection();
} }
protected function initializeStatistics($id) protected function findOrMakeStatisticForAppId($appId): Statistic
{ {
if (!isset($this->statistics[$id])) { if (! isset($this->statistics[$appId])) {
$this->statistics[$id] = new Statistic($id); $this->statistics[$appId] = new Statistic($appId);
} }
return $this->statistics[$appId];
} }
public function save() public function save()
{ {
foreach ($this->statistics as $appId => $statistic) { foreach ($this->statistics as $appId => $statistic) {
if (!$statistic->isEnabled()) { if (! $statistic->isEnabled()) {
continue; continue;
} }