Completed
Push — master ( 221734...f96e3c )
by Casper
06:04
created

PushoverChannel::send()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 29
ccs 23
cts 23
cp 1
rs 8.8571
cc 3
eloc 20
nc 3
nop 2
crap 3
1
<?php
2
3
namespace NotificationChannels\Pushover;
4
5
use Illuminate\Notifications\Notification;
6
use Illuminate\Notifications\Events\NotificationFailed;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use NotificationChannels\Pushover\Exceptions\ServiceCommunicationError;
9
10
class PushoverChannel
11
{
12
    /** @var Pushover */
13
    protected $pushover;
14
15
    /**
16
     * Create a new Pushover channel instance.
17
     *
18
     * @param  Pushover $pushover
19
     */
20 4
    public function __construct(Pushover $pushover, Dispatcher $events)
21
    {
22 4
        $this->pushover = $pushover;
23 4
        $this->events = $events;
0 ignored issues
show
Bug introduced by
The property events does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24 4
    }
25
26
    /**
27
     * Send the given notification.
28
     *
29
     * @param mixed $notifiable
30
     * @param \Illuminate\Notifications\Notification $notification
31
     *
32
     * @throws \NotificationChannels\Pushover\Exceptions\CouldNotSendNotification
33
     */
34 4
    public function send($notifiable, Notification $notification)
35
    {
36 4
        if (! $pushoverKey = $notifiable->routeNotificationFor('pushover')) {
37 1
            return;
38
        }
39
40 3
        $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...
41
42
        try {
43 3
            $this->pushover->send([
44 3
                'user' => $pushoverKey,
45 3
                'message' => $message->content,
46 3
                'title' => $message->title,
47 3
                'timestamp' => $message->timestamp,
48 3
                'priority' => $message->priority,
49 3
                'url' => $message->url,
50 3
                'url_title' => $message->urlTitle,
51 3
                'sound' => $message->sound,
52 3
                'retry' => $message->retry,
53 3
                'expire' => $message->expire,
54 3
            ]);
55 3
        } catch (ServiceCommunicationError $serviceCommunicationError) {
56 1
            $this->events->fire(
57 1
                new NotificationFailed($notifiable, $notification, 'pushover', [
58 1
                    $serviceCommunicationError->getMessage()
59 1
                ])
60 1
            );
61
        }
62 3
    }
63
}
64