Advantasms::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
namespace Savannabits\Advantasms;
4
5
class Advantasms
6
{
7
    private $apikey, $partnerId, $shortcode;
8
    private $message, $to;
9
    private $baseUrl = "https://quicksms.advantasms.com";
10
    private $sendsms = "/api/services/sendsms/";
11
    /**
12
     * Advantasms constructor.
13
     * @param string $apiKey |The advanta sms API Key. See documentation for more details
14
     * @param string $partnerId | The Partner ID. See advantaSMS documentation for more details
15
     * @param string $shortCode | The Shortcode of used to send sms. See documentation for more details
16
     * @param string|null $domain | The base domain in case it is different from https://quicksms.advantasms.com
0 ignored issues
show
Bug introduced by
There is no parameter named $domain. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
17
     * @return Advantasms
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
     */
19
    public function __construct($apiKey, $partnerId, $shortCode, $baseUrl="https://quicksms.advantasms.com")
20
    {
21
        $this->apikey = $apiKey;
22
        $this->partnerId = $partnerId;
23
        $this->shortcode = $shortCode;
24
        $this->baseUrl  = $baseUrl;
25
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
26
    }
27
28
    /**
29
     * Instantiate the Advantasms class.
30
     * @param string $apiKey |The advanta sms API Key. See documentation for more details
31
     * @param string $partnerId | The Partner ID. See advantaSMS documentation for more details
32
     * @param string $shortCode | The Shortcode of used to send sms. See documentation for more details
33
     * @param string|null $domain | The base domain in case it is different from quicksms.advantasms.com
0 ignored issues
show
Bug introduced by
There is no parameter named $domain. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
34
     * @return Advantasms
35
     */
36
    public static function init($apiKey,$partnerId, $shortCode, $baseUrl="https://quicksms.advantasms.com") {
37
        $instance = new self($apiKey,$partnerId,$shortCode,$baseUrl);
38
        return $instance;
39
    }
40
41
    /**
42
     * @param $mobileNumber
43
     * @return Advantasms
44
     */
45
    public function to($mobileNumber) {
46
        $this->to = $mobileNumber;
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $message
52
     * @return Advantasms
53
     */
54
    public function message(string $message="") {
55
        $this->message = $message;
56
        return $this;
57
    }
58
59
    /**
60
     * Execute sms sending action
61
     * @return array|mixed
62
     *
63
     * 200;Successful Request Call
64
     * 1001;Invalid sender id
65
     * 1002;Network not allowed
66
     * 1003;Invalid mobile number
67
     * 1004;Low bulk credits
68
     * 1005;Failed. System error
69
     * 1006;Invalid credentials
70
     * 1007;Failed. System error
71
     * 1008;No Delivery Report
72
     * 1009;unsupported data type
73
     * 1010;unsupported request type
74
     * 4090;Internal Error. Try again after 5 minutes
75
     * 4091;No Partner ID is Set
76
     * 4092;No API KEY Provided
77
     * 4093;Details Not Found
78
     * */
79 View Code Duplication
    public function send() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
        $data = [
81
            "apikey"=>$this->apikey,
82
            "partnerID"=>trim($this->partnerId),
83
            "message"=>trim($this->message),
84
            "shortcode"=>$this->shortcode,
85
            "mobile"=>trim($this->to),
86
            'pass_type' => 'plain',
87
        ];
88
        $response = $this->curlPost($this->sendsms,$data);
89
        $return = [
90
            "success" => false,
91
            "message" => "",
92
            "payload" => []
93
        ];
94
        if (!$response) {
95
            $return["success"] = false;
96
            $return["message"] = "No response from the server.";
97
            return $return;
98
        } else {
99
            if (isset($response['responses'])) {
100
                $first = $response["responses"][0];
101
                $return["success"] = $first["response-code"] ===200;
102
                $return["code"] = $first["response-code"];
103
                $return["message"] = $first["response-description"];
104
                $return["payload"] = $response["responses"];
105
                return $return;
106
            }
107
            if (isset($response["response-code"])) {
108
                $first = $response;
109
                $return["success"] = $first["response-code"] ===200;
110
                $return["code"] = $first["response-code"];
111
                $return["message"] = $first["response-description"];
112
                $return["payload"] = $response;
113
                return $return;
114
            }
115
            //Temporal fix for the mis-spelled api response code to 'respose-code'
116
            if (isset($response["respose-code"])) {
117
                $first = $response;
118
                $return["success"] = $first["respose-code"] ===200;
119
                $return["code"] = $first["respose-code"];
120
                $return["message"] = $first["response-description"];
121
                $return["payload"] = $response;
122
                return $return;
123
            }
124
125
            $return["success"] = false;
126
            $return["message"] = "Unknown Error";
127
            $return["payload"] = $response;
128
            return $return;
129
        }
130
    }
131
132
    /**
133
     * Schedule sms sending action
134
     * @param string $time | Time to send in Y-m-d H:i format
135
     * @return array|mixed
136
     *
137
     * 200;Successful Request Call
138
     * 1001;Invalid sender id
139
     * 1002;Network not allowed
140
     * 1003;Invalid mobile number
141
     * 1004;Low bulk credits
142
     * 1005;Failed. System error
143
     * 1006;Invalid credentials
144
     * 1007;Failed. System error
145
     * 1008;No Delivery Report
146
     * 1009;unsupported data type
147
     * 1010;unsupported request type
148
     * 4090;Internal Error. Try again after 5 minutes
149
     * 4091;No Partner ID is Set
150
     * 4092;No API KEY Provided
151
     * 4093;Details Not Found
152
     * */
153 View Code Duplication
    public function schedule($time) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
        $data = [
155
            "apikey"=>$this->apikey,
156
            "partnerID"=>trim($this->partnerId),
157
            "message"=>trim($this->message),
158
            "shortcode"=>$this->shortcode,
159
            "mobile"=>trim($this->to),
160
            "timeToSend" => trim($time),
161
            'pass_type' => 'plain',
162
        ];
163
        $response = $this->curlPost($this->sendsms,$data);
164
        $return = [
165
            "success" => false,
166
            "message" => "",
167
            "payload" => []
168
        ];
169
        if (!$response) {
170
            $return["success"] = false;
171
            $return["message"] = "No response from the server.";
172
            return $return;
173
        } else {
174
            if (isset($response['responses'])) {
175
                $first = $response["responses"][0];
176
                $return["success"] = $first["response-code"] ===200;
177
                $return["code"] = $first["response-code"];
178
                $return["message"] = $first["response-description"];
179
                $return["payload"] = $response["responses"];
180
                return $return;
181
            }
182
            if (isset($response["response-code"])) {
183
                $first = $response;
184
                $return["success"] = $first["response-code"] ===200;
185
                $return["code"] = $first["response-code"];
186
                $return["message"] = $first["response-description"];
187
                $return["payload"] = $response;
188
                return $return;
189
            }
190
            //Temporal fix for the mis-spelled api response code to 'respose-code'
191
            if (isset($response["respose-code"])) {
192
                $first = $response;
193
                $return["success"] = $first["respose-code"] ===200;
194
                $return["code"] = $first["respose-code"];
195
                $return["message"] = $first["response-description"];
196
                $return["payload"] = $response;
197
                return $return;
198
            }
199
200
            $return["success"] = false;
201
            $return["message"] = "Unknown Error";
202
            $return["payload"] = $response;
203
            return $return;
204
        }
205
    }
206
    /**
207
     * @param string $endpoint
208
     * @param array $data
209
     * @param array $headers
210
     * @return array|mixed
211
     */
212
    private function curlPost(string $endpoint, array $data, array $headers=[]) {
213
        $url = $this->baseUrl.$endpoint;
214
        $curl = curl_init();
215
        curl_setopt($curl, CURLOPT_URL, $url);
216
        curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'),$headers));
217
218
        $data_string = json_encode($data);
219
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
220
        curl_setopt($curl, CURLOPT_POST, true);
221
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
222
        curl_setopt($curl, CURLOPT_HEADER, false);
223
        $curl_response = curl_exec($curl);
224
        return json_decode($curl_response,true);
225
    }
226
227
}
228