InfobipChannel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 63
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 14 2
A getRecipient() 0 8 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