Completed
Pull Request — master (#447)
by Marcel
03:21 queued 01:37
created

MemoryStatisticsLogger::disconnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
4
5
use BeyondCode\LaravelWebSockets\Apps\App;
6
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
7
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
8
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
9
10
class MemoryStatisticsLogger implements StatisticsLogger
11
{
12
    /**
13
     * The list of stored statistics.
14
     *
15
     * @var array
16
     */
17
    protected $statistics = [];
18
19
    /**
20
     * The Channel manager.
21
     *
22
     * @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager
23
     */
24
    protected $channelManager;
25
26
    /**
27
     * The statistics driver instance.
28
     *
29
     * @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver
30
     */
31
    protected $driver;
32
33
    /**
34
     * Initialize the logger.
35
     *
36
     * @param  \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager  $channelManager
37
     * @param  \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver  $driver
38
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
39
     */
40
    public function __construct(ChannelManager $channelManager, StatisticsDriver $driver)
41
    {
42
        $this->channelManager = $channelManager;
43
        $this->driver = $driver;
44
    }
45
46
    /**
47
     * Handle the incoming websocket message.
48
     *
49
     * @param  mixed  $appId
50
     * @return void
51
     */
52
    public function webSocketMessage($appId)
53
    {
54
        $this->findOrMakeStatisticForAppId($appId)
55
            ->webSocketMessage();
56
    }
57
58
    /**
59
     * Handle the incoming API message.
60
     *
61
     * @param  mixed  $appId
62
     * @return void
63
     */
64
    public function apiMessage($appId)
65
    {
66
        $this->findOrMakeStatisticForAppId($appId)
67
            ->apiMessage();
68
    }
69
70
    /**
71
     * Handle the new conection.
72
     *
73
     * @param  mixed  $appId
74
     * @return void
75
     */
76
    public function connection($appId)
77
    {
78
        $this->findOrMakeStatisticForAppId($appId)
79
            ->connection();
80
    }
81
82
    /**
83
     * Handle disconnections.
84
     *
85
     * @param  mixed  $appId
86
     * @return void
87
     */
88
    public function disconnection($appId)
89
    {
90
        $this->findOrMakeStatisticForAppId($appId)
91
            ->disconnection();
92
    }
93
94
    /**
95
     * Save all the stored statistics.
96
     *
97
     * @return void
98
     */
99
    public function save()
100
    {
101
        foreach ($this->statistics as $appId => $statistic) {
102
            if (! $statistic->isEnabled()) {
103
                continue;
104
            }
105
106
            $this->createRecord($statistic, $appId);
107
108
            $currentConnectionCount = $this->channelManager->getConnectionCount($appId);
109
110
            $statistic->reset($currentConnectionCount);
111
        }
112
    }
113
114
    /**
115
     * Find or create a defined statistic for an app.
116
     *
117
     * @param  mixed  $appId
118
     * @return Statistic
119
     */
120
    protected function findOrMakeStatisticForAppId($appId): Statistic
121
    {
122
        if (! isset($this->statistics[$appId])) {
123
            $this->statistics[$appId] = new Statistic($appId);
124
        }
125
126
        return $this->statistics[$appId];
127
    }
128
129
    /**
130
     * Get the saved statistics.
131
     *
132
     * @return array
133
     */
134
    public function getStatistics(): array
135
    {
136
        return $this->statistics;
137
    }
138
139
    /**
140
     * Create a new record using the Statistic Driver.
141
     *
142
     * @param  Statistic  $statistic
143
     * @param  mixed  $appId
144
     * @return void
145
     */
146
    public function createRecord(Statistic $statistic, $appId)
0 ignored issues
show
Unused Code introduced by
The parameter $appId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
    {
148
        $this->driver::create($statistic->toArray());
149
    }
150
}
151