Passed
Push — main ( 5bbbb3...90ce0a )
by mohsen
03:58 queued 01:38
created

MonitorCommand::handle()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 19
c 3
b 0
f 0
nc 64
nop 0
dl 0
loc 29
rs 6.9666

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\Events\TroubleOccurred;
7
use MohsenAbrishami\Stethoscope\LogRecord\Facades\Record;
8
use MohsenAbrishami\Stethoscope\Services\Cpu;
9
use MohsenAbrishami\Stethoscope\Services\HardDisk;
10
use MohsenAbrishami\Stethoscope\Services\Memory;
11
use MohsenAbrishami\Stethoscope\Services\Network;
12
use MohsenAbrishami\Stethoscope\Services\WebServer;
13
14
class MonitorCommand extends Command
15
{
16
    public function __construct(Cpu $cpu, Memory $memory, Network $network, WebServer $webServer, HardDisk $hardDisk)
17
    {
18
        parent::__construct();
19
20
        $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...
21
        $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...
22
        $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...
23
        $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...
24
        $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...
25
    }
26
27
28
    /**
29
     * The name and signature of the console command.
30
     *
31
     * @var string
32
     */
33
    protected $signature = 'stethoscope:monitor';
34
35
    /**
36
     * The console command description.
37
     *
38
     * @var string
39
     */
40
    protected $description = 'monitor memory usage, cpu usage, network connection and nginx status';
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return int
46
     */
47
    public function handle()
48
    {
49
        $resourceReports = [];
50
51
        $cpuUsage = $this->cpu->check();
52
        $memoryUsage = $this->memory->check();
53
        $networkStatus = $this->network->check();
54
        $hardDiskFreeSpace = $this->hardDisk->check();
55
        $webServerStatuses = $this->webServer->check();
56
57
        if (config('stethoscope.monitorable_resources.cpu') && $cpuUsage > config(('stethoscope.thresholds.cpu')))
58
            $resourceReports['cpu'] = $cpuUsage;
59
60
        if ($memoryUsage > config(('stethoscope.thresholds.memory')) && config('stethoscope.monitorable_resources.memory'))
61
            $resourceReports['memory'] = $memoryUsage;
62
63
        if ($networkStatus == 'false' && config('stethoscope.monitorable_resources.network'))
64
            $resourceReports['network'] = $networkStatus;
65
66
        if ($hardDiskFreeSpace < config(('stethoscope.thresholds.hard_disk')) && config('stethoscope.monitorable_resources.hard_disk'))
67
            $resourceReports['hardDisk'] = $hardDiskFreeSpace;
68
69
        if ($webServerStatuses != 'active' && config('stethoscope.monitorable_resources.web_server'))
70
            $resourceReports['webServer'] = $webServerStatuses;
71
72
        Record::record($resourceReports);
0 ignored issues
show
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

72
        Record::/** @scrutinizer ignore-call */ 
73
                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...
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

72
        Record::record(/** @scrutinizer ignore-type */ $resourceReports);
Loading history...
73
74
        if (!empty($resourceReports))
75
            TroubleOccurred::dispatch($resourceReports);
76
    }
77
}
78