SMSCChannel::__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
cc 1
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Author: Facundo J Gonzalez
4
 * Date: 17/11/2017.
5
 */
6
7
namespace NotificationChannels\SMSC;
8
9
use Illuminate\Notifications\Notification;
10
use Illuminate\Contracts\Events\Dispatcher;
11
use NotificationChannels\SMSC\Events\MessageWasSent;
12
use NotificationChannels\SMSC\Events\SendingMessage;
13
use NotificationChannels\SMSC\Clients\SMSCClientInterface;
14
use NotificationChannels\SMSC\Clients\SMSCApiResponseInterface;
15
use NotificationChannels\SMSC\Exceptions\CouldNotSendNotification;
16
17
/**
18
 * Class SMSCChannel.
19
 */
20
final class SMSCChannel
21
{
22
    /**
23
     * The guzzle http client.
24
     *
25
     * @var SMSCClientInterface
26
     */
27
    private $client;
28
29
    /**
30
     * The Laravel event dispatcher implementation.
31
     *
32
     * @var Dispatcher
33
     */
34
    private $dispatcher;
35
36
    /**
37
     * SMSCChannel constructor.
38
     *
39
     * @param SMSCClientInterface   $client
40
     * @param Dispatcher            $dispatcher
41
     */
42
    public function __construct(SMSCClientInterface $client, Dispatcher $dispatcher)
43
    {
44
        $this->client = $client;
45
        $this->dispatcher = $dispatcher;
46
    }
47
48
    /**
49
     * Send the given notification.
50
     *
51
     * @param  mixed                                  $notifiable
52
     * @param  \Illuminate\Notifications\Notification $notification
53
     * @throws \NotificationChannels\SMSC\Exceptions\CouldNotSendNotification
54
     * @return void
55
     */
56
    public function send($notifiable, Notification $notification)
57
    {
58
        $to = $notifiable->routeNotificationFor('SMSC');
59
60
        if (empty($to)) {
61
            throw CouldNotSendNotification::missingRecipient();
62
        }
63
64
        $message = $notification->toSMSC($notifiable);
0 ignored issues
show
Bug introduced by
The method toSMSC() 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...
65
66
        if (is_string($message)) {
67
            $message = new SMSCMessage($message, $to);
68
        }
69
70
        if (strlen($message->content()) > 160) {
71
            throw CouldNotSendNotification::contentLengthLimitExceeded();
72
        }
73
74
        $this->client->addToRequest($message);
75
        $this->fireSendingEvent($message);
76
        $response = $this->sendMessage();
77
        $this->fireSentEvent($message, $response);
78
    }
79
80
    /**
81
     * Send the message.
82
     *
83
     * @return SMSCApiResponseInterface
84
     */
85
    private function sendMessage()
86
    {
87
        $response = $this->client->sendRequest();
88
89
        return $response;
90
    }
91
92
    /**
93
     * Fire the sending event for the prepared message.
94
     *
95
     * @param SMSCMessage $message
96
     */
97
    private function fireSendingEvent(SMSCMessage $message)
98
    {
99
        $this->dispatcher->fire(new SendingMessage($message));
100
    }
101
102
    /**
103
     * Fire  the sent event for the message.
104
     *
105
     * @param SMSCMessage         $message
106
     * @param SMSCApiResponseInterface $response
107
     */
108
    private function fireSentEvent(SMSCMessage $message, SMSCApiResponseInterface $response)
109
    {
110
        $this->dispatcher->fire(new MessageWasSent($message, $response));
111
    }
112
}
113