Test Failed
Pull Request — master (#52)
by
unknown
09:39
created

ExpoChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications;
4
5
use ExponentPhpSDK\Exceptions\ExpoException;
6
use ExponentPhpSDK\Expo;
7
use Illuminate\Contracts\Events\Dispatcher;
8
use Illuminate\Notifications\Events\NotificationFailed;
9
use Illuminate\Notifications\Notification;
10
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotSendNotification;
11
12
class ExpoChannel
13
{
14
    /**
15
     * @var Dispatcher
16
     */
17
    private $events;
18
19
    /**
20
     * @var Expo
21
     */
22
    public $expo;
23
24
    /**
25
     * ExpoChannel constructor.
26
     *
27
     * @param Expo $expo
28
     * @param Dispatcher $events
29
     */
30 12
    public function __construct(Expo $expo, Dispatcher $events)
31
    {
32 12
        $this->events = $events;
33 12
        $this->expo = $expo;
34 12
    }
35
36
    /**
37
     * Send the given notification.
38
     *
39
     * @param mixed $notifiable
40
     * @param \Illuminate\Notifications\Notification $notification
41
     *
42
     * @throws CouldNotSendNotification
43
     *
44
     * @return void
45
     */
46 2
    public function send($notifiable, Notification $notification)
47
    {
48 2
        $interest = $notifiable->routeNotificationFor('ExpoPushNotifications')
49 2
            ?: $this->interestName($notifiable);
50
51
        try {
52 2
            $this->expo->notify(
53 2
                $interest,
54 2
                $notification->toExpoPush($notifiable)->toArray(),
55 2
                config('exponent-push-notifications.debug')
56
            );
57 2
        } catch (ExpoException $e) {
58
            $this->events->dispatch(
59
                new NotificationFailed($notifiable, $notification, 'expo-push-notifications', $e->getMessage())
60
            );
61
        }
62
    }
63
64
    /**
65
     * Get the interest name for the notifiable.
66
     *
67
     * @param $notifiable
68
     *
69
     * @return string
70
     */
71 8
    public function interestName($notifiable)
72
    {
73 8
        $class = str_replace('\\', '.', get_class($notifiable));
74
75 8
        return $class.'.'.$notifiable->getKey();
76
    }
77
}
78