SmsLive247::send()   B
last analyzed

Complexity

Conditions 8
Paths 40

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 26.9658

Importance

Changes 0
Metric Value
cc 8
nc 40
nop 1
dl 0
loc 63
ccs 12
cts 36
cp 0.3333
crap 26.9658
rs 7.5628
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Djunehor
5
 * Date: 5/25/2019
6
 * Time: 5:51 PM.
7
 */
8
9
namespace Djunehor\Sms\Concrete;
10
11
use GuzzleHttp\Exception\ClientException;
12
use GuzzleHttp\Psr7\Request;
13
14
class SmsLive247 extends Sms
15
{
16
    private $baseUrl = 'http://www.smslive247.com/http/';
17
    private $messageType = false;
18
19
    /**
20
     * Class Constructor.
21
     *
22
     * @param null $message
23
     */
24 1
    public function __construct($message = null)
25
    {
26 1
        $this->username = config('laravel-sms.smslive247.token');
27
28 1
        if ($message) {
29
            $this->text($message);
30
        }
31
32 1
        $this->client = self::getInstance();
33 1
        $this->request = new Request('GET', $this->baseUrl.'index.aspx');
34 1
    }
35
36
    public function type(bool $type)
37
    {
38
        $this->messageType = $type;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param null $text
45
     * @return bool
46
     */
47 1
    public function send($text = null): bool
48
    {
49 1
        if ($text) {
50
            $this->setText($text);
51
        }
52
53
        // get sessionID
54
55
        try {
56 1
            $response = $this->client->send($this->request, [
57
                'query' => [
58 1
                    'cmd' => 'login',
59 1
                    'owner_email' => config('laravel-sms.smslive247.owner_email'),
60 1
                    'subacct' => config('laravel-sms.smslive247.subacct_username'),
61 1
                    'subacctpwd' => config('laravel-sms.smslive247.subacct_password'),
62
                ],
63
            ]);
64
65 1
            $response = json_decode($response->getBody()->getContents(), true);
66
67 1
            $split = explode(':', $response);
68
69 1
            if ($split[0] == 'OK' && isset($split[1])) {
70
                $sessionId = trim($split[1]);
71
            } else {
72 1
                $this->response = last($split);
73
74 1
                return false;
75
            }
76
        } catch (\Exception $exception) {
77
            $this->httpError = $exception;
78
79
            return false;
80
        }
81
        try {
82
            $response = $this->client->send($this->request, [
83
                'query' => [
84
                    'cmd' => 'sendmsg',
85
                    'sessionid' => $sessionId,
86
                    'sendto' => implode(',', $this->recipients),
87
                    'sender' => $this->sender ?? config('laravel-sms.sender'),
88
                    'message' => $this->text,
89
                    'msgtype' => (int) $this->messageType,
90
                ],
91
            ]);
92
93
            $response = json_decode($response->getBody()->getContents(), true);
94
            $split = explode(':', $response);
95
            $this->response = last($split);
96
97
            return $split[0] == 'OK' ? true : false;
98
        } catch (ClientException $e) {
99
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
100
            $this->httpError = $e;
101
102
            return false;
103
        } catch (\Exception $e) {
104
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
105
            $this->httpError = $e;
106
107
            return false;
108
        }
109
    }
110
}
111