InfobipChannel::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace NotificationChannels\Infobip;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Notifications\Notification;
7
use NotificationChannels\Infobip\Events\NotificationFailed;
8
use NotificationChannels\Infobip\Events\NotificationSent;
9
use NotificationChannels\Infobip\Exceptions\CouldNotSendNotification;
10
11
class InfobipChannel
12
{
13
    /**
14
     * @var Dispatcher
15
     */
16
    public $events;
17
18
    /**
19
     * @var Infobip
20
     */
21
    public $infobip;
22
23
    /**
24
     * InfobipChannel constructor.
25
     *
26
     * @param Infobip $infobip
27
     * @param Dispatcher $events
28
     */
29
    public function __construct(Infobip $infobip, Dispatcher $events)
30
    {
31
        $this->events = $events;
32
        $this->infobip = $infobip;
33
    }
34
35
    /**
36
     * Send the given notification.
37
     *
38
     * @param mixed $notifiable
39
     * @param \Illuminate\Notifications\Notification $notification
40
     *
41
     * @throws \NotificationChannels\Infobip\Exceptions\CouldNotSendNotification
42
     */
43
    public function send($notifiable, Notification $notification)
44
    {
45
        $recipient = $this->getRecipient($notifiable);
46
        $message = $notification->toInfoBip($notifiable);
0 ignored issues
show
Bug introduced by
The method toInfoBip() 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...
47
48
        try {
49
            $response = $this->infobip->sendMessage($message, $recipient);
50
            $sentMessageInfo = $response->getMessages()[0];
51
52
            $this->events->dispatch(new NotificationSent($notifiable, $notification, $sentMessageInfo));
53
        } catch (\Exception $exception) {
54
            $this->events->dispatch(new NotificationFailed($notifiable, $notification, $exception));
55
        }
56
    }
57
58
    /**
59
     * Get message recipient.
60
     *
61
     * @param $notifiable
62
     * @return mixed
63
     * @throws CouldNotSendNotification
64
     */
65
    public function getRecipient($notifiable)
66
    {
67
        if ($notifiable->routeNotificationForInfoBip()) {
68
            return $notifiable->routeNotificationForInfoBip();
69
        }
70
71
        throw CouldNotSendNotification::invalidReceiver();
72
    }
73
}
74