Passed
Push — master ( 4f5b3b...1c5d75 )
by Darko
09:48
created

CollectSystemMetrics   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 1
b 0
f 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 3
A __construct() 0 4 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Services\SystemMetricsService;
0 ignored issues
show
Bug introduced by
The type App\Services\SystemMetricsService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Console\Command;
7
8
class CollectSystemMetrics extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'metrics:collect {--cleanup : Clean up old metrics}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Collect and store current system metrics (CPU and RAM usage)';
23
24
    protected SystemMetricsService $metricsService;
25
26
    /**
27
     * Create a new command instance.
28
     */
29
    public function __construct(SystemMetricsService $metricsService)
30
    {
31
        parent::__construct();
32
        $this->metricsService = $metricsService;
33
    }
34
35
    /**
36
     * Execute the console command.
37
     */
38
    public function handle(): int
39
    {
40
        try {
41
            if ($this->option('cleanup')) {
42
                $this->info('Cleaning up old metrics...');
43
                $deleted = $this->metricsService->cleanupOldMetrics();
44
                $this->info("Deleted {$deleted} old metric records.");
45
            }
46
47
            $this->info('Collecting system metrics...');
48
            $this->metricsService->collectMetrics();
49
            $this->info('System metrics collected successfully.');
50
51
            return Command::SUCCESS;
52
        } catch (\Exception $e) {
53
            $this->error('Failed to collect system metrics: '.$e->getMessage());
54
55
            return Command::FAILURE;
56
        }
57
    }
58
}
59