ListNotifications::getTableRows()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.0534
cc 4
eloc 13
nc 5
nop 1
1
<?php
2
3
namespace TheCodingMachine\ServerMonitorPluginNotificationByHost\Commands;
4
5
use Illuminate\Support\Collection;
6
use Spatie\ServerMonitor\Commands\BaseCommand;
7
use Spatie\ServerMonitor\Models\Check;
8
use Spatie\ServerMonitor\Models\Host;
9
use Symfony\Component\Console\Helper\TableSeparator;
10
11
class ListNotifications extends BaseCommand
12
{
13
    protected $signature = 'server-monitor:list-notifications
14
                            {--host= : Only show checks for certain host}';
15
16
    protected $description = 'List all notifications for host(s)';
17
18
    private $channels = [];
19
    private $notifications = [];
20
21
    public function handle()
22
    {
23
        if ($this->determineHostModelClass()::count() === 0) {
24
            return $this->info('There are no hosts configured');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->info('There are no hosts configured') targeting Illuminate\Console\Command::info() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
25
        }
26
27
        $this->channels = config('server-monitor-plugin-notification-by-host.notifications.channels');
28
        $this->notifications = config('server-monitor.notifications.notifications');
29
        $titles = ['Host'];
30
        foreach ($this->channels as $type => $channel) {
31
            $titles[] = $type;
32
        }
33
34
        $hosts = $this->determineHostModelClass()::all();
35
        if ($hostName = $this->option('host')) {
36
            $hosts = $hosts->filter(function (Host $host) use ($hostName) {
37
                return $host->name === $hostName;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Spatie\ServerMonitor\Models\Host. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
38
            });
39
        }
40
41
        $this->tableWithTitle(
42
            'Custom notification by host',
43
            $titles,
44
            $this->getTableRows($hosts)
45
        );
46
    }
47
48
    protected function getTableRows(Collection $hosts): array
49
    {
50
        $rows = [];
51
        $first = true;
52
        foreach ($hosts as $host) {
53
            if(!$first) {
54
                $rows[] = new TableSeparator();
55
            }
56
            /* @var $host Host */
57
            $rows[] = [$host->name];
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Spatie\ServerMonitor\Models\Host. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
58
59
            $hostNotifications = $host->getCustomProperty('notifications');
60
            if($hostNotifications) {
61
                $rows = $this->displayCustomNotification($hostNotifications, $rows);
62
            }
63
            else {
64
                $rows = $this->displayGlobalNotification($rows);
65
            }
66
            $first = false;
67
        }
68
        return $rows;
69
    }
70
71
    private function displayCustomNotification(array $hostNotifications, array $rows): array {
72
        foreach ($this->notifications as $notification => $global) {
73
            $rows[] = [''];
74
            $temp = [implode("\n", str_split($notification, 50))];
75
            foreach ($this->channels as $type => $channel) {
76
                if(isset($hostNotifications[$notification]) && array_search($type, $hostNotifications[$notification]['channels']) !== false) {
77
                    if(isset($hostNotifications['configuration'][$type])) {
78
                        $configurationList = [];
79
                        foreach ($hostNotifications['configuration'][$type] as $configurationAttribut => $configurationValue) {
80
                            $configurationList[] = $configurationAttribut.':'.(is_array($configurationValue)?implode(',', $configurationValue):$configurationValue);
81
                        }
82
                        $temp[] = implode("\n", $configurationList);
83
                    }
84
                    else {
85
                        $temp[] = 'Global';
86
                    }
87
                }
88
                else {
89
                    $temp[] = '';
90
                }
91
            }
92
            $rows[] = $temp;
93
        }
94
        return $rows;
95
    }
96
97
    private function displayGlobalNotification(array $rows): array{
98
99
        foreach ($this->notifications as $notification => $global) {
100
            $rows[] = [''];
101
            $temp = [implode("\n", str_split($notification, 50))];
102
            foreach ($this->channels as $type => $channel) {
103
                if (array_search($type, $global) !== false) {
104
                    $temp[] = 'Global';
105
                }
106
                else {
107
                    $temp[] = '';
108
                }
109
            }
110
            $rows[] = $temp;
111
        }
112
        return $rows;
113
    }
114
115
    protected function tableWithTitle(string $title, array $header, array $rows)
116
    {
117
        $this->info($title);
118
        $this->info(str_repeat('=', strlen($title)));
119
        $this->table($header, $rows);
120
        $this->comment('');
121
    }
122
}
123