Completed
Push — master ( 58c0d7...dbbb64 )
by Sam
01:08
created

Advantasms::send()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 8.9163
c 0
b 0
f 0
cc 4
nc 4
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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/api/services";
10
    private $sendsms = "/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
     * @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...
17
     */
18
    public function __construct($apiKey, $partnerId, $shortCode)
19
    {
20
        $this->apikey = $apiKey;
21
        $this->partnerId = $partnerId;
22
        $this->shortcode = $shortCode;
23
        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...
24
    }
25
26
    /**
27
     * Instantiate the Advantasms class.
28
     * @param string $apiKey |The advanta sms API Key. See documentation for more details
29
     * @param string $partnerId | The Partner ID. See advantaSMS documentation for more details
30
     * @param string $shortCode | The Shortcode of used to send sms. See documentation for more details
31
     * @return Advantasms
32
     */
33
    public static function init($apiKey,$partnerId, $shortCode) {
34
        $instance = new self($apiKey,$partnerId,$shortCode);
35
        return $instance;
36
    }
37
38
    /**
39
     * @param $mobileNumber
40
     * @return Advantasms
41
     */
42
    public function to($mobileNumber) {
43
        $this->to = $mobileNumber;
44
        return $this;
45
    }
46
47
    /**
48
     * @param string $message
49
     * @return Advantasms
50
     */
51
    public function message(string $message="") {
52
        $this->message = $message;
53
        return $this;
54
    }
55
56
    /**
57
     * Execute sms sending action
58
     * @return array|mixed
59
     */
60
    public function send() {
61
        $data = [
62
            "apikey"=>$this->apikey,
63
            "partnerID"=>trim($this->partnerId),
64
            "message"=>trim($this->message),
65
            "shortcode"=>$this->shortcode,
66
            "mobile"=>trim($this->to)
67
        ];
68
        $response = $this->curlPost($this->sendsms,$data);
69
        /*
70
            200;Successful Request Call
71
            1001;Invalid sender id
72
            1002;Network not allowed
73
            1003;Invalid mobile number
74
            1004;Low bulk credits
75
            1005;Failed. System error
76
            1006;Invalid credentials
77
            1007;Failed. System error
78
            1008;No Delivery Report
79
            1009;unsupported data type
80
            1010;unsupported request type
81
            4090;Internal Error. Try again after 5 minutes
82
            4091;No Partner ID is Set
83
            4092;No API KEY Provided
84
            4093;Details Not Found
85
         */
86
        $return = [
87
            "success" => false,
88
            "message" => "",
89
            "payload" => []
90
        ];
91
        if (!$response) {
92
            $return["success"] = false;
93
            $return["message"] = "No response from the server.";
94
            return $return;
95
        } else {
96
            if (isset($response['responses'])) {
97
                $first = $response["responses"][0];
98
                $return["success"] = $first["response-code"] ===200;
99
                $return["code"] = $first["response-code"];
100
                $return["message"] = $first["response-description"];
101
                $return["payload"] = $response["responses"];
102
                return $return;
103
            }
104
            if (isset($response["response-code"])) {
105
                $first = $response;
106
                $return["success"] = $first["response-code"] ===200;
107
                $return["code"] = $first["response-code"];
108
                $return["message"] = $first["response-description"];
109
                $return["payload"] = $response;
110
                return $return;
111
            }
112
            $return["success"] = false;
113
            $return["message"] = "Unknown Error";
114
            $return["payload"] = $response;
115
            return $response;
116
        }
117
    }
118
119
    /**
120
     * Execute sms sending action
121
     * @param string $time | Time when the sms will be sent in the format Y-m-d H:i
122
     * @return array|mixed
123
     */
124
    public function schedule(string $time) {
125
        $data = [
126
            "apikey"=>$this->apikey,
127
            "partnerID"=>trim($this->partnerId),
128
            "message"=>trim($this->message),
129
            "shortcode"=>$this->shortcode,
130
            "mobile"=>trim($this->to),
131
            "timeToSend" => trim($time),
132
        ];
133
        $response = $this->curlPost($this->sendsms,$data);
134
        return $response;
135
    }
136
137
    /**
138
     * @param string $endpoint
139
     * @param array $data
140
     * @param array $headers
141
     * @return array|mixed
142
     */
143
    private function curlPost(string $endpoint, array $data, array $headers=[]) {
144
        $url = $this->baseUrl.$endpoint;
145
        $curl = curl_init();
146
        curl_setopt($curl, CURLOPT_URL, $url);
147
        curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'),$headers));
148
149
        $data_string = json_encode($data);
150
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
151
        curl_setopt($curl, CURLOPT_POST, true);
152
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
153
        curl_setopt($curl, CURLOPT_HEADER, false);
154
        $curl_response = curl_exec($curl);
155
        return json_decode($curl_response);
156
    }
157
158
}
159