TurboSMSChannel   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 122
ccs 24
cts 33
cp 0.7272
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getWsdlEndpoint() 0 4 1
A __construct() 0 9 1
A getClient() 0 8 2
A send() 0 37 5
1
<?php
2
3
namespace NotificationChannels\TurboSMS;
4
5
use Illuminate\Notifications\Notification;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\Log;
8
use NotificationChannels\TurboSMS\Exceptions\CouldNotSendNotification;
9
10
class TurboSMSChannel
11
{
12
    /**
13
     * Login to API endpoint.
14
     *
15
     * @var string
16
     */
17
    protected $login;
18
19
    /**
20
     * Password to API endpoint.
21
     *
22
     * @var string
23
     */
24
    protected $password;
25
26
    /**
27
     * API endpoint wsdl url.
28
     *
29
     * @var string
30
     */
31
    protected $wsdlEndpoint;
32
33
    /**
34
     * Registered sender. Should be requested in TurboSMS user's page.
35
     *
36
     * @var string
37
     */
38
    protected $sender;
39
40
    /**
41
     * Debug flag. If true, messages send/result wil be stored in Laravel log.
42
     *
43
     * @var bool
44
     */
45
    protected $debug;
46
47
    /**
48
     * Sandbox mode flag. If true, endpoint API will not be invoked, useful for dev purposes.
49
     *
50
     * @var bool
51
     */
52
    protected $sandboxMode;
53
54
    /**
55
     * @return mixed
56
     */
57
    public function getWsdlEndpoint()
58
    {
59
        return $this->wsdlEndpoint;
60
    }
61
62 3
    public function __construct(array $config = [])
63
    {
64 3
        $this->login = Arr::get($config, 'login');
65 3
        $this->password = Arr::get($config, 'password');
66 3
        $this->wsdlEndpoint = Arr::get($config, 'wsdlEndpoint');
67 3
        $this->sender = Arr::get($config, 'sender');
68 3
        $this->debug = Arr::get($config, 'debug', false);
69 3
        $this->sandboxMode = Arr::get($config, 'sandboxMode', false);
70 3
    }
71
72
    /**
73
     * @return \SoapClient
74
     * @throws CouldNotSendNotification
75
     */
76
    protected function getClient()
77
    {
78
        try {
79
            return new \SoapClient($this->wsdlEndpoint);
80
        } catch (\Exception $exception) {
81
            throw CouldNotSendNotification::couldNotCommunicateWithEndPoint($exception);
82
        }
83
    }
84
85
    /**
86
     * Send the given notification.
87
     *
88
     * @param mixed $notifiable
89
     *
90
     * @param Notification $notification
91
     * @return void|array
92
     * @throws CouldNotSendNotification
93
     */
94 2
    public function send($notifiable, Notification $notification)
95
    {
96
        /** @var TurboSMSMessage $message */
97 2
        $message = $notification->toTurboSMS($notifiable);
0 ignored issues
show
Bug introduced by
The method toTurboSMS() 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...
98 2
        if (is_string($message)) {
99
            $message = new TurboSMSMessage($message);
100
        }
101
102
        $sms = [
103 2
            'sender' => $this->sender,
104 2
            'destination' => $notifiable->routeNotificationFor('turbosms'),
105 2
            'text' => $message->body,
106
        ];
107
108 2
        if ($this->debug) {
109
            Log::info('TurboSMS sending sms - '.print_r($sms, true));
110
        }
111
112
        $auth = [
113 2
            'login' => $this->login,
114 2
            'password' => $this->password,
115
        ];
116
117 2
        if ($this->sandboxMode) {
118 1
            return;
119
        }
120
121 1
        $client = $this->getClient();
122 1
        $client->Auth($auth);
123 1
        $result = $client->SendSMS($sms);
124
125 1
        if ($this->debug) {
126
            Log::info('TurboSMS send result - '.print_r($result->SendSMSResult->ResultArray, true));
127
        }
128
129 1
        return $result;
130
    }
131
}
132