Completed
Push — master ( cd3bab...9e6217 )
by
unknown
03:06
created

ApiDriver   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 7
dl 0
loc 174
rs 10
c 0
b 0
f 0

13 Methods

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