|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tzsk\Sms\Drivers; |
|
3
|
|
|
|
|
4
|
|
|
use Kavenegar\KavenegarApi; |
|
5
|
|
|
use Tzsk\Sms\Abstracts\Driver; |
|
6
|
|
|
|
|
7
|
|
|
class Kavenegar extends Driver |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Kavenegar Settings. |
|
11
|
|
|
* |
|
12
|
|
|
* @var null|object |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $settings = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Kavenegar Client. |
|
18
|
|
|
* |
|
19
|
|
|
* @var null|KavenegarApi |
|
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 KavenegarApi($this->settings->apiKey); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Send text message and return response. |
|
37
|
|
|
* |
|
38
|
|
|
* @return object |
|
39
|
|
|
*/ |
|
40
|
|
|
public function send() |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
|
|
$response = ['status' => true, 'data' =>[]]; |
|
44
|
|
View Code Duplication |
foreach ($this->recipients as $recipient) { |
|
|
|
|
|
|
45
|
|
|
$sms = $this->client->Send( |
|
46
|
|
|
$this->settings->from, |
|
47
|
|
|
$recipient, |
|
48
|
|
|
$this->body |
|
49
|
|
|
); |
|
50
|
|
|
$response['data'][$recipient] = $this->getSmsResponse( |
|
51
|
|
|
json_decode($sms, true) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
} catch (\Exception $e) { |
|
55
|
|
|
$response['status'][$recipient] = false; |
|
56
|
|
|
$response['data'][$recipient] = $e->getMessage(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return (object) $response; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the Kavenegar Response. |
|
64
|
|
|
* |
|
65
|
|
|
* @param $sms |
|
66
|
|
|
* @return object |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getSmsResponse($sms) |
|
69
|
|
|
{ |
|
70
|
|
|
$attributes = [ |
|
71
|
|
|
'messageid', 'message', 'status', 'statustext', |
|
72
|
|
|
'sender', 'receptor', 'date', 'cost', |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
$res = []; |
|
76
|
|
|
foreach ($attributes as $attribute) { |
|
77
|
|
|
$res[$attribute] = $sms->$attribute; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return (object) $res; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.