Passed
Push — master ( 605a7f...c53e78 )
by Marcel
14:27 queued 03:25
created

DatabaseStore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 37
c 3
b 0
f 0
dl 0
loc 127
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getForGraph() 0 7 1
A getRecords() 0 10 1
A delete() 0 7 1
A getRawRecords() 0 8 1
A store() 0 3 1
A statisticToArray() 0 7 1
A statisticsToGraph() 0 14 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Statistics\Stores;
4
5
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
10
class DatabaseStore implements StatisticsStore
11
{
12
    /**
13
     * The model that will interact with the database.
14
     *
15
     * @var string
16
     */
17
    public static $model = \BeyondCode\LaravelWebSockets\Models\WebSocketsStatisticsEntry::class;
18
19
    /**
20
     * Store a new record in the database and return
21
     * the created instance.
22
     *
23
     * @param  array  $data
24
     * @return mixed
25
     */
26
    public static function store(array $data)
27
    {
28
        return static::$model::create($data);
29
    }
30
31
    /**
32
     * Delete records older than the given moment,
33
     * for a specific app id (if given), returning
34
     * the amount of deleted records.
35
     *
36
     * @param  \Carbon\Carbon  $moment
37
     * @param  string|int|null  $appId
38
     * @return int
39
     */
40
    public static function delete(Carbon $moment, $appId = null): int
41
    {
42
        return static::$model::where('created_at', '<', $moment->toDateTimeString())
43
            ->when(! is_null($appId), function ($query) use ($appId) {
44
                return $query->whereAppId($appId);
45
            })
46
            ->delete();
47
    }
48
49
    /**
50
     * Get the query result as eloquent collection.
51
     *
52
     * @param  callable  $processQuery
53
     * @return \Illuminate\Support\Collection
54
     */
55
    public function getRawRecords(callable $processQuery = null)
56
    {
57
        return static::$model::query()
58
            ->when(! is_null($processQuery), function ($query) use ($processQuery) {
59
                return call_user_func($processQuery, $query);
0 ignored issues
show
Bug introduced by
It seems like $processQuery can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
                return call_user_func(/** @scrutinizer ignore-type */ $processQuery, $query);
Loading history...
60
            }, function ($query) {
61
                return $query->latest()->limit(120);
62
            })->get();
63
    }
64
65
    /**
66
     * Get the results for a specific query.
67
     *
68
     * @param  callable  $processQuery
69
     * @param  callable  $processCollection
70
     * @return array
71
     */
72
    public function getRecords(callable $processQuery = null, callable $processCollection = null): array
73
    {
74
        return $this->getRawRecords($processQuery)
75
            ->when(! is_null($processCollection), function ($collection) use ($processCollection) {
76
                return call_user_func($processCollection, $collection);
0 ignored issues
show
Bug introduced by
It seems like $processCollection can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
                return call_user_func(/** @scrutinizer ignore-type */ $processCollection, $collection);
Loading history...
77
            })
78
            ->map(function (Model $statistic) {
79
                return $this->statisticToArray($statistic);
80
            })
81
            ->toArray();
82
    }
83
84
    /**
85
     * Get the results for a specific query into a
86
     * format that is easily to read for graphs.
87
     *
88
     * @param  callable  $processQuery
89
     * @param  callable  $processCollection
90
     * @return array
91
     */
92
    public function getForGraph(callable $processQuery = null, callable $processCollection = null): array
93
    {
94
        $statistics = collect(
95
            $this->getRecords($processQuery, $processCollection)
96
        );
97
98
        return $this->statisticsToGraph($statistics);
99
    }
100
101
    /**
102
     * Turn the statistic model to an array.
103
     *
104
     * @param  \Illuminate\Database\Eloquent\Model  $statistic
105
     * @return array
106
     */
107
    protected function statisticToArray(Model $statistic): array
108
    {
109
        return [
110
            'timestamp' => (string) $statistic->created_at,
111
            'peak_connections_count' => $statistic->peak_connections_count,
112
            'websocket_messages_count' => $statistic->websocket_messages_count,
113
            'api_messages_count' => $statistic->api_messages_count,
114
        ];
115
    }
116
117
    /**
118
     * Turn the statistics collection to an array used for graph.
119
     *
120
     * @param  \Illuminate\Support\Collection  $statistics
121
     * @return array
122
     */
123
    protected function statisticsToGraph(Collection $statistics): array
124
    {
125
        return [
126
            'peak_connections' => [
127
                'x' => $statistics->pluck('timestamp')->toArray(),
128
                'y' => $statistics->pluck('peak_connections_count')->toArray(),
129
            ],
130
            'websocket_messages_count' => [
131
                'x' => $statistics->pluck('timestamp')->toArray(),
132
                'y' => $statistics->pluck('websocket_messages_count')->toArray(),
133
            ],
134
            'api_messages_count' => [
135
                'x' => $statistics->pluck('timestamp')->toArray(),
136
                'y' => $statistics->pluck('api_messages_count')->toArray(),
137
            ],
138
        ];
139
    }
140
}
141