|
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
|
|
|
protected function getUrl(): string |
|
69
|
|
|
{ |
|
70
|
|
|
$action = [WebSocketStatisticsEntriesController::class, 'store']; |
|
71
|
|
|
|
|
72
|
|
|
$url_override = config('websockets.statistics.base_url_override', null); |
|
73
|
|
|
|
|
74
|
|
|
if ($url_override !== null) { |
|
75
|
|
|
return $url_override . action($action, [], false); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return action($action); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function save() |
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($this->statistics as $appId => $statistic) { |
|
84
|
|
|
if (! $statistic->isEnabled()) { |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$postData = array_merge($statistic->toArray(), [ |
|
89
|
|
|
'secret' => App::findById($appId)->secret, |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
|
|
$this |
|
93
|
|
|
->browser |
|
94
|
|
|
->post( |
|
95
|
|
|
$this->getUrl(), |
|
96
|
|
|
['Content-Type' => 'application/json'], |
|
97
|
|
|
stream_for(json_encode($postData)) |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$currentConnectionCount = $this->channelManager->getConnectionCount($appId); |
|
101
|
|
|
$statistic->reset($currentConnectionCount); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
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: