AddNotificationByHost::channelChoice()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 6
nop 0
1
<?php
2
3
namespace TheCodingMachine\ServerMonitorPluginNotificationByHost\Commands;
4
5
use InvalidArgumentException;
6
use Spatie\ServerMonitor\Commands\BaseCommand;
7
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
8
9
class AddNotificationByHost extends BaseCommand
10
{
11
    protected $signature = 'server-monitor:add-notification-host';
12
13
    protected $description = 'Add notification by host';
14
15
    public function handle()
16
    {
17
        $this->info("Let's add notification by host!");
18
19
        $host = $this->hostChoice();
20
        $notificationsParameters = $this->channelChoice();
21
        $notificationsParameters = $this->configuration($host, $notificationsParameters);
22
        $host->setCustomProperty('notifications', $notificationsParameters);
23
        $host->save();
24
        $this->info("Host notification `{$host->name}` saved");
25
    }
26
27
    private function getAllHostNames() {
28
        $hosts = $this->determineHostModelClass()::all();
29
        $hostList = [];
30
        foreach ($hosts as $host) {
31
            $hostList[$host->id] = $host->name;
32
        }
33
        return $hostList;
34
    }
35
36
    private function askConfiguration($notifications) {
37
        $configure = [];
38
        foreach ($notifications as $channel) {
39
            $attributes = config('server-monitor-plugin-notification-by-host.notifications.channels.'.$channel);
40
            if($attributes) {
41
                foreach ($attributes as $attribute => $type) {
42
                    $configure[$channel][$attribute] = $this->ask("For channel `$channel` attribute `$attribute`?".($type == 'array'?" (values separate by ,)":""), false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $default of Illuminate\Console\Command::ask(). ( Ignorable by Annotation )

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

42
                    $configure[$channel][$attribute] = $this->ask("For channel `$channel` attribute `$attribute`?".($type == 'array'?" (values separate by ,)":""), /** @scrutinizer ignore-type */ false);
Loading history...
43
                    if($type == 'array') {
44
                        $configure[$channel][$attribute] = explode(',', $configure[$channel][$attribute]);
45
                    }
46
                    if(!$configure[$channel][$attribute]) {
47
                        unset($configure[$channel][$attribute]);
48
                    }
49
                }
50
                if(!$configure[$channel]) {
51
                    unset($configure[$channel]);
52
                }
53
            }
54
            else {
55
                $this->error("No configuration for ".$channel." in server-monitor-plugin-notification-by-host.php file configuration");
56
            }
57
        }
58
        return $configure;
59
    }
60
61
    private function hostChoice() {
62
        $this->line("----- Host choice -----");
63
        $hostList = $this->getAllHostNames();
64
        // Get the host object
65
        do {
66
            $hostName = $this->confirm('Do you know the host name?')
67
                ? $this->ask('Which host name?')
68
                : $this->choice('Which host?', $hostList);
69
70
            $host = $this->determineHostModelClass()::where('name', $hostName)->first();
71
            if(!$host) {
72
                $this->warn("This host `{$hostName}` doesn't exist");
73
            }
74
        } while(!$host);
75
76
        $this->info("Host: {$host->name}");
77
        return $host;
78
    }
79
80
    private function channelChoice() {
81
        $this->line("----- Channel configuration -----");
82
83
        $notifications = ['No channel'];
84
        foreach (config('server-monitor-plugin-notification-by-host.notifications.channels') as $channel => $config) {
85
            $notifications[] = $channel;
86
        }
87
        $notificationsParameters = [];
88
        foreach(config('server-monitor.notifications.notifications') as $class => $notification) {
89
            $this->line("Notification for ".$class);
90
91
            $channels = $this->choice('Which channel? (values separate by ,)', $notifications, 0, null, true);
92
            if($channels[0] != 'No channel') {
93
                $notificationsParameters[$class]['channels'] = $channels;
94
            }
95
        }
96
        return $notificationsParameters;
97
    }
98
99
    private function configuration($host, $notificationsParameters) {
100
        if(!$notificationsParameters) {
101
            $this->warn("The host {$host->name} has no notification configure !");
102
        }
103
        else {
104
105
            $this->line("----- Notification configuration -----");
106
107
            $this->line('Leave empty question, if you want to use the global configuration stored in server-monitor');
108
109
            $notificationsUsed = [];
110
            foreach ($notificationsParameters as $check) {
111
                if(isset($check['channels']) && $check['channels']) {
112
                    $notificationsUsed = array_unique(array_merge($notificationsUsed, $check['channels']), SORT_REGULAR);
113
                }
114
            }
115
116
            if($notificationsUsed) {
117
                $configure = $this->askConfiguration($notificationsUsed);
118
                $notificationsParameters['configuration'] = $configure;
119
            }
120
        }
121
        return $notificationsParameters;
122
    }
123
}
124