Completed
Push — master ( fcb419...62d24f )
by Kazi Mainuddin
03:08
created

Textlocal::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
namespace Tzsk\Sms\Drivers;
3
4
5
use GuzzleHttp\Client;
6
use Tzsk\Sms\Contract\SendSmsInterface;
7
8
class Textlocal extends MasterDriver implements SendSmsInterface
9
{
10
    /**
11
     * Textlocal Settings.
12
     *
13
     * @var object|null
14
     */
15
    protected $settings = null;
16
17
    /**
18
     * Http Client.
19
     *
20
     * @var Client|null
21
     */
22
    protected $client = null;
23
24
    /**
25
     * Construct the class with the relevant settings.
26
     *
27
     * SendSmsInterface constructor.
28
     * @param array $settings
29
     */
30
    public function __construct($settings)
31
    {
32
        $this->settings = (object) $settings;
33
        $this->client = new Client();
34
    }
35
36
    /**
37
     * Send text message and return response.
38
     *
39
     * @return mixed
40
     */
41
    public function send()
42
    {
43
        $numbers = implode(",", $this->recipients);
44
45
        $response = $this->client->request("POST", $this->settings->url, [
46
            "form_params" => [
47
                "username" => $this->settings->username,
48
                "hash" => $this->settings->hash,
49
                "numbers" => $numbers,
50
                "sender" => urlencode($this->settings->sender),
51
                "message" => $this->body,
52
            ],
53
        ]);
54
55
        $data = $this->getResponseData($response);
56
57
        return (object) array_merge($data, ["status" => true]);
58
59
    }
60
61
    /**
62
     * Get the response data.
63
     *
64
     * @param  object $response
65
     * @return array|object
66
     */
67
    protected function getResponseData($response)
68
    {
69
        if ($response->getStatusCode() != 200) {
70
            return (object) ["status" => false, "message" => "Request Error. " . $response->getReasonPhrase()];
71
        }
72
73
        $data = json_decode((string) $response->getBody(), true);
74
75
        if ($data["status"] != "success") {
76
            return (object) ["status" => false, "message" => "Something went wrong.", "data" => $data];
77
        }
78
79
        return $data;
80
    }
81
}
82