Completed
Pull Request — master (#13)
by
unknown
02:02
created

Smsir::ultraFastSend()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
cc 4
nc 6
nop 2
1
<?php
2
namespace Tzsk\Sms\Drivers;
3
4
use GuzzleHttp\Client;
5
use Tzsk\Sms\Abstracts\Driver;
6
7
class Smsir extends Driver
8
{
9
    /**
10
     * Smsir Settings.
11
     *
12
     * @var null|object
13
     */
14
    protected $settings = null;
15
16
    /**
17
     * Smsir Client.
18
     *
19
     * @var null|Client
20
     */
21
    protected $client = null;
22
23
    /**
24
     * Construct the class with the relevant settings.
25
     *
26
     * SendSmsInterface constructor.
27
     * @param $settings object
28
     */
29
    public function __construct($settings)
30
    {
31
        $this->settings = (object) $settings;
32
        $this->client = new Client();
33
    }
34
35
    /**
36
     * Get token.
37
     *
38
     * @return mixed - the Token for use api
39
     */
40
    private function getToken()
41
    {
42
        $body = [
43
            'UserApiKey' => $this->settings->apiKey,
44
            'SecretKey'=> $this->settings->secretKey,
45
        ];
46
        $response = $this->client->post(
47
            $this->settings->url.'api/Token',
48
            [
49
                'json' => $body,
50
                'connect_timeout' => 30
51
            ]
52
        );
53
54
        return $this->getResponseData($response);
55
    }
56
57
    /**
58
     * Send with ultraFast method
59
     *
60
     * @param array $parameters
61
     * @param $template_id
62
     * @param $number
63
     * @return mixed
64
     */
65
    public function ultraFastSend(array $parameters, $template_id)
66
    {
67
        $responses = [];
68
69
        $params = [];
70
71
        foreach ($parameters as $key => $value) {
72
            $params[] = ['Parameter' => $key, 'ParameterValue' => $value];
73
        }
74
75
        foreach ($this->recipients as $recipient) {
76
77
            // Get token:
78
            $response = $this->getToken();
79
            if ($response['status'] == false) {
80
                $responses[$recipient] = (object) $response;
81
                continue;
82
            }
83
            $token = $response['data']['TokenKey'];
84
85
            $body = [
86
                'ParameterArray' => $params,
87
                'TemplateId' => $template_id,
88
                'Mobile' => $recipient,
89
            ];
90
            $response = $this->client->post(
91
                $this->settings->url.'api/UltraFastSend',
92
                [
93
                    'json' => $body,
94
                    'headers' => [
95
                        'x-sms-ir-secure-token' => $token,
96
                    ],
97
                    'connect_timeout' => 30
98
                ]
99
            );
100
            $responses[$recipient] = $this->getResponseData($response);
101
        }
102
103
        return (object) $responses;
104
    }
105
106
    /**
107
     * Send text message and return response.
108
     *
109
     * @return object
110
     * @throws \GuzzleHttp\Exception\GuzzleException
111
     */
112
    public function send()
113
    {
114
        // Get token:
115
        $response = $this->getToken();
116
        if ($response['status'] == false) {
117
            return (object) $response;
118
        }
119
        $token = $response['data']['TokenKey'];
120
121
        // Create message:
122
        $body = [
123
            'Messages' => [$this->body],
124
            'MobileNumbers' => $this->recipients,
125
            'LineNumber' => $this->settings->from,
126
        ];
127
128
        // Send message:
129
        $response = $this->client->request("POST", $this->settings->url.'api/MessageSend',
130
            [
131
                'json' => $body,
132
                'headers' => [
133
                    'x-sms-ir-secure-token' => $token
134
                ],
135
                'connect_timeout' => 30
136
            ]
137
        );
138
139
        $data = $this->getResponseData($response);
140
141
        return (object) $data;
142
    }
143
144
    /**
145
     * Get the response data.
146
     *
147
     * @param  object $response
148
     * @return array|object
149
     */
150
    protected function getResponseData($response)
151
    {
152
        if ($response->getStatusCode() != 200 && $response->getStatusCode() != 201) {
153
            return ["status" => false, "message" => "Request Error. " . $response->getReasonPhrase()];
154
        }
155
156
        $data = json_decode((string) $response->getBody(), true);
157
158
        if ($data["IsSuccessful"] == false) {
159
            return ["status" => false, "message" => "Something went wrong.", "data" => $data];
160
        }
161
162
        return ["status" => true, "message" => "", "data" => $data];
163
    }
164
}
165