Nexmo::send()   B
last analyzed

Complexity

Conditions 5
Paths 20

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.7355

Importance

Changes 0
Metric Value
cc 5
nc 20
nop 1
dl 0
loc 39
ccs 12
cts 23
cp 0.5217
crap 7.7355
rs 8.9848
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 Nexmo extends Sms
9
{
10
    private $baseUrl = 'https://rest.nexmo.com/sms/';
11
12
    /**
13
     * Class Constructor.
14
     * @param null $message
15
     */
16 2 View Code Duplication
    public function __construct($message = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18 2
        $this->username = config('laravel-sms.nexmo.api_key');
19 2
        $this->password = config('laravel-sms.nexmo.api_secret');
20 2
        if ($message) {
21 1
            $this->text($message);
22
        }
23
24 2
        $this->client = self::getInstance();
25 2
        $this->request = new Request('POST', $this->baseUrl.'json');
26 2
    }
27
28
    /**
29
     * @param null $text
30
     * @return bool
31
     */
32 2
    public function send($text = null): bool
33
    {
34 2
        if ($text) {
35
            $this->setText($text);
36
        }
37
        try {
38 2
            $request = $this->client->send($this->request, [
39
                'form_params' => [
40 2
                    'to' => implode(',', $this->recipients),
41 2
                    'from' => $this->sender ?? config('laravel-sms.sender'),
42 2
                    'api_key' => $this->username,
43 2
                    'api_secret' => $this->password,
44 2
                    'text' => $this->text,
45
                ],
46
            ]);
47
48 2
            $response = json_decode($request->getBody()->getContents(), true);
49
50 2
            if ($response['messages'][0]['status'] == 0) {
51
                $this->response = 'The message was sent successfully';
52
53
                return true;
54
            }
55
56 2
            $this->response = $response['messages'][0]['error-text'];
57
58 2
            return false;
59
        } catch (ClientException $e) {
60
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
61
            $this->httpError = $e;
62
63
            return false;
64
        } catch (\Exception $e) {
65
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
66
            $this->httpError = $e;
67
68
            return false;
69
        }
70
    }
71
}
72