Completed
Push — main ( 4c158d...f0eec3 )
by mohsen
16s queued 14s
created

ListenCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 39
c 2
b 1
f 0
dl 0
loc 86
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
C handle() 0 50 12
A __construct() 0 11 1
1
<?php
2
3
namespace MohsenAbrishami\Stethoscope\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
use MohsenAbrishami\Stethoscope\Events\TroubleOccurred;
8
use MohsenAbrishami\Stethoscope\Services\Cpu;
9
use MohsenAbrishami\Stethoscope\Services\Storage as StorageService;
10
use MohsenAbrishami\Stethoscope\Services\Memory;
11
use MohsenAbrishami\Stethoscope\Services\Network;
12
use MohsenAbrishami\Stethoscope\Services\WebServer;
13
use MohsenAbrishami\Stethoscope\Traits\MessageCreatorTrait;
14
15
class ListenCommand extends Command
16
{
17
    use MessageCreatorTrait;
18
19
    public function __construct(Cpu $cpu, Memory $memory, Network $network, WebServer $webServer, StorageService $storage)
20
    {
21
        parent::__construct();
22
23
        $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...
24
        $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...
25
        $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...
26
        $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...
27
        $this->storageService = $storage;
0 ignored issues
show
Bug Best Practice introduced by
The property storageService does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
29
        $this->storage = Storage::disk(config('stethoscope.storage.driver'));
0 ignored issues
show
Bug Best Practice introduced by
The property storage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
    }
31
32
    /**
33
     * The name and signature of the console command.
34
     *
35
     * @var string
36
     */
37
    protected $signature = 'stethoscope:listen {resources?*} {--notif}';
38
39
    /**
40
     * The console command description.
41
     *
42
     * @var string
43
     */
44
    protected $description = 'monitor memory usage, cpu usage, network connection and nginx status';
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return int
50
     */
51
    public function handle()
52
    {
53
        $resources = collect($this->argument('resources'));
54
55
        $resourcesIsEmpty = $resources->isEmpty();
56
57
        $logs = [];
58
        $logs['signature'] = $this->signature;
59
60
        $this->line(
61
            $this->timeMessage()
62
        );
63
64
        if ($resources->contains('cpu') || $resourcesIsEmpty) {
65
            $logs['cpu'] = $this->cpu->check();
66
            $this->info(
67
                $this->cpuMessage($logs['cpu'])
68
            );
69
        }
70
71
        if ($resources->contains('memory') || $resourcesIsEmpty) {
72
            $logs['memory'] = $this->memory->check();
73
            $this->info(
74
                $this->memoryMessage($logs['memory'])
75
            );
76
        }
77
78
        if ($resources->contains('network') || $resourcesIsEmpty) {
79
            $logs['network'] = $this->network->check();
80
            $this->info(
81
                $this->networkMessage($logs['network'])
82
            );
83
        }
84
85
        if ($resources->contains('web-server') || $resourcesIsEmpty) {
86
            $logs['webServer'] = $this->webServer->check();
87
            $this->info(
88
                $this->webServerMessage($logs['webServer'])
89
            );
90
        }
91
92
        if ($resources->contains('storage') || $resourcesIsEmpty) {
93
            $logs['storage'] = $this->storageService->check();
94
            $this->info(
95
                $this->storageMessage($logs['storage'])
96
            );
97
        }
98
        
99
        if ($this->option('notif')){
100
            TroubleOccurred::dispatch($logs);
101
        }
102
    }
103
}
104