Statistic   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 203
rs 10
wmc 15

14 Methods

Rating   Name   Duplication   Size   Complexity  
A webSocketMessage() 0 3 1
A connection() 0 5 1
A reset() 0 6 1
A __construct() 0 3 1
A new() 0 3 1
A apiMessage() 0 3 1
A disconnection() 0 5 1
A setPeakConnectionsCount() 0 5 1
A isEnabled() 0 3 1
A setWebSocketMessagesCount() 0 5 1
A toArray() 0 7 1
A setCurrentConnectionsCount() 0 5 1
A setApiMessagesCount() 0 5 1
A shouldHaveTracesRemoved() 0 3 2
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Statistics;
4
5
use BeyondCode\LaravelWebSockets\Apps\App;
6
7
class Statistic
8
{
9
    /**
10
     * The app id.
11
     *
12
     * @var mixed
13
     */
14
    protected $appId;
15
16
    /**
17
     * The current connections count ticker.
18
     *
19
     * @var int
20
     */
21
    protected $currentConnectionsCount = 0;
22
23
    /**
24
     * The peak connections count ticker.
25
     *
26
     * @var int
27
     */
28
    protected $peakConnectionsCount = 0;
29
30
    /**
31
     * The websockets connections count ticker.
32
     *
33
     * @var int
34
     */
35
    protected $webSocketMessagesCount = 0;
36
37
    /**
38
     * The api messages connections count ticker.
39
     *
40
     * @var int
41
     */
42
    protected $apiMessagesCount = 0;
43
44
    /**
45
     * Create a new statistic.
46
     *
47
     * @param  string|int  $appId
48
     * @return void
49
     */
50
    public function __construct($appId)
51
    {
52
        $this->appId = $appId;
53
    }
54
55
    /**
56
     * Create a new statistic instance.
57
     *
58
     * @param  string|int  $appId
59
     * @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
60
     */
61
    public static function new($appId)
62
    {
63
        return new static($appId);
64
    }
65
66
    /**
67
     * Set the current connections count.
68
     *
69
     * @param  int  $currentConnectionsCount
70
     * @return $this
71
     */
72
    public function setCurrentConnectionsCount(int $currentConnectionsCount)
73
    {
74
        $this->currentConnectionsCount = $currentConnectionsCount;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Set the peak connections count.
81
     *
82
     * @param  int  $peakConnectionsCount
83
     * @return $this
84
     */
85
    public function setPeakConnectionsCount(int $peakConnectionsCount)
86
    {
87
        $this->peakConnectionsCount = $peakConnectionsCount;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Set the peak connections count.
94
     *
95
     * @param  int  $webSocketMessagesCount
96
     * @return $this
97
     */
98
    public function setWebSocketMessagesCount(int $webSocketMessagesCount)
99
    {
100
        $this->webSocketMessagesCount = $webSocketMessagesCount;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Set the peak connections count.
107
     *
108
     * @param  int  $apiMessagesCount
109
     * @return $this
110
     */
111
    public function setApiMessagesCount(int $apiMessagesCount)
112
    {
113
        $this->apiMessagesCount = $apiMessagesCount;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Check if the app has statistics enabled.
120
     *
121
     * @return bool
122
     */
123
    public function isEnabled(): bool
124
    {
125
        return App::findById($this->appId)->statisticsEnabled;
126
    }
127
128
    /**
129
     * Handle a new connection increment.
130
     *
131
     * @return void
132
     */
133
    public function connection()
134
    {
135
        $this->currentConnectionsCount++;
136
137
        $this->peakConnectionsCount = max($this->currentConnectionsCount, $this->peakConnectionsCount);
138
    }
139
140
    /**
141
     * Handle a disconnection decrement.
142
     *
143
     * @return void
144
     */
145
    public function disconnection()
146
    {
147
        $this->currentConnectionsCount--;
148
149
        $this->peakConnectionsCount = max($this->currentConnectionsCount, $this->peakConnectionsCount);
150
    }
151
152
    /**
153
     * Handle a new websocket message.
154
     *
155
     * @return void
156
     */
157
    public function webSocketMessage()
158
    {
159
        $this->webSocketMessagesCount++;
160
    }
161
162
    /**
163
     * Handle a new api message.
164
     *
165
     * @return void
166
     */
167
    public function apiMessage()
168
    {
169
        $this->apiMessagesCount++;
170
    }
171
172
    /**
173
     * Reset all the connections to a specific count.
174
     *
175
     * @param  int  $currentConnectionsCount
176
     * @return void
177
     */
178
    public function reset(int $currentConnectionsCount)
179
    {
180
        $this->currentConnectionsCount = $currentConnectionsCount;
181
        $this->peakConnectionsCount = max(0, $currentConnectionsCount);
182
        $this->webSocketMessagesCount = 0;
183
        $this->apiMessagesCount = 0;
184
    }
185
186
    /**
187
     * Check if the current statistic entry is empty. This means
188
     * that the statistic entry can be easily deleted if no activity
189
     * occured for a while.
190
     *
191
     * @return bool
192
     */
193
    public function shouldHaveTracesRemoved(): bool
194
    {
195
        return $this->currentConnectionsCount === 0 && $this->peakConnectionsCount === 0;
196
    }
197
198
    /**
199
     * Transform the statistic to array.
200
     *
201
     * @return array
202
     */
203
    public function toArray()
204
    {
205
        return [
206
            'app_id' => $this->appId,
207
            'peak_connections_count' => $this->peakConnectionsCount,
208
            'websocket_messages_count' => $this->webSocketMessagesCount,
209
            'api_messages_count' => $this->apiMessagesCount,
210
        ];
211
    }
212
}
213