ApiDriver   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.63%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 7
dl 0
loc 174
ccs 72
cts 73
cp 0.9863
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 24 1
A setCallback() 0 6 1
A setDestination() 0 10 2
A getDestination() 0 8 2
A setContent() 0 10 2
A getContent() 0 8 2
A setUrl() 0 10 2
A getUrl() 0 8 2
A setKey() 0 10 2
A getKey() 0 8 2
A setUserAgent() 0 16 3
A getDefaultUrl() 0 12 2
1
<?php
2
3
namespace NotificationChannels\Gammu\Drivers;
4
5
use Illuminate\Contracts\Config\Repository;
6
use GuzzleHttp\Client;
7
use NotificationChannels\Gammu\Exceptions\CouldNotSendNotification;
8
9
class ApiDriver extends DriverAbstract
10
{
11
    protected $callback;
12
13
    protected $config;
14
15
    protected $client;
16
17
    protected $url;
18
19
    protected $key;
20
21
    protected $ua;
22
23
    protected $data = [];
24
25
    protected $chunks = [];
26
27
    public $destination;
28
29
    public $content;
30
31
    public $apiStatusCode;
32
33
    public $apiResponseBody;
34
35 14
    public function __construct(Repository $config, Client $client)
36
    {
37 14
        $this->config = $config;
38 14
        $this->client = $client;
39 14
    }
40
41 1
    public function send($phoneNumber, $content, $sender = null, $callback = null)
42
    {
43 1
        $this->getUrl();
44 1
        $this->setCallback($callback);
45 1
        $this->setKey();
46 1
        $this->setUserAgent();
47 1
        $this->setDestination($phoneNumber);
48 1
        $this->setContent($content);
49
50 1
        $response = $this->client->post($this->url, [
51
            'json' => [
52 1
                'key' => $this->key,
53 1
                'to' => $this->destination,
54 1
                'message' => $this->content,
55 1
                'callback' => $this->callback,
56
            ],
57
            'headers' => [
58 1
                'user-agent' => $this->ua,
59
            ],
60
        ]);
61
62 1
        $this->apiStatusCode = $response->getStatusCode();
63 1
        $this->apiResponseBody = $response->getBody()->getContents();
64 1
    }
65
66 1
    public function setCallback($callback)
67
    {
68 1
        $this->callback = $callback;
69
70 1
        return $this;
71
    }
72
73 3
    public function setDestination($phoneNumber)
74
    {
75 3
        if (empty($phoneNumber)) {
76 1
            throw CouldNotSendNotification::destinationNotProvided();
77
        }
78
79 2
        $this->destination = trim($phoneNumber);
80
81 2
        return $this;
82
    }
83
84 2
    public function getDestination()
85
    {
86 2
        if (empty($this->destination)) {
87 1
            throw CouldNotSendNotification::destinationNotProvided();
88
        }
89
90 1
        return $this->destination;
91
    }
92
93 3
    public function setContent($content)
94
    {
95 3
        if (empty($content)) {
96 1
            throw CouldNotSendNotification::contentNotProvided();
97
        }
98
99 2
        $this->content = $content;
100
101 2
        return $this;
102
    }
103
104 2
    public function getContent()
105
    {
106 2
        if (empty($this->content)) {
107 1
            throw CouldNotSendNotification::contentNotProvided();
108
        }
109
110 1
        return $this->content;
111
    }
112
113 1
    public function setUrl($url)
114
    {
115 1
        if (empty($url)) {
116
            $this->url = $this->getDefaultUrl();
117
        }
118
119 1
        $this->url = $url;
120
121 1
        return $this;
122
    }
123
124 5
    public function getUrl()
125
    {
126 5
        if (empty($this->url)) {
127 4
            return $this->getDefaultUrl();
128
        }
129
130 1
        return $this->url;
131
    }
132
133 3
    public function setKey()
134
    {
135 3
        $this->key = $this->config->get('services.gammu.auth');
136
137 3
        if (empty($this->key)) {
138 1
            throw CouldNotSendNotification::authKeyNotProvided();
139
        }
140
141 2
        return $this;
142
    }
143
144 2
    public function getKey()
145
    {
146 2
        if (empty($this->key)) {
147 1
            throw CouldNotSendNotification::authKeyNotProvided();
148
        }
149
150 1
        return $this->key;
151
    }
152
153 1
    public function setUserAgent()
154
    {
155
        $userAgents = [
156 1
            $this->getSignature(),
157 1
            'GuzzleHttp/'.Client::VERSION,
158 1
            'PHP/'.PHP_VERSION,
159
        ];
160
161 1
        if (extension_loaded('curl') && function_exists('curl_version')) {
162 1
            array_push($userAgents, 'curl/'.\curl_version()['version']);
163
        }
164
165 1
        $this->ua = collect($userAgents)->implode(' ');
166
167 1
        return $this;
168
    }
169
170 4
    private function getDefaultUrl()
171
    {
172 4
        $url = $this->config->get('services.gammu.url');
173
174 4
        if (empty($url)) {
175 2
            throw CouldNotSendNotification::apiUrlNotProvided();
176
        }
177
178 2
        $this->url = $url;
179
180 2
        return $this->url;
181
    }
182
}
183