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

BulkSmsNigeria   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
B send() 0 32 6
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