Completed
Push — master ( f4336a...a269ea )
by Eric
7s
created

EventHandler::whenSSLCertificateValid()   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 EricMakesStuff\ServerMonitor\Events\SSLCertificateExpiring;
10
use EricMakesStuff\ServerMonitor\Events\SSLCertificateInvalid;
11
use EricMakesStuff\ServerMonitor\Events\SSLCertificateValid;
12
use Illuminate\Events\Dispatcher;
13
14
class EventHandler
15
{
16
    /**
17
     * @var \EricMakesStuff\ServerMonitor\Notifications\Notifier
18
     */
19
    protected $notifier;
20
21
    public function __construct()
22
    {
23
        $notifierClass = config('server-monitor.notifications.handler');
24
25
        $this->notifier = app($notifierClass);
26
    }
27
28
    /**
29
     * @param \EricMakesStuff\ServerMonitor\Events\DiskUsageAlarm $event
30
     */
31
    public function whenDiskUsageAlarm(DiskUsageAlarm $event)
32
    {
33
        $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...
34
    }
35
36
    /**
37
     * @param \EricMakesStuff\ServerMonitor\Events\DiskUsageHealthy $event
38
     */
39
    public function whenDiskUsageHealthy(DiskUsageHealthy $event)
40
    {
41
        $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...
42
    }
43
44
    /**
45
     * @param \EricMakesStuff\ServerMonitor\Events\HttpPingDown $event
46
     */
47
    public function whenHttpPingDown(HttpPingDown $event)
48
    {
49
        $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...
50
    }
51
52
    /**
53
     * @param \EricMakesStuff\ServerMonitor\Events\HttpPingUp $event
54
     */
55
    public function whenHttpPingUp(HttpPingUp $event)
56
    {
57
        $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...
58
    }
59
60
    /**
61
     * @param \EricMakesStuff\ServerMonitor\Events\SSLCertificateValid $event
62
     */
63
    public function whenSSLCertificateValid(SSLCertificateValid $event)
64
    {
65
        $this->notifier->sslCertificateValid($event->sslCertificateMonitor);
0 ignored issues
show
Bug introduced by
It seems like $event->sslCertificateMonitor can be null; however, sslCertificateValid() 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...
66
    }
67
68
    /**
69
     * @param \EricMakesStuff\ServerMonitor\Events\SSLCertificateInvalid $event
70
     */
71
    public function whenSSLCertificateInvalid(SSLCertificateInvalid $event)
72
    {
73
        $this->notifier->sslCertificateInvalid($event->sslCertificateMonitor);
0 ignored issues
show
Bug introduced by
It seems like $event->sslCertificateMonitor can be null; however, sslCertificateInvalid() 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...
74
    }
75
76
    /**
77
     * @param \EricMakesStuff\ServerMonitor\Events\SSLCertificateExpiring $event
78
     */
79
    public function whenSSLCertificateExpiring(SSLCertificateExpiring $event)
80
    {
81
        $this->notifier->sslCertificateExpiring($event->sslCertificateMonitor);
0 ignored issues
show
Bug introduced by
It seems like $event->sslCertificateMonitor can be null; however, sslCertificateExpiring() 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...
82
    }
83
84
    /**
85
     * Register the listeners for the subscriber.
86
     *
87
     * @param \Illuminate\Events\Dispatcher $events
88
     *
89
     * @return array
90
     */
91
    public function subscribe(Dispatcher $events)
92
    {
93
        $events->listen(
94
            DiskUsageHealthy::class,
95
            static::class.'@whenDiskUsageHealthy'
96
        );
97
98
        $events->listen(
99
            DiskUsageAlarm::class,
100
            static::class.'@whenDiskUsageAlarm'
101
        );
102
103
        $events->listen(
104
            HttpPingUp::class,
105
            static::class.'@whenHttpPingUp'
106
        );
107
108
        $events->listen(
109
            HttpPingDown::class,
110
            static::class.'@whenHttpPingDown'
111
        );
112
113
        $events->listen(
114
            SSLCertificateValid::class,
115
            static::class.'@whenSSLCertificateValid'
116
        );
117
118
        $events->listen(
119
            SSLCertificateInvalid::class,
120
            static::class.'@whenSSLCertificateInvalid'
121
        );
122
123
        $events->listen(
124
            SSLCertificateExpiring::class,
125
            static::class.'@whenSSLCertificateExpiring'
126
        );
127
    }
128
}
129