Passed
Pull Request — main (#57)
by mohsen
02:54
created

MonitorCommand::handle()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 17
c 2
b 0
f 0
nc 32
nop 0
dl 0
loc 26
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MohsenAbrishami\Stethoscope\Commands;
4
5
use Illuminate\Console\Command;
6
use MohsenAbrishami\Stethoscope\LogRecord\Facades\Record;
7
use MohsenAbrishami\Stethoscope\Services\Cpu;
8
use MohsenAbrishami\Stethoscope\Services\HardDisk;
9
use MohsenAbrishami\Stethoscope\Services\Memory;
10
use MohsenAbrishami\Stethoscope\Services\Network;
11
use MohsenAbrishami\Stethoscope\Services\WebServer;
12
13
class MonitorCommand extends Command
14
{
15
    public function __construct(Cpu $cpu, Memory $memory, Network $network, WebServer $webServer, HardDisk $hardDisk)
16
    {
17
        parent::__construct();
18
19
        $this->cpu = $cpu;
0 ignored issues
show
Bug Best Practice introduced by
The property cpu does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->memory = $memory;
0 ignored issues
show
Bug Best Practice introduced by
The property memory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        $this->network = $network;
0 ignored issues
show
Bug Best Practice introduced by
The property network does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
        $this->webServer = $webServer;
0 ignored issues
show
Bug Best Practice introduced by
The property webServer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
        $this->hardDisk = $hardDisk;
0 ignored issues
show
Bug Best Practice introduced by
The property hardDisk does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
    }
25
26
27
    /**
28
     * The name and signature of the console command.
29
     *
30
     * @var string
31
     */
32
    protected $signature = 'stethoscope:monitor';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'monitor memory usage, cpu usage, network connection and nginx status';
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return int
45
     */
46
    public function handle()
47
    {
48
        $resourceReports = [];
49
50
        $cpuUsage = $this->cpu->check();
51
        $memoryUsage = $this->memory->check();
52
        $networkStatus = $this->network->check();
53
        $hardDiskFreeSpace = $this->hardDisk->check();
54
        $webServerStatuses = $this->webServer->check();
55
56
        if (config('stethoscope.monitorable_resources.cpu') && $cpuUsage > config(('stethoscope.thresholds.cpu')))
57
            $resourceReports['cpu'] = $cpuUsage;
58
59
        if ($memoryUsage > config(('stethoscope.thresholds.memory')) && config('stethoscope.monitorable_resources.memory'))
60
            $resourceReports['memory'] = $memoryUsage;
61
62
        if (!$networkStatus && config('stethoscope.monitorable_resources.network'))
63
            $resourceReports['network'] = $networkStatus;
64
65
        if ($hardDiskFreeSpace < config(('stethoscope.thresholds.hard_disk')) && config('stethoscope.monitorable_resources.hard_disk'))
66
            $resourceReports['hardDisk'] = $hardDiskFreeSpace;
67
68
        if (config('stethoscope.monitorable_resources.web_server'))
69
            $resourceReports['webServer'] = $webServerStatuses;
70
71
        Record::record($resourceReports);
0 ignored issues
show
Bug introduced by
$resourceReports of type array is incompatible with the type integer expected by parameter $cpuUsage of MohsenAbrishami\Stethosc...acades\Record::record(). ( Ignorable by Annotation )

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

71
        Record::record(/** @scrutinizer ignore-type */ $resourceReports);
Loading history...
Bug introduced by
The call to MohsenAbrishami\Stethosc...acades\Record::record() has too few arguments starting with memoryUsage. ( Ignorable by Annotation )

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

71
        Record::/** @scrutinizer ignore-call */ 
72
                record($resourceReports);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
    }
73
}
74