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

BulkSmsNigeria::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

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 11
ccs 0
cts 9
cp 0
crap 6
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Djunehor
5
 * Date: 1/22/2019
6
 * Time: 9:36 AM.
7
 */
8
9
namespace Djunehor\Sms\Concrete;
10
11
use GuzzleHttp\Exception\ClientException;
12
use GuzzleHttp\Psr7\Request;
13
14
class BulkSmsNigeria extends Sms
15
{
16
    private $baseUrl = 'https://www.bulksmsnigeria.com/api/v1/sms/';
17
18
    /**
19
     * Class Constructor.
20
     * @param null $message
21
     */
22
    public function __construct($message = null)
23
    {
24
        $this->username = config('laravel-sms.bulk_sms_nigeria.token');
25
26
        if ($message) {
27
            $this->text($message);
28
        }
29
30
        $this->client = self::getInstance();
31
        $this->request = new Request('POST', $this->baseUrl.'create');
32
    }
33
34
    /**
35
     * @param null $text
36
     * @return bool
37
     */
38
    public function send($text = null): bool
39
    {
40
        if ($text) {
41
            $this->setText($text);
42
        }
43
        try {
44
            $response = $this->client->send($this->request, [
45
                'form_params' => [
46
                    'api_token' => config('laravel-sms.bulk_sms_nigeria.token'),
47
                    'to' => implode(',', $this->recipients),
48
                    'from' => $this->sender ?? config('laravel-sms.sender'),
49
                    'body' => $this->text,
50
                    'dnd' => config('laravel-sms.bulk_sms_nigeria.dnd'),
51
                ],
52
            ]);
53
54
            $response = json_decode($response->getBody()->getContents(), true) ?? [];
55
            $this->response = array_key_exists('error', $response) ? $response['error']['message'] : $response['data']['message'];
56
57
            return $response['data']['status'] == 'success' ? true : false;
58
        } catch (ClientException $e) {
59
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
60
            $this->httpError = $e;
61
62
            return false;
63
        } catch (\Exception $e) {
64
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
65
            $this->httpError = $e;
66
67
            return false;
68
        }
69
    }
70
}
71