Completed
Push — master ( b9c6ac...0b8bdd )
by Sam
59s
created

Advantasms::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
cc 4
nc 4
nop 0
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
    public function send() {
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
        ];
87
        $response = $this->curlPost($this->sendsms,$data);
88
        $return = [
89
            "success" => false,
90
            "message" => "",
91
            "payload" => []
92
        ];
93
        if (!$response) {
94
            $return["success"] = false;
95
            $return["message"] = "No response from the server.";
96
            return $return;
97
        } else {
98
            if (isset($response['responses'])) {
99
                $first = $response["responses"][0];
100
                $return["success"] = $first["response-code"] ===200;
101
                $return["code"] = $first["response-code"];
102
                $return["message"] = $first["response-description"];
103
                $return["payload"] = $response["responses"];
104
                return $return;
105
            }
106
            if (isset($response["response-code"])) {
107
                $first = $response;
108
                $return["success"] = $first["response-code"] ===200;
109
                $return["code"] = $first["response-code"];
110
                $return["message"] = $first["response-description"];
111
                $return["payload"] = $response;
112
                return $return;
113
            }
114
            $return["success"] = false;
115
            $return["message"] = "Unknown Error";
116
            $return["payload"] = $response;
117
            return $response;
118
        }
119
    }
120
121
    /**
122
     * Execute sms sending action
123
     * @param string $time | Time when the sms will be sent in the format Y-m-d H:i
124
     * @return array|mixed
125
     */
126
    public function schedule(string $time) {
127
        $data = [
128
            "apikey"=>$this->apikey,
129
            "partnerID"=>trim($this->partnerId),
130
            "message"=>trim($this->message),
131
            "shortcode"=>$this->shortcode,
132
            "mobile"=>trim($this->to),
133
            "timeToSend" => trim($time),
134
        ];
135
        $response = $this->curlPost($this->sendsms,$data);
136
        return $response;
137
    }
138
139
    /**
140
     * @param string $endpoint
141
     * @param array $data
142
     * @param array $headers
143
     * @return array|mixed
144
     */
145
    private function curlPost(string $endpoint, array $data, array $headers=[]) {
146
        $url = $this->baseUrl.$endpoint;
147
        $curl = curl_init();
148
        curl_setopt($curl, CURLOPT_URL, $url);
149
        curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'),$headers));
150
151
        $data_string = json_encode($data);
152
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
153
        curl_setopt($curl, CURLOPT_POST, true);
154
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
155
        curl_setopt($curl, CURLOPT_HEADER, false);
156
        $curl_response = curl_exec($curl);
157
        return json_decode($curl_response,true);
158
    }
159
160
}
161