1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Logger; |
4
|
|
|
|
5
|
|
|
use Clue\React\Buzz\Browser; |
6
|
|
|
use Ratchet\ConnectionInterface; |
7
|
|
|
use function GuzzleHttp\Psr7\stream_for; |
8
|
|
|
use BeyondCode\LaravelWebSockets\Apps\App; |
9
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Statistic; |
10
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
11
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; |
12
|
|
|
|
13
|
|
|
class HttpStatisticsLogger implements StatisticsLogger |
14
|
|
|
{ |
15
|
|
|
/** @var \BeyondCode\LaravelWebSockets\Statistics\Statistic[] */ |
16
|
|
|
protected $statistics = []; |
17
|
|
|
|
18
|
|
|
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ |
19
|
|
|
protected $channelManager; |
20
|
|
|
|
21
|
|
|
/** @var \Clue\React\Buzz\Browser */ |
22
|
|
|
protected $browser; |
23
|
|
|
|
24
|
|
|
public function __construct(ChannelManager $channelManager, Browser $browser) |
25
|
|
|
{ |
26
|
|
|
$this->channelManager = $channelManager; |
27
|
|
|
|
28
|
|
|
$this->browser = $browser; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function webSocketMessage(ConnectionInterface $connection) |
32
|
|
|
{ |
33
|
|
|
$this |
34
|
|
|
->findOrMakeStatisticForAppId($connection->app->id) |
|
|
|
|
35
|
|
|
->webSocketMessage(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function apiMessage($appId) |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
|
|
->findOrMakeStatisticForAppId($appId) |
42
|
|
|
->apiMessage(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function connection(ConnectionInterface $connection) |
46
|
|
|
{ |
47
|
|
|
$this |
48
|
|
|
->findOrMakeStatisticForAppId($connection->app->id) |
|
|
|
|
49
|
|
|
->connection(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function disconnection(ConnectionInterface $connection) |
53
|
|
|
{ |
54
|
|
|
$this |
55
|
|
|
->findOrMakeStatisticForAppId($connection->app->id) |
|
|
|
|
56
|
|
|
->disconnection(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function findOrMakeStatisticForAppId($appId): Statistic |
60
|
|
|
{ |
61
|
|
|
if (! isset($this->statistics[$appId])) { |
62
|
|
|
$this->statistics[$appId] = new Statistic($appId); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->statistics[$appId]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function save() |
69
|
|
|
{ |
70
|
|
|
foreach ($this->statistics as $appId => $statistic) { |
71
|
|
|
if (! $statistic->isEnabled()) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$postData = array_merge($statistic->toArray(), [ |
76
|
|
|
'secret' => App::findById($appId)->secret, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$this |
80
|
|
|
->browser |
81
|
|
|
->post( |
82
|
|
|
action([WebSocketStatisticsEntriesController::class, 'store']), |
83
|
|
|
['Content-Type' => 'application/json'], |
84
|
|
|
stream_for(json_encode($postData)) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$currentConnectionCount = $this->channelManager->getConnectionCount($appId); |
88
|
|
|
$statistic->reset($currentConnectionCount); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: