Completed
Push — master ( 7c85b2...91e0f6 )
by Aly
05:47
created

ExpoChannel::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 11
nc 4
nop 2
crap 12
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications;
4
5
use ExponentPhpSDK\Exceptions\ExpoException;
6
use ExponentPhpSDK\Expo;
7
use Illuminate\Events\Dispatcher;
8
use Illuminate\Notifications\Events\NotificationFailed;
9
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotSendNotification;
10
use Illuminate\Notifications\Notification;
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 2
    public function __construct(Expo $expo, Dispatcher $events)
31
    {
32 2
        $this->events = $events;
33 2
        $this->expo = $expo;
34 2
    }
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
    public function send($notifiable, Notification $notification)
47
    {
48
        $interest = $notifiable->routeNotificationFor('ExpoPushNotifications')
49
            ?: $this->interestName($notifiable);
50
51
        try {
52
            $response = $this->expo->notify(
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
                $interest,
54
                $notification->toExpoPush($notifiable)->toArray(),
1 ignored issue
show
Bug introduced by
The method toExpoPush() 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...
55
                true
56
            );
57
        } catch (ExpoException $e) {
58
            $this->events->fire(
59
                new NotificationFailed($notifiable, $notification, 'expo-push-notifications', $e->getMessage())
1 ignored issue
show
Documentation introduced by
$e->getMessage() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
            );
61
        }
62
    }
63
64
    /**
65
     * Get the interest name for the notifiable.
66
     *
67
     * @param $notifiable
68
     *
69
     * @return string
70
     */
71 2
    public function interestName($notifiable)
72
    {
73 2
        $class = str_replace('\\', '.', get_class($notifiable));
74
75 2
        return $class.'.'.$notifiable->getKey();
76
    }
77
}
78