1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Djunehor\Sms\Concrete; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
|
8
|
|
View Code Duplication |
class NigerianBulkSms extends Sms |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
private $baseUrl = 'http://portal.nigeriabulksms.com/'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Constructor. |
14
|
|
|
* @param null $message |
15
|
|
|
*/ |
16
|
1 |
|
public function __construct($message = null) |
17
|
|
|
{ |
18
|
1 |
|
$this->username = config('laravel-sms.nigerian_bulk_sms.username'); |
19
|
1 |
|
$this->password = config('laravel-sms.nigerian_bulk_sms.password'); |
20
|
1 |
|
if ($message) { |
21
|
|
|
$this->text($message); |
22
|
|
|
} |
23
|
|
|
$headers = [ |
24
|
1 |
|
'apiKey' => $this->username, |
25
|
1 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
26
|
1 |
|
'Accept' => 'application/json', |
27
|
1 |
|
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 OPR/47.0.2631.39', |
28
|
|
|
]; |
29
|
|
|
|
30
|
1 |
|
$this->client = self::getInstance(); |
31
|
1 |
|
$this->request = new Request('GET', $this->baseUrl.'api', $headers); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param null $text |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
1 |
|
public function send($text = null): bool |
39
|
|
|
{ |
40
|
1 |
|
if ($text) { |
41
|
|
|
$this->setText($text); |
42
|
|
|
} |
43
|
|
|
try { |
44
|
1 |
|
$request = $this->client->send($this->request, [ |
45
|
|
|
'query' => [ |
46
|
1 |
|
'username' => $this->username, |
47
|
1 |
|
'password' => $this->password, |
48
|
1 |
|
'sender' => $this->sender ?? config('laravel-sms.sender'), |
49
|
1 |
|
'mobiles' => implode(',', $this->recipients), |
50
|
1 |
|
'message' => $this->text, |
51
|
|
|
], |
52
|
|
|
]); |
53
|
|
|
|
54
|
1 |
|
$response = json_decode($request->getBody()->getContents(), true); |
55
|
|
|
|
56
|
1 |
|
if (isset($response['status']) && $response['status'] == 'OK') { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->response = $response['error']; |
61
|
|
|
|
62
|
1 |
|
return false; |
63
|
|
|
} catch (ClientException $e) { |
64
|
|
|
logger()->error('HTTP Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage()); |
65
|
|
|
$this->httpError = $e; |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} catch (\Exception $e) { |
69
|
|
|
logger()->error('SMS Exception in '.__CLASS__.': '.__METHOD__.'=>'.$e->getMessage()); |
70
|
|
|
$this->httpError = $e; |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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.