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

Smsir::getResponseData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
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 json_decode((string) $response->getBody(), true)['TokenKey'];
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 = [
0 ignored issues
show
Unused Code introduced by
$body is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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' => $this->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
     */
102
    public function send()
103
    {
104
        $body = [
105
            'Messages' => $this->body,
106
            'MobileNumbers' => [$this->recipients],
107
            'LineNumber' => $this->settings->from,
108
        ];
109
        $response = $this->client->request("POST", $this->settings->url.'api/MessageSend',
110
            [
111
                'json' => $body,
112
                'headers' => [
113
                    'x-sms-ir-secure-token' => $this->getToken()
114
                ],
115
                'connect_timeout' => 30
116
            ]
117
        );
118
119
        $data = $this->getResponseData($response);
120
121
        return (object) array_merge($data, ["status" => true]);
122
    }
123
124
    /**
125
     * Get the response data.
126
     *
127
     * @param  object $response
128
     * @return array|object
129
     */
130
    protected function getResponseData($response)
131
    {
132
        if ($response->getStatusCode() != 200) {
133
            return ["status" => false, "message" => "Request Error. " . $response->getReasonPhrase()];
134
        }
135
136
        $data = json_decode((string) $response->getBody(), true);
137
138
        if ($data["status"] != "success") {
139
            return ["status" => false, "message" => "Something went wrong.", "data" => $data];
140
        }
141
142
        return $data;
143
    }
144
}
145