MeboSms::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.9332
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 MeboSms extends Sms
9
{
10
    private $baseUrl = 'http://mebosms.com/';
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.mebo_sms.api_key');
19 1
        if ($message) {
20
            $this->text($message);
21
        }
22
23 1
        $this->client = self::getInstance();
24 1
        $this->request = new Request('GET', $this->baseUrl.'api');
25 1
    }
26
27
    /**
28
     * @param null $text
29
     * @return bool
30
     */
31 1
    public function send($text = null): bool
32
    {
33 1
        if ($text) {
34
            $this->setText($text);
35
        }
36
        try {
37 1
            $request = $this->client->send($this->request, [
38
                'query' => [
39 1
                    'apikey' => $this->username,
40 1
                    'sender' => $this->sender ?? config('laravel-sms.sender'),
41 1
                    'destination' => implode(',', $this->recipients),
42 1
                    'mssg' => $this->text,
43 1
                    'dnd' => config('laravel-sms.mebo_sms.dnd'),
44
                ],
45
            ]);
46
47 1
            $response = json_decode($request->getBody()->getContents(), true);
48
49 1
            if (isset($response['status']) && $response['status'] == 'OK') {
50
                return true;
51
            }
52
53 1
            $this->response = $response['error'];
54
55 1
            return false;
56
        } catch (ClientException $e) {
57
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
58
            $this->httpError = $e;
59
60
            return false;
61
        } catch (\Exception $e) {
62
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
63
            $this->httpError = $e;
64
65
            return false;
66
        }
67
    }
68
}
69