JazzCMS::balanceInquiry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace NsTechNs\JazzCMS;
4
5
class JazzCMS
6
{
7
    private $config;
8
    private $response;
9
10
    public function __construct(array $config = [])
11
    {
12
        $this->response = new \stdClass;
13
        $this->setConfigParams($config);
14
    }
15
16
    private function setConfigParams($config)
17
    {
18
        $this->resetConfig();
19
        foreach ($config as $key => $value) {
20
            $this->config[$key] = $config[$key];
21
        }
22
    }
23
24
    private function resetConfig()
25
    {
26
        $this->config = [
27
            'base_url' => config('jazz-cms.base_url'),
28
            'username' => config('jazz-cms.username'),
29
            'password' => config('jazz-cms.password'),
30
            'from_mask' => config('jazz-cms.from'),
31
            'is_urdu' => config('jazz-cms.is_urdu', false),
32
            'show_status' => config('jazz-cms.show_status', true),
33
            'short_code' => config('jazz-cms.short_code'),
34
            'user_agent' => request()->header('User-Agent'),
35
            ];
36
    }
37
38
    public function setIsUrdu($value = true)
39
    {
40
        $this->config['is_urdu'] = $value;
41
    }
42
43
    public function setShowStatus($value = true)
44
    {
45
        $this->config['show_status'] = $value;
46
    }
47
48
    private function sendRequest($type, $end_point, $data = [], $headers = [], $file = false)
49
    {
50
        try {
51
            $ch = curl_init();
52
            curl_setopt($ch, CURLOPT_URL, $this->config['base_url'].$end_point);
53
            curl_setopt($ch, CURLOPT_USERAGENT, $this->config['user_agent']);
54
55
            if (! empty($headers)) {
56
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
57
            }
58
59
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
60
61
            if ($type == 'POST' && ! empty($data)) {
62
                curl_setopt($ch, CURLOPT_POST, 1);
63
                if (! empty($file)) {
64
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
65
                } else {
66
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
67
                }
68
            }
69
70
            if (! empty($file)) {
71
                curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
72
            }
73
74
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
75
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
76
            curl_setopt($ch, CURLOPT_ENCODING, '');
77
            curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
78
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
79
            curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
80
            curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
81
            curl_setopt($ch, CURLOPT_TIMEOUT, 0);
82
            $result = curl_exec($ch);
83
84
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
85
            $curlError = curl_error($ch);
86
            $requestSize = curl_getinfo($ch, CURLINFO_REQUEST_SIZE);
87
            $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
88
            $redirectCount = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT);
89
            $effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
90
            $totalTime = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
91
92
            $this->response->request = ['request_size' => $requestSize, 'curl_error' => $curlError, 'base_url' => $this->config['base_url'], 'content_type' => $contentType, 'redirect_count' => $redirectCount, 'effective_url' => $effectiveUrl, 'total_time' => $totalTime];
93
            $this->response->http_code = $httpCode;
94
95
            if (! empty($data) && ! $file) {
96
                $this->response->data = $this->parseXmlData($result);
97
            } else {
98
                $this->response->data["statusmessage"] = $result;
99
            }
100
101
            if ($httpCode == "200" && (isset($this->response->data["statusmessage"]) &&
102
                in_array(
103
                    $this->response->data["statusmessage"],
104
                    ["Message Sent Successfully!","In Process.. and check your campaign logs.", "Your Campaign runs successfully. Please check your campaign logs."]
105
                ))) {
106
                $this->response->status = 'success';
107
            } else {
108
                $this->response->status = 'failed';
109
            }
110
            curl_close($ch);
111
        } catch (\Exception $ex) {
112
            $this->response->request = ['request_size' => null, 'base_url' => $this->config['base_url'], 'curl_error' => null, 'content_type' => null, 'redirect_count' => null, 'effective_url' => null, 'total_time' => null];
113
            $this->response->http_code = null;
114
            $this->response->status = 'failed';
115
            $this->response->data = $ex;
116
        }
117
    }
118
119
    private function parseXmlData($result): array
120
    {
121
        $xml_utf8 = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $result);
122
        $simple_xml = simplexml_load_string($xml_utf8);
