Passed
Pull Request — main (#145)
by mohsen
18:05
created

ListenCommand::handle()   B

Complexity

Conditions 11
Paths 32

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 11
eloc 19
c 2
b 1
f 0
nc 32
nop 0
dl 0
loc 37
rs 7.3166

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 Illuminate\Support\Facades\Storage;
7
use MohsenAbrishami\Stethoscope\Services\Cpu;
8
use MohsenAbrishami\Stethoscope\Services\storage;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use MohsenAbrishami\Stethoscope\Services\storage as storage because the name is already in use
Loading history...
9
use MohsenAbrishami\Stethoscope\Services\Memory;
10
use MohsenAbrishami\Stethoscope\Services\Network;
11
use MohsenAbrishami\Stethoscope\Services\WebServer;
12
use MohsenAbrishami\Stethoscope\Traits\MessageCreatorTrait;
13
14
class ListenCommand extends Command
15
{
16
    use MessageCreatorTrait;
17
18
    public function __construct(Cpu $cpu, Memory $memory, Network $network, WebServer $webServer, storage $storage)
19
    {
20
        parent::__construct();
21
22
        $this->cpu = $cpu;
23
        $this->memory = $memory;
24
        $this->network = $network;
25
        $this->webServer = $webServer;
26
        $this->storage = $storage;
27
28
        $this->storage = Storage::disk(config('stethoscope.storage.driver'));
29
    }
30
31
    /**
32
     * The name and signature of the console command.
33
     *
34
     * @var string
35
     */
36
    protected $signature = 'stethoscope:listen {resources?*}';
37
38
    /**
39
     * The console command description.
40
     *
41
     * @var string
42
     */
43
    protected $description = 'monitor memory usage, cpu usage, network connection and nginx status';
44
45
    /**
46
     * Execute the console command.
47
     *
48
     * @return int
49
     */
50
    public function handle()
51
    {
52
        $resources = collect($this->argument('resources'));
53
54
        $resourcesIsEmpty = $resources->isEmpty();
55
56
        $this->line(
57
            $this->timeMessage()
58
        );
59
60
        if ($resources->contains('cpu') || $resourcesIsEmpty) {
61
            $this->info(
62
                $this->cpuMessage($this->cpu->check())
63
            );
64
        }
65
66
        if ($resources->contains('memory') || $resourcesIsEmpty) {
67
            $this->info(
68
                $this->memoryMessage($this->memory->check())
69
            );
70
        }
71
72
        if ($resources->contains('network') || $resourcesIsEmpty) {
73
            $this->info(
74
                $this->networkMessage($this->network->check())
75
            );
76
        }
77
78
        if ($resources->contains('web-server') || $resourcesIsEmpty) {
79
            $this->info(
80
                $this->webServerMessage($this->webServer->check())
81
            );
82
        }
83
84
        if ($resources->contains('hdd') || $resourcesIsEmpty) {
85
            $this->info(
86
                $this->storageMessage($this->storage->check())
87
            );
88
        }
89
    }
90
}
91