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

XWireless::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 12
loc 12
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Djunehor
5
 * Date: 1/22/2019
6
 * Time: 9:36 AM.
7
 */
8
9
namespace Djunehor\Sms\Concrete;
10
11
use GuzzleHttp\Exception\ClientException;
12
use GuzzleHttp\Psr7\Request;
13
14
class XWireless extends Sms
15
{
16
    private $responseCodes = [
17
        'OK' => 'Successful',
18
19
        '2904' => 'SMS Sending Failed',
20
21
        '2905' => 'Invalid username/password combination',
22
23
        '2906' => 'Credit exhausted',
24
25
        '2907' => 'Gateway unavailable',
26
27
        '2908' => 'Invalid schedule date format',
28
29
        '2909' => 'Unable to schedule',
30
31
        '2910' => 'Username is empty',
32
33
        '2911' => 'Password is empty',
34
35
        '2912' => 'Recipient is empty',
36
37
        '2913=Message is empty',
38
39
        '2914=Sender is empty',
40
41
        '2915' => 'One or more required fields are empty',
42
    ];
43
    private $baseUrl = 'https://secure.xwireless.net/api/v2/';
44
45
    /**
46
     * Class Constructor.
47
     * @param null $message
48
     */
49 1 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...
50
    {
51 1
        $this->username = config('laravel-sms.x_wireless.api_key');
52 1
        $this->password = config('laravel-sms.x_wireless.client_id');
53
54 1
        if ($message) {
55
            $this->text($message);
56
        }
57
58 1
        $this->client = self::getInstance();
59 1
        $this->request = new Request('GET', $this->baseUrl.'SendSMS');
60 1
    }
61
62
    public function getResponse()
63
    {
64
        $split = explode(' ', $this->response);
65
66
        return array_key_exists($split[0], $this->responseCodes) ? $this->responseCodes[$split[0]] : '';
67
    }
68
69
    /**
70
     * @param null $text
71
     * @return bool
72
     */
73 1
    public function send($text = null): bool
74
    {
75 1
        if ($text) {
76
            $this->setText($text);
77
        }
78
        try {
79 1
            $response = $this->client->send($this->request, [
80
                'query' => [
81 1
                    'ApiKey' => $this->username,
82 1
                    'ClientId' => $this->password,
83 1
                    'MobileNumbers' => implode(',', $this->recipients),
84 1
                    'SenderId' => $this->sender ?? config('laravel-sms.sender'),
85 1
                    'message' => $this->text,
86
                ],
87
            ]);
88
89 1
            $this->response = json_decode($response->getBody()->getContents(), true);
90
91 1
            return $this->response['ErrorDescription'] == 'Success' ? true : false;
92
        } catch (ClientException $e) {
93
            logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
94
            $this->httpError = $e;
95
96
            return false;
97
        } catch (\Exception $e) {
98
            logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage());
99
            $this->httpError = $e;
100
101
            return false;
102
        }
103
    }
104
}
105