Test Failed
Pull Request — master (#7)
by Zacchaeus
16:16 queued 01:09
created

MeboSms::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 6
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
    public function __construct($message = null)
17
    {
18
        $this->username = config('laravel-sms.mebo_sms.api_key');
19
        if ($message) {
20
            $this->text($message);
21
        }
22
23
        $this->client = self::getInstance();
24
        $this->request = new Request('GET', $this->baseUrl.'api');
25
    }
26
27
    /**
28
     * @param null $text
29
     * @return bool
30
     */
31
    public function send($text = null): bool
32
    {
33
        if ($text) {
34
            $this->setText($text);
35
        }
36
        try {
37
            $request = $this->client->send($this->request, [
38
                'query' => [
39
                    'apikey' => $this->username,
40
                    'sender' => $this->sender ?? config('laravel-sms.sender'),
41
                    'destination' => implode(',', $this->recipients),
42
                    'mssg' => $this->text,
43
                    'dnd' => config('laravel-sms.mebo_sms.dnd'),
44
                ],
45
            ]);
46
47
            $response = json_decode($request->getBody()->getContents(), true);
48
49
            if (isset($response['status']) && $response['status'] == 'OK') {
50
                return true;
51
            }
52
53
            $this->response = $response['error'];
54
55
            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