Completed
Pull Request — master (#20)
by Casper
06:41
created

PushoverChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace NotificationChannels\Pushover;
4
5
use Illuminate\Notifications\Notification;
6
use Illuminate\Contracts\Events\Dispatcher;
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
    public function __construct(Pushover $pushover, Dispatcher $events)
24
    {
25
        $this->pushover = $pushover;
26
        $this->events = $events;
27
    }
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
    public function send($notifiable, Notification $notification)
38
    {
39
        if (! $pushoverReceiver = $notifiable->routeNotificationFor('pushover')) {
40
            return;
41
        }
42
43
        if (is_string($pushoverReceiver)) {
44
            $pushoverReceiver = PushoverReceiver::withUserKey($pushoverReceiver);
45
        }
46
47
        $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...
48
49
        try {
50
            $this->pushover->send(array_merge($message->toArray(), $pushoverReceiver->toArray()));
51
        } catch (ServiceCommunicationError $serviceCommunicationError) {
52
            $this->fireFailedEvent($notifiable, $notification, $serviceCommunicationError->getMessage());
53
        }
54
    }
55
56
    protected function fireFailedEvent($notifiable, $notification, $message)
57
    {
58
        $this->events->fire(
59
            new NotificationFailed($notifiable, $notification, 'pushover', [$message])
60
        );
61
    }
62
}
63