Nexmo   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 17.19 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 64.52%

Importance

Changes 0
Metric Value
dl 11
loc 64
ccs 20
cts 31
cp 0.6452
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
B send() 0 39 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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