|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tzsk\Sms\Drivers; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
|
5
|
|
|
use Tzsk\Sms\Contract\MasterDriver; |
|
6
|
|
|
|
|
7
|
|
View Code Duplication |
class Linkmobility extends MasterDriver |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Linkmobility Settings. |
|
11
|
|
|
* |
|
12
|
|
|
* @var object|null |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $settings = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Http Client. |
|
18
|
|
|
* |
|
19
|
|
|
* @var Client|null |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $client = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Construct the class with the relevant settings. |
|
25
|
|
|
* |
|
26
|
|
|
* SendSmsInterface constructor. |
|
27
|
|
|
* @param array $settings |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($settings) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->settings = (object) $settings; |
|
32
|
|
|
$this->client = new Client(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Send text message and return response. |
|
37
|
|
|
* |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function send() |
|
41
|
|
|
{ |
|
42
|
|
|
$numbers = implode(",", $this->recipients); |
|
43
|
|
|
|
|
44
|
|
|
$response = $this->client->request("POST", $this->settings->url, [ |
|
45
|
|
|
"form_params" => [ |
|
46
|
|
|
"USER" => $this->settings->username, |
|
47
|
|
|
"PW" => $this->settings->password, |
|
48
|
|
|
"RCV" => $numbers, |
|
49
|
|
|
"SND" => urlencode($this->settings->sender), |
|
50
|
|
|
"TXT" => $this->body, |
|
51
|
|
|
], |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
$data = $this->getResponseData($response); |
|
55
|
|
|
|
|
56
|
|
|
return (object) array_merge($data, ["status" => true]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param mixed $response |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed|object |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getResponseData($response) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($response->getStatusCode() != 200) { |
|
67
|
|
|
return (object) ["status" => false, "message" => "Request Error. " . $response->getReasonPhrase()]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$data = json_decode((string) $response->getBody(), true); |
|
71
|
|
|
|
|
72
|
|
|
if ($data["status"] != "success") { |
|
73
|
|
|
return (object) ["status" => false, "message" => "Something went wrong.", "data" => $data]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $data; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
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.