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

Smsir::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
            $body = [
77
                'ParameterArray' => $params,
78
                'TemplateId' => $template_id,
79
                'Mobile' => $recipient,
80
            ];
81
            $response = $this->client->post(
82
                $this->settings->url.'api/UltraFastSend',
83
                [
84
                    'json' => $body,
85
                    'headers' => [
86
                        'x-sms-ir-secure-token' => $this->getToken(),
87
                    ],
88
                    'connect_timeout' => 30
89
                ]
90
            );
91
            $responses[$recipient] = $this->getResponseData($response);
92
        }
93
94
        return (object) $responses;
95
    }
96
97
    /**
98
     * Send text message and return response.
99
     *
100
     * @return object
101
     * @throws \GuzzleHttp\Exception\GuzzleException
102
     */
103
    public function send()
104
    {
105
        // Get token:
106
        $response = $this->getToken();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getToken(); of type array|object adds the type array to the return on line 108 which is incompatible with the return type declared by the abstract method Tzsk\Sms\Abstracts\Driver::send of type object.
Loading history...
107
        if ($response['status'] == false) {
108
            return $response;
109
        }
110
        $token = $response['data']['TokenKey'];
111
112
        // Create message:
113
        $body = [
114
            'Messages' => [$this->body],
115
            'MobileNumbers' => $this->recipients,
116
            'LineNumber' => $this->settings->from,
117
        ];
118
119
        // Send message:
120
        $response = $this->client->request("POST", $this->settings->url.'api/MessageSend',
121
            [
122
                'json' => $body,
123
                'headers' => [
124
                    'x-sms-ir-secure-token' => $token
125
                ],
126
                'connect_timeout' => 30
127
            ]
128
        );
129
130
        $data = $this->getResponseData($response);
131
132
        return (object) $data;
133
    }
134
135
    /**
136
     * Get the response data.
137
     *
138
     * @param  object $response
139
     * @return array|object
140
     */
141
    protected function getResponseData($response)
142
    {
143
        if ($response->getStatusCode() != 200 && $response->getStatusCode() != 201) {
144
            return ["status" => false, "message" => "Request Error. " . $response->getReasonPhrase()];
145
        }
146
147
        $data = json_decode((string) $response->getBody(), true);
148
149
        if ($data["IsSuccessful"] == false) {
150
            return ["status" => false, "message" => "Something went wrong.", "data" => $data];
151
        }
152
153
        return ["status" => true, "message" => "", "data" => $data];
154
    }
155
}
156