Completed
Pull Request — master (#447)
by Alexandru
01:32
created

MemoryCollector   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 160
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A webSocketMessage() 0 5 1
A apiMessage() 0 5 1
A connection() 0 5 1
A disconnection() 0 5 1
A save() 0 20 4
A flush() 0 4 1
A getStatistics() 0 4 1
A getAppStatistics() 0 6 1
A findOrMake() 0 8 2
A createRecord() 0 4 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Statistics\Collectors;
4
5
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
6
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector;
7
use BeyondCode\LaravelWebSockets\Facades\StatisticsStore;
8
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
9
use React\Promise\FulfilledPromise;
10
use React\Promise\PromiseInterface;
11
12
class MemoryCollector implements StatisticsCollector
13
{
14
    /**
15
     * The list of stored statistics.
16
     *
17
     * @var array
18
     */
19
    protected $statistics = [];
20
21
    /**
22
     * The Channel manager.
23
     *
24
     * @var \BeyondCode\LaravelWebSockets\Contracts\ChannelManager
25
     */
26
    protected $channelManager;
27
28
    /**
29
     * Initialize the logger.
30
     *
31
     * @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...
32
     */
33
    public function __construct()
34
    {
35
        $this->channelManager = app(ChannelManager::class);
36
    }
37
38
    /**
39
     * Handle the incoming websocket message.
40
     *
41
     * @param  string|int  $appId
42
     * @return void
43
     */
44
    public function webSocketMessage($appId)
45
    {
46
        $this->findOrMake($appId)
47
            ->webSocketMessage();
48
    }
49
50
    /**
51
     * Handle the incoming API message.
52
     *
53
     * @param  string|int  $appId
54
     * @return void
55
     */
56
    public function apiMessage($appId)
57
    {
58
        $this->findOrMake($appId)
59
            ->apiMessage();
60
    }
61
62
    /**
63
     * Handle the new conection.
64
     *
65
     * @param  string|int  $appId
66
     * @return void
67
     */
68
    public function connection($appId)
69
    {
70
        $this->findOrMake($appId)
71
            ->connection();
72
    }
73
74
    /**
75
     * Handle disconnections.
76
     *
77
     * @param  string|int  $appId
78
     * @return void
79
     */
80
    public function disconnection($appId)
81
    {
82
        $this->findOrMake($appId)
83
            ->disconnection();
84
    }
85
86
    /**
87
     * Save all the stored statistics.
88
     *
89
     * @return void
90
     */
91
    public function save()
92
    {
93
        $this->getStatistics()->then(function ($statistics) {
94
            foreach ($statistics as $appId => $statistic) {
95
                if (! $statistic->isEnabled()) {
96
                    continue;
97
                }
98
99
                $this->createRecord($statistic, $appId);
100
101
                $this->channelManager
102
                    ->getGlobalConnectionsCount($appId)
103
                    ->then(function ($connections) use ($statistic) {
104
                        $statistic->reset(
105
                            is_null($connections) ? 0 : $connections
106
                        );
107
                    });
108
            }
109
        });
110
    }
111
112
    /**
113
     * Flush the stored statistics.
114
     *
115
     * @return void
116
     */
117
    public function flush()
118
    {
119
        $this->statistics = [];
120
    }
121
122
    /**
123
     * Get the saved statistics.
124
     *
125
     * @return PromiseInterface[array]
0 ignored issues
show
Documentation introduced by
The doc-type PromiseInterface[array] could not be parsed: Expected "]" at position 2, but found "array". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
126
     */
127
    public function getStatistics(): PromiseInterface
128
    {
129
        return new FulfilledPromise($this->statistics);
0 ignored issues
show
Deprecated Code introduced by
The class React\Promise\FulfilledPromise has been deprecated with message: 2.8.0 External usage of FulfilledPromise is deprecated, use `resolve()` instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
130
    }
131
132
    /**
133
     * Get the saved statistics for an app.
134
     *
135
     * @param  string|int  $appId
136
     * @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null]
0 ignored issues
show
Documentation introduced by
The doc-type PromiseInterface[\Beyond...tistics\Statistic|null] could not be parsed: Expected "]" at position 2, but found "\BeyondCode\LaravelWebSockets\Statistics\Statistic". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
137
     */
138
    public function getAppStatistics($appId): PromiseInterface
139
    {
140
        return new FulfilledPromise(
0 ignored issues
show
Deprecated Code introduced by
The class React\Promise\FulfilledPromise has been deprecated with message: 2.8.0 External usage of FulfilledPromise is deprecated, use `resolve()` instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
141
            $this->statistics[$appId] ?? null
142
        );
143
    }
144
145
    /**
146
     * Find or create a defined statistic for an app.
147
     *
148
     * @param  string|int  $appId
149
     * @return \BeyondCode\LaravelWebSockets\Statistics\Statistic
150
     */
151
    protected function findOrMake($appId): Statistic
152
    {
153
        if (! isset($this->statistics[$appId])) {
154
            $this->statistics[$appId] = Statistic::new($appId);
155
        }
156
157
        return $this->statistics[$appId];
158
    }
159
160
    /**
161
     * Create a new record using the Statistic Store.
162
     *
163
     * @param  \BeyondCode\LaravelWebSockets\Statistics\Statistic  $statistic
164
     * @param  mixed  $appId
165
     * @return void
166
     */
167
    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...
168
    {
169
        StatisticsStore::store($statistic->toArray());
170
    }
171
}
172