CleanStatistics   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 3
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 22 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Console;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Database\Eloquent\Builder;
8
9
class CleanStatistics extends Command
10
{
11
    protected $signature = 'websockets:clean
12
                            {appId? : (optional) The app id that will be cleaned.}';
13
14
    protected $description = 'Clean up old statistics from the websocket log.';
15
16
    public function handle()
17
    {
18
        $this->comment('Cleaning WebSocket Statistics...');
19
20
        $appId = $this->argument('appId');
21
22
        $maxAgeInDays = config('websockets.statistics.delete_statistics_older_than_days');
23
24
        $cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s');
25
26
        $webSocketsStatisticsEntryModelClass = config('websockets.statistics.model');
27
28
        $amountDeleted = $webSocketsStatisticsEntryModelClass::where('created_at', '<', $cutOffDate)
29
            ->when(! is_null($appId), function (Builder $query) use ($appId) {
30
                $query->where('app_id', $appId);
31
            })
32
            ->delete();
33
34
        $this->info("Deleted {$amountDeleted} record(s) from the WebSocket statistics.");
35
36
        $this->comment('All done!');
37
    }
38
}
39