Completed
Push — master ( f51b12...f4336a )
by Eric
9s
created

EventHandler::whenHttpPingUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace EricMakesStuff\ServerMonitor\Notifications;
4
5
use EricMakesStuff\ServerMonitor\Events\HttpPingDown;
6
use EricMakesStuff\ServerMonitor\Events\HttpPingUp;
7
use EricMakesStuff\ServerMonitor\Events\DiskUsageAlarm;
8
use EricMakesStuff\ServerMonitor\Events\DiskUsageHealthy;
9
use Illuminate\Events\Dispatcher;
10
11
class EventHandler
12
{
13
    /**
14
     * @var \EricMakesStuff\ServerMonitor\Notifications\Notifier
15
     */
16
    protected $notifier;
17
18
    public function __construct()
19
    {
20
        $notifierClass = config('server-monitor.notifications.handler');
21
22
        $this->notifier = app($notifierClass);
23
    }
24
25
    /**
26
     * @param \EricMakesStuff\ServerMonitor\Events\DiskUsageAlarm $event
27
     */
28
    public function whenDiskUsageAlarm(DiskUsageAlarm $event)
29
    {
30
        $this->notifier->diskUsageAlarm($event->diskUsageMonitor);
0 ignored issues
show
Bug introduced by
It seems like $event->diskUsageMonitor can be null; however, diskUsageAlarm() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
31
    }
32
33
    /**
34
     * @param \EricMakesStuff\ServerMonitor\Events\DiskUsageHealthy $event
35
     */
36
    public function whenDiskUsageHealthy(DiskUsageHealthy $event)
37
    {
38
        $this->notifier->diskUsageHealthy($event->diskUsageMonitor);
0 ignored issues
show
Bug introduced by
It seems like $event->diskUsageMonitor can be null; however, diskUsageHealthy() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
    }
40
41
    /**
42
     * @param \EricMakesStuff\ServerMonitor\Events\HttpPingDown $event
43
     */
44
    public function whenHttpPingDown(HttpPingDown $event)
45
    {
46
        $this->notifier->httpPingDown($event->httpPingMonitor);
0 ignored issues
show
Bug introduced by
It seems like $event->httpPingMonitor can be null; however, httpPingDown() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
47
    }
48
49
    /**
50
     * @param \EricMakesStuff\ServerMonitor\Events\HttpPingUp $event
51
     */
52
    public function whenHttpPingUp(HttpPingUp $event)
53
    {
54
        $this->notifier->httpPingUp($event->httpPingMonitor);
0 ignored issues
show
Bug introduced by
It seems like $event->httpPingMonitor can be null; however, httpPingUp() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
55
    }
56
57
    /**
58
     * Register the listeners for the subscriber.
59
     *
60
     * @param \Illuminate\Events\Dispatcher $events
61
     *
62
     * @return array
63
     */
64
    public function subscribe(Dispatcher $events)
65
    {
66
        $events->listen(
67
            DiskUsageHealthy::class,
68
            static::class.'@whenDiskUsageHealthy'
69
        );
70
71
        $events->listen(
72
            DiskUsageAlarm::class,
73
            static::class.'@whenDiskUsageAlarm'
74
        );
75
76
        $events->listen(
77
            HttpPingUp::class,
78
            static::class.'@whenHttpPingUp'
79
        );
80
81
        $events->listen(
82
            HttpPingDown::class,
83
            static::class.'@whenHttpPingDown'
84
        );
85
    }
86
}
87