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

Nexmo::send()   B

Complexity

Conditions 5
Paths 20

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 20
nop 1
dl 0
loc 39
ccs 0
cts 32
cp 0
crap 30
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 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
        $this->username = config('laravel-sms.nexmo.api_key');
19
        $this->password = config('laravel-sms.nexmo.api_secret');
20
        if ($message) {
21
            $this->text($message);
22
        }
23
24
        $this->client = self::getInstance();
25
        $this->request = new Request('POST', $this->baseUrl.'json');
26
    }
27
28
    /**
29
     * @param null $text
30
     * @return bool
31
     */
32
    public function send($text = null): bool
33
    {
34
        if ($text) {
35
            $this->setText($text);
36
        }
37
        try {
38
            $request = $this->client->send($this->request, [
39
                'form_params' => [
40
                    'to' => implode(',', $this->recipients),
41
                    'from' => $this->sender ?? config('laravel-sms.sender'),
42
                    'api_key' => $this->username,
43
                    'api_secret' => $this->password,
44
                    'text' => $this->text,
45
                ],
46
            ]);
47
48
            $response = json_decode($request->getBody()->getContents(), true);
49
50
            if ($response['messages'][0]['status'] == 0) {
51
                $this->response = 'The message was sent successfully';
52
53
                return true;
54
            }
55
56
            $this->response = $response['messages'][0]['error-text'];
57
58
            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