Passed
Push — master ( 7d6da9...87fa1f )
by Zacchaeus
01:25 queued 10s
created

KudiSms   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 67.65%

Importance

Changes 0
Metric Value
dl 67
loc 67
ccs 23
cts 34
cp 0.6765
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 2
B send() 37 37 6

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 View Code Duplication
class KudiSms extends Sms
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    private $baseUrl = 'https://account.kudisms.net/';
11
12
    /**
13
     * Class Constructor.
14
     * @param null $message
15
     */
16 1
    public function __construct($message = null)
17
    {
18 1
        $this->username = config('laravel-sms.kudi_sms.username');
19 1
        $this->password = config('laravel-sms.kudi_sms.password');
20 1
        if ($message) {
21
            $this->text($message);
22
        }
23
        $headers = [
24 1
                'apiKey' => $this->username,
25 1
                'Content-Type' => 'application/x-www-form-urlencoded',
26 1
                'Accept' => 'application/json',
27 1
                'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 OPR/47.0.2631.39',
28
            ];
29 1
        $this->client = self::getInstance();
30 1
        $this->request = new Request('GET', $this->baseUrl.'api/', $headers);
31 1
    }
32
33
    /**
34
     * @param null $text
35
     * @return bool
36
     */
37 1
    public function send($text = null): bool
38
    {
39 1
        if ($text) {
40
            $this->setText($text);
41
        }
42
        try {
43 1
            $request = $this->client->send($this->request, [
44
                'query' => [
45 1
                    'username' => $this->username,
46 1
                    'password' => $this->password,
47 1
                    'sender' => $this->sender ?? config('laravel-sms.sender'),
48 1
                    'mobiles' => implode(',', $this->recipients),
49 1
                    'message' => $this->text,
50
                ],
51
            ]);
52
53 1
            $response = json_decode($request->getBody()->getContents(), true);
54
55 1
            if (isset($response['status']) && $response['status'] == 'OK') {
56
                return true;
57
            }
58
59 1
            $this->response = $response['error'];
60
61 1
            return false;
62
        } catch (ClientException $e) {
63
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
64
            $this->httpError = $e;
65
66
            return false;
67
        } catch (\Exception $e) {
68
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
69
            $this->httpError = $e;
70
71
            return false;
72
        }
73
    }
74
}
75