|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tzsk\Sms\Drivers; |
|
3
|
|
|
|
|
4
|
|
|
use Melipayamak\MelipayamakApi; |
|
5
|
|
|
use Tzsk\Sms\Abstracts\Driver; |
|
6
|
|
|
|
|
7
|
|
|
class Melipayamak extends Driver |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Melipayamak Settings. |
|
11
|
|
|
* |
|
12
|
|
|
* @var null|object |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $settings = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Melipayamak Client. |
|
18
|
|
|
* |
|
19
|
|
|
* @var null|MelipayamakApi |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $client = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Construct the class with the relevant settings. |
|
25
|
|
|
* |
|
26
|
|
|
* SendSmsInterface constructor. |
|
27
|
|
|
* @param $settings object |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($settings) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->settings = (object) $settings; |
|
32
|
|
|
$this->client = new MelipayamakApi($this->settings->username, $this->settings->password); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Determine if the sms must be a flash message or not. |
|
37
|
|
|
* |
|
38
|
|
|
* @param bool $flash |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
|
|
public function asFlash($flash=true) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->settings->flash = $flash; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Send text message and return response. |
|
50
|
|
|
* |
|
51
|
|
|
* @return object |
|
52
|
|
|
*/ |
|
53
|
|
View Code Duplication |
public function send() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$response = []; |
|
56
|
|
|
foreach ($this->recipients as $recipient) { |
|
57
|
|
|
$sms = $this->client->sms()->send( |
|
58
|
|
|
$recipient, |
|
59
|
|
|
$this->settings->from, |
|
60
|
|
|
$this->body, |
|
61
|
|
|
$this->settings->flash |
|
62
|
|
|
); |
|
63
|
|
|
$response[$recipient]['data'] = $this->getSmsResponse($sms); |
|
64
|
|
|
$response[$recipient]['status'] = true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return (object) $response; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the Melipayamak Response. |
|
72
|
|
|
* |
|
73
|
|
|
* @param $sms |
|
74
|
|
|
* @return object |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getSmsResponse($sms) |
|
77
|
|
|
{ |
|
78
|
|
|
$sms = json_decode($sms, true); |
|
79
|
|
|
$attributes = [ |
|
80
|
|
|
'Value', 'RetStatus', 'StrRetStatus' |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$res = []; |
|
84
|
|
|
foreach ($attributes as $attribute) { |
|
85
|
|
|
$res[$attribute] = $sms[$attribute]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return (object) $res; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
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.