Notifiable::getHost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace TheCodingMachine\ServerMonitorPluginNotificationByHost\Notifications;
4
5
6
class Notifiable extends \Spatie\ServerMonitor\Notifications\Notifiable
7
{
8
    public function routeNotificationForMail(): ?array
9
    {
10
        if($notification = $this->getSpecificConfiguration('mail')) {
11
            return $notification;
12
        }
13
        return parent::routeNotificationForMail();
14
    }
15
16
    public function routeNotificationForSlack(): ?string
17
    {
18
        if($notification = $this->getSpecificConfiguration('slack')) {
19
            return $notification;
20
        }
21
        return parent::routeNotificationForSlack();
22
    }
23
24
    /**
25
     * @return \Spatie\ServerMonitor\Models\Host
26
     */
27
    protected function getHost() {
28
        return $this->event->check->host()->first();
29
    }
30
31
    private function getSpecificConfiguration($type) {
32
        $notifications = $this->getHost()->getCustomProperty('notifications');
33
        if($notifications) {
34
            if(!isset($notifications['configuration'][$type])) {
35
                return null;
36
            }
37
            if(count($notifications['configuration'][$type]) == 1) {
38
                $configuration = config('server-monitor-plugin-notification-by-host.notifications.channels.'.$type);
39
                return $notifications['configuration'][$type][array_keys($configuration)[0]];
40
            }
41
            return $notifications['configuration'][$type];
42
        }
43
        return null;
44
    }
45
}
46