Jusibe   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 174
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A prepareRequest() 0 4 1
A performGetRequest() 0 6 1
A performPostRequest() 0 7 1
A sendSMS() 0 10 2
A sendBulkSMS() 0 14 3
A checkAvailableCredits() 0 6 1
A checkDeliveryStatus() 0 10 2
A checkBulkDeliveryStatus() 0 10 2
A getResponse() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Jusibe PHP library.
5
 *
6
 * (c) Prosper Otemuyiwa <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Unicodeveloper\Jusibe;
13
14
use GuzzleHttp\Client;
15
use Unicodeveloper\Jusibe\Exceptions\IsNull;
16
use Unicodeveloper\Jusibe\Exceptions\IsEmpty;
17
18
class Jusibe {
19
20
    /**
21
     * Jusibe API Base Url
22
     */
23
    const baseURL = 'https://jusibe.com';
24
25
    /**
26
     * Public key
27
     * @var string
28
     */
29
    protected $publicKey;
30
31
    /**
32
     * Access token
33
     * @var string
34
     */
35
    protected $accessToken;
36
37
    /**
38
     *  Response from requests made to Jusibe
39
     * @var mixed
40
     */
41
    protected $response;
42
43
    /**
44
     * Instance of Guzzle Client
45
     * @var object
46
     */
47
    protected $client;
48
49
    /**
50
     * Constructor
51
     * @param $publicKey   string
52
     * @param $accessToken string
53
     */
54
    public function __construct($publicKey = null, $accessToken = null)
55
    {
56
        if (is_null($publicKey)) {
57
            throw IsNull::create("The Public Key can not be null. Please pass it to the constructor");
58
        }
59
60
        if (is_null($accessToken)) {
61
            throw IsNull::create("The Access Token can not be null. Please pass it to the constructor");
62
        }
63
64
        $this->publicKey = $publicKey;
65
        $this->accessToken = $accessToken;
66
        $this->prepareRequest();
67
    }
68
69
    /**
70
     * Instantiate Guzzle Client and prepare request for http operations
71
     * @return none
72
     */
73
    private function prepareRequest()
74
    {
75
        $this->client = new Client(['base_uri' => self::baseURL]);
76
    }
77
78
    /**
79
     * Perform a GET request
80
     * @param  string $relativeUrl
81
     * @return none
82
     */
83
    private function performGetRequest($relativeUrl)
84
    {
85
        $this->response = $this->client->request('GET', $relativeUrl, [
86
            'auth' => [$this->publicKey, $this->accessToken]
87
        ]);
88
    }
89
90
    /**
91
     * Perform a POST request
92
     * @param  string $relativeUrl
93
     * @param  array $data
94
     * @return none
95
     */
96
    private function performPostRequest($relativeUrl, $data)
97
    {
98
        $this->response = $this->client->request('POST', $relativeUrl, [
99
            'auth' => [$this->publicKey, $this->accessToken],
100
            'form_params' => $data
101
        ]);
102
    }
103
104
    /**
105
     * Send SMS using the Jusibe API
106
     * @param  array $payload
107
     * @return $this
108
     */
109
    public function sendSMS($payload = [])
110
    {
111
        if (empty($payload)) {
112
            throw IsEmpty::create("Message Payload can not be empty. Please fill the appropriate details");
113
        }
114
115
        $this->performPostRequest('/smsapi/send_sms', $payload);
116
117
        return $this;
118
    }
119
120
    /**
121
     * Send Bulk SMS using the Jusibe API
122
     * @param  array $payload
123
     * @return $this
124
     */
125
    public function sendBulkSMS($payload = [])
126
    {
127
        if (empty($payload)) {
128
            throw IsEmpty::create("Message Payload can not be empty. Please fill the appropriate details");
129
        }
130
131
        if(empty($payload['to'])){
132
            throw IsEmpty::create("Message destination can not be empty.");
133
        }
134
135
        $this->performPostRequest('/smsapi/bulk/send_sms', $payload);
136
137
        return $this;
138
    }
139
140
    /**
141
     * Check the available SMS credits left in your JUSIBE account
142
     * @return $this
143
     */
144
    public function checkAvailableCredits()
145
    {
146
        $this->performGetRequest('/smsapi/get_credits');
147
148
        return $this;
149
    }
150
151
    /**
152
     * Check the delivery status of a sent SMS
153
     * @param  string $messageID
154
     * @return $this
155
     */
156
    public function checkDeliveryStatus($messageID = null)
157
    {
158
        if (is_null($messageID)) {
159
            throw IsNull::create("Message ID can not be empty.");
160
        }
161
162
        $this->performGetRequest("/smsapi/delivery_status?message_id={$messageID}");
163
164
        return $this;
165
    }
166
167
    /**
168
     * Check the delivery status of a sent bulk SMS
169
     * @param  string $bulkID
170
     * @return $this
171
     */
172
    public function checkBulkDeliveryStatus($bulkID = null)
173
    {
174
        if (is_null($bulkID)) {
175
            throw IsNull::create("Bulk ID can not be empty.");
176
        }
177
178
        $this->performGetRequest("/smsapi/bulk/status?bulk_message_id={$bulkID}");
179
180
        return $this;
181
    }
182
183
    /**
184
     * Return the response object of any operation
185
     * @return object
186
     */
187
    public function getResponse()
188
    {
189
        return json_decode($this->response->getBody());
190
    }
191
}
192