InfoBip::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 17
ccs 11
cts 12
cp 0.9167
crap 2.0023
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Djunehor\Sms\Concrete;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Psr7\Request;
7
8
class InfoBip extends Sms
9
{
10
    private $baseUrl;
11
12
    /**
13
     * Class Constructor.
14
     * @param null $message
15
     */
16 1
    public function __construct($message = null)
17
    {
18 1
        $this->username = config('laravel-sms.infobip.username');
19 1
        $this->password = config('laravel-sms.infobip.password');
20 1
        $this->baseUrl = config('laravel-sms.infobip.base_url', 'http://infobio.com/');
21 1
        if ($message) {
22
            $this->text($message);
23
        }
24
        $headers = [
25 1
            'Authorization' => 'Basic '.base64_encode("$this->username:$this->password"),
26 1
            'Content-Type' => 'application/json',
27 1
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 OPR/47.0.2631.39',
28
        ];
29
30 1
        $this->client = self::getInstance();
31 1
        $this->request = new Request('POST', $this->baseUrl.'/sms/2/text/single', $headers);
32 1
    }
33
34
    /**
35
     * @param null $text
36
     * @return bool
37
     */
38 1
    public function send($text = null): bool
39
    {
40 1
        if ($text) {
41
            $this->setText($text);
42
        }
43
        try {
44 1
            $request = $this->client->send($this->request, [
45
                'form_params' => [
46 1
                    'from' => $this->sender ?? config('laravel-sms.sender'),
47 1
                    'to' => implode(',', $this->recipients),
48 1
                    'text' => $this->text,
49
                ],
50
            ]);
51
52
            $response = json_decode($request->getBody()->getContents(), true);
53
            $this->response = $response;
54
55
            return $request->getStatusCode() == 200 ? true : false;
56 1
        } catch (ClientException $e) {
57
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
58
            $this->httpError = $e;
59
60
            return false;
61 1
        } catch (\Exception $e) {
62 1
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
63 1
            $this->httpError = $e;
64
65 1
            return false;
66
        }
67
    }
68
}
69