Completed
Push — master ( 56566c...f8ca6f )
by Muhammad
06:54
created

ApiDriver::setUserAgent()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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