Completed
Push — master ( 01668e...b2b309 )
by Casper
06:27
created

PushoverChannel::fireFailedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php
2
3
namespace NotificationChannels\Pushover;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Notifications\Notification;
7
use Illuminate\Notifications\Events\NotificationFailed;
8
use NotificationChannels\Pushover\Exceptions\ServiceCommunicationError;
9
10
class PushoverChannel
11
{
12
    /** @var Pushover */
13
    protected $pushover;
14
15
    /** @var Dispatcher */
16
    protected $events;
17
18
    /**
19
     * Create a new Pushover channel instance.
20
     *
21
     * @param  Pushover $pushover
22
     */
23 4
    public function __construct(Pushover $pushover, Dispatcher $events)
24
    {
25 4
        $this->pushover = $pushover;
26 4
        $this->events = $events;
27 4
    }
28
29
    /**
30
     * Send the given notification.
31
     *
32
     * @param mixed $notifiable
33
     * @param \Illuminate\Notifications\Notification $notification
34
     *
35
     * @throws \NotificationChannels\Pushover\Exceptions\CouldNotSendNotification
36
     */
37 3
    public function send($notifiable, Notification $notification)
38
    {
39 3
        if (! $pushoverKey = $notifiable->routeNotificationFor('pushover')) {
40 1
            return;
41
        }
42
43 2
        $message = $notification->toPushover($notifiable);
0 ignored issues
show
Bug introduced by
The method toPushover() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
45
        try {
46 2
            $this->pushover->send(array_merge($message->toArray(), [
47 2
                'user' => $pushoverKey,
48
            ]));
49
        } catch (ServiceCommunicationError $serviceCommunicationError) {
50
            $this->fireFailedEvent($notifiable, $notification, $serviceCommunicationError->getMessage());
51
        }
52 2
    }
53
54
    protected function fireFailedEvent($notifiable, $notification, $message)
55
    {
56
        $this->events->fire(
57
            new NotificationFailed($notifiable, $notification, 'pushover', [$message])
58
        );
59
    }
60
}
61