SendberryChannel::send()   A
last analyzed

Complexity

Conditions 5
Paths 18

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 21
rs 9.4888
cc 5
nc 18
nop 2
1
<?php
2
3
namespace NotificationChannels\Sendberry;
4
5
use Illuminate\Events\Dispatcher;
6
use Illuminate\Notifications\Events\NotificationFailed;
7
use NotificationChannels\Sendberry\Exceptions\CouldNotSendNotification;
8
use Illuminate\Notifications\Notification;
9
use Exception;
10
11
class SendberryChannel
12
{
13
    /**
14
     * @var TurboSmsApi
0 ignored issues
show
Bug introduced by
The type NotificationChannels\Sendberry\TurboSmsApi was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
     */
16
    protected $smsApi;
17
18
    /**
19
     * @var Dispatcher
20
     */
21
    protected $events;
22
23
    /**
24
     * SendberryChannel constructor.
25
     * @param TurboSmsApi $smsApi
26
     * @param Dispatcher $events
27
     */
28
    public function __construct(SendberryApi $smsApi, Dispatcher $events)
29
    {
30
        $this->smsApi = $smsApi;
0 ignored issues
show
Documentation Bug introduced by
It seems like $smsApi of type NotificationChannels\Sendberry\SendberryApi is incompatible with the declared type NotificationChannels\Sendberry\TurboSmsApi of property $smsApi.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
        $this->events = $events;
32
    }
33
    /**
34
     * Send the given notification.
35
     *
36
     * @param  mixed  $notifiable
37
     * @param  Notification  $notification
38
     *
39
     * @throws CouldNotSendNotification
40
     *
41
     * @return array|null
42
     */
43
    public function send($notifiable, Notification $notification)
44
    {
45
        try {
46
            $recipient = $this->getRecipient($notifiable);
47
            $message = $notification->toSendberry($notifiable);
0 ignored issues
show
Bug introduced by
The method toSendberry() does not exist on Illuminate\Notifications\Notification. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            /** @scrutinizer ignore-call */ 
48
            $message = $notification->toSendberry($notifiable);

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
            if (is_string($message)) {
50
                $message = new SendberryMessage($message);
51
            }
52
            if (! $message instanceof SendberryMessage) {
53
                throw CouldNotSendNotification::invalidMessageObject($message);
54
            }
55
            return $this->smsApi->sendMessage($recipient, $message);
56
57
        } catch (Exception $exception) {
58
            $event = new NotificationFailed($notifiable, $notification, 'Sendberry', ['message' => $exception->getMessage(), 'exception' => $exception]);
59
60
            if (function_exists('event')) { // Use event helper when possible to add Lumen support
61
                event($event);
62
            } else {
63
                $this->events->fire($event);
64
            }
65
        }
66
    }
67
68
    protected function getRecipient($notifiable)
69
    {
70
        if ($notifiable->routeNotificationFor('Sendberry')) {
71
            return $notifiable->routeNotificationFor('Sendberry');
72
        }
73
74
        if (isset($notifiable->phone)) {
75
            return $notifiable->phone;
76
        }
77
78
        throw CouldNotSendNotification::invalidReceiver();
79
    }
80
}
81