SmsRuChannel::getRecipient()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace NotificationChannels\SmsRu;
4
5
use Illuminate\Events\Dispatcher;
0 ignored issues
show
Bug introduced by
The type Illuminate\Events\Dispatcher 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...
6
use Illuminate\Notifications\Events\NotificationFailed;
7
use NotificationChannels\SmsRu\Exceptions\CouldNotSendNotification;
8
use Illuminate\Notifications\Notification;
9
10
class SmsRuChannel
11
{
12
    /**
13
     * @var SmsRuApi
14
     */
15
    protected $smsApi;
16
17
    /**
18
     * @var Dispatcher
19
     */
20
    protected $events;
21
22
    /**
23
     * SmsRuChannel constructor.
24
     * @param SmsRuApi $smsApi
25
     * @param Dispatcher $events
26
     */
27
    public function __construct(SmsRuApi $smsApi, Dispatcher $events)
28
    {
29
        $this->smsApi = $smsApi;
30
        $this->events = $events;
31
    }
32
    /**
33
     * Send the given notification.
34
     *
35
     * @param  mixed  $notifiable
36
     * @param  Notification  $notification
37
     *
38
     * @throws CouldNotSendNotification
39
     *
40
     * @return array|null
41
     */
42
    public function send($notifiable, Notification $notification)
43
    {
44
        try {
45
            $recipient = $this->getRecipient($notifiable);
46
            $message = $notification->toSmsru($notifiable);
0 ignored issues
show
Bug introduced by
The method toSmsru() 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

46
            /** @scrutinizer ignore-call */ 
47
            $message = $notification->toSmsru($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...
47
48
            if (is_string($message)) {
49
                $message = new SmsRuMessage($message);
50
            }
51
            if (! $message instanceof SmsRuMessage) {
52
                throw CouldNotSendNotification::invalidMessageObject($message);
53
            }
54
55
            return $this->smsApi->sendMessage($recipient, $message);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->smsApi->se...e($recipient, $message) returns the type Zelenin\SmsRu\Response\SmsResponse which is incompatible with the documented return type array|null.
Loading history...
56
57
        } catch (Exception $exception) {
58
            $event = new NotificationFailed($notifiable, $notification, 'smsru', ['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('smsru')) {
71
            return $notifiable->routeNotificationFor('smsru');
72
        }
73
74
        if (isset($notifiable->phone)) {
75
            return $notifiable->phone;
76
        }
77
78
        throw CouldNotSendNotification::invalidReceiver();
79
    }
80
}
81