123
        $xml_to_json = json_encode($simple_xml);
124
125
        return json_decode($xml_to_json, true);
126
    }
127
128
    public function sendSMS($to, $message, $identifier = null, $unique_id = null, $product_id = null, $channel = null, $transaction_id = null): \stdClass
129
    {
130
        $xmldoc = "<SMSRequest><Username>{$this->config['username']}</Username><Password>{$this->config['password']}</Password><From>{$this->config['from_mask']}</From><To>{$to}</To><Message>{$message}</Message><urdu>{$this->config['is_urdu']}</urdu><statuscode>{$this->config['show_status']}</statuscode><Identifier>{$identifier}</Identifier><UniqueId>{$unique_id}</UniqueId><ProductId>{$product_id}</ProductId><Channel>{$channel}</Channel><TransactionId>{$transaction_id}</TransactionId></SMSRequest>";
131
        $this->sendRequest('POST', '/sendsms_xml.html', ['xmldoc' => $xmldoc]);
132
133
        return $this->response;
134
    }
135
136
    public function directSendSMS($to, $message, $identifier = null, $unique_id = null, $product_id = null, $channel = null, $transaction_id = null): \stdClass
137
    {
138
        $parameters = ['Username' => $this->config['username'], 'Password' => $this->config['password'], 'From' => $this->config['from_mask'], 'To' => $to, 'Message' => $message, 'Identifier' => $identifier, 'UniqueId' => $unique_id, 'ProductId' => $product_id, 'Channel' => $channel, 'TransactionId' => $transaction_id];
139
        $this->sendRequest('GET', '/sendsms_url.html?'.http_build_query($parameters));
140
141
        return $this->response;
142
    }
143
144
    public function receivingSMS($from_date = null, $to_date = null): \stdClass
145
    {
146
        $filter_date = "";
147
        if (! empty($from_date) && ! empty($to_date)) {
148
            $filter_date = "<FromDate>{$from_date}</FromDate><ToDate>{$to_date}</ToDate>";
149
        }
150
151
        $xmldoc = "<SMSRequest><Username>{$this->config['username']}</Username><Password>{$this->config['password']}</Password><Shortcode>{$this->config['short_code']}</Shortcode><urdu>{$this->config['is_urdu']}</urdu><statuscode>{$this->config['show_status']}</statuscode>{$filter_date}</SMSRequest>";
152
        $this->sendRequest('POST', '/receivesms_xml.html', ['xmldoc' => $xmldoc]);
153
154
        return $this->response;
155
    }
156
157
    public function balanceInquiry(): \stdClass
158
    {
159
        $parameters = ['Username' => $this->config['username'], 'Password' => $this->config['password']];
160
        $this->sendRequest('GET', '/request_sms_check.html?'.http_build_query($parameters));
161
162
        return $this->response;
163
    }
164
165
    public function sendSMSGroups($group_name, $message): \stdClass
166
    {
167
        $parameters = ['Username' => $this->config['username'], 'Password' => $this->config['password'], 'From' => $this->config['from_mask'],'Group' => $group_name, 'Message' => $message];
168
        $this->sendRequest('GET', '/sendsms_group.html?'.http_build_query($parameters));
169
170
        return $this->response;
171
    }
172
173
    public function sendSMSInternational($to, $message): \stdClass
174
    {
175
        $parameters = ['Username' => $this->config['username'], 'Password' => $this->config['password'], 'From' => $this->config['from_mask'], 'To' => $to, 'Message' => $message];
176
        $this->sendRequest('GET', '/int_api.html?'.http_build_query($parameters));
177
178
        return $this->response;
179
    }
180
181
    public function scheduleJob($contacts_file, $message, $schedule_date_time = null): \stdClass
182
    {
183
        $curl_file = new \CURLFile(realpath($contacts_file));
184
        $parameters = ['Username' => $this->config['username'], 'Password' => $this->config['password'], 'From' => $this->config['from_mask'], 'Message' => $message, 'ScheduleDateTime' => $schedule_date_time, 'file_contents' => $curl_file];
185
        $this->sendRequest('POST', '/upload_txt.html', $parameters, [], true);
186
187
        return $this->response;
188
    }
189
}
190