Infobip   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 11
dl 0
loc 128
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A sendMessage() 0 17 3
A sendSms() 0 11 1
A sendSmsAdvanced() 0 19 1
A getFrom() 0 8 4
A getNotifyUrl() 0 8 4
1
<?php
2
3
namespace NotificationChannels\Infobip;
4
5
use infobip\api\client\SendMultipleTextualSmsAdvanced;
6
use infobip\api\client\SendSingleTextualSms;
7
use infobip\api\configuration\BasicAuthConfiguration;
8
use infobip\api\model\Destination;
9
use infobip\api\model\sms\mt\send\Message;
10
use infobip\api\model\sms\mt\send\textual\SMSAdvancedTextualRequest;
11
use infobip\api\model\sms\mt\send\textual\SMSTextualRequest;
12
use NotificationChannels\Infobip\Exceptions\CouldNotSendNotification;
13
14
class Infobip
15
{
16
    /**
17
     * @var InfobipConfig
18
     */
19
    public $config;
20
21
    /**
22
     * @var InfobipMessage
23
     */
24
    public $message;
25
26
    /**
27
     * Infobip constructor.
28
     *
29
     * @param InfobipMessage $message
30
     * @param InfobipConfig $config
31
     */
32
    public function __construct(InfobipMessage $message, InfobipConfig $config)
33
    {
34
        $this->config = $config;
35
        $this->message = $message;
36
    }
37
38
    /**
39
     * Send sms message to recipient.
40
     *
41
     * @param InfobipMessage $message
42
     * @param $recipient
43
     * @return \infobip\api\model\sms\mt\send\SMSResponse
44
     * @throws CouldNotSendNotification
45
     */
46
    public function sendMessage(InfobipMessage $message, $recipient)
47
    {
48
        if ($message instanceof InfobipMessage) {
49
            $message->from($this->config->getFrom());
50
51
            return $this->sendSms($message, $recipient);
52
        }
53
54
        if ($message instanceof InfobipSmsAdvancedMessage) {
55
            $message->from($this->config->getFrom());
56
            $message->notifyUrl($this->config->getNotifyUrl());
57
58
            return $this->sendSmsAdvanced($message, $recipient);
59
        }
60
61
        throw CouldNotSendNotification::invalidMessageObject($message);
62
    }
63
64
    /**
65
     * Send sms message to recipient using Infobip SMSTextualRequest.
66
     *
67
     * @param InfobipMessage $message
68
     * @param $recipient
69
     * @return \infobip\api\model\sms\mt\send\SMSResponse
70
     */
71
    public function sendSms(InfobipMessage $message, $recipient)
72
    {
73
        $client = new SendSingleTextualSms(new BasicAuthConfiguration($this->config->config['username'], $this->config->config['password']));
74
75
        $request = new SMSTextualRequest();
76
        $request->setFrom($this->getFrom($message));
77
        $request->setTo($recipient);
78
        $request->setText($message->content);
79
80
        return $client->execute($request);
81
    }
82
83
    /**
84
     * Send sms advanced to recipient using SMSAdvancedTextualRequest.
85
     *
86
     * @param InfobipSmsAdvancedMessage $message
87
     * @param $recipient
88
     * @return \infobip\api\model\sms\mt\send\SMSResponse
89
     */
90
    public function sendSmsAdvanced(InfobipSmsAdvancedMessage $message, $recipient)
91
    {
92
        $client = new SendMultipleTextualSmsAdvanced(new BasicAuthConfiguration($this->config->config['username'], $this->config->config['password']));
93
94
        $destination = new Destination();
95
96
        $destination->setTo($recipient);
97
98
        $requestMessage = new Message();
99
        $requestMessage->setFrom($this->getFrom($message));
100
        $requestMessage->setDestinations([$destination]);
101
        $requestMessage->setText($message->content);
102
        $requestMessage->setNotifyUrl($this->getNotifyUrl($message));
103
104
        $request = new SMSAdvancedTextualRequest();
105
        $request->setMessages([$requestMessage]);
106
107
        return $client->execute($request);
108
    }
109
110
    /**
111
     * Get message from phone number from message or config.
112
     *
113
     * @param InfobipMessage $message
114
     * @return mixed
115
     * @throws CouldNotSendNotification
116
     */
117
    public function getFrom(InfobipMessage $message)
118
    {
119
        if (! $from = $message->from ?: $this->config->config['from']) {
120
            throw CouldNotSendNotification::missingFrom();
121
        }
122
123
        return $message->from ?: $this->config->config['from'];
124
    }
125
126
    /**
127
     * Get sms notify url.
128
     *
129
     * @param InfobipSmsAdvancedMessage $message
130
     * @return mixed
131
     * @throws CouldNotSendNotification
132
     */
133
    public function getNotifyUrl(InfobipSmsAdvancedMessage $message)
134
    {
135
        if (! $notifyUrl = $message->notifyUrl ?: $this->config->config['notify_url']) {
136
            throw CouldNotSendNotification::missingNotifyUrl();
137
        }
138
139
        return $message->notify_url ?: $this->config->config['notify_url'];
0 ignored issues
show
Bug introduced by
The property notify_url does not seem to exist in NotificationChannels\Inf...fobipSmsAdvancedMessage.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
140
    }
141
}
142