1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HoomanMirghasemi\Sms\Drivers; |
4
|
|
|
|
5
|
|
|
use HoomanMirghasemi\Sms\Abstracts\Driver; |
6
|
|
|
use Kavenegar\KavenegarApi; |
7
|
|
|
|
8
|
|
|
class Kavenegar extends Driver |
9
|
|
|
{ |
10
|
|
|
protected string $from = ''; |
11
|
|
|
|
12
|
|
|
public function __construct(protected array $settings, private KavenegarApi $kavenegarApi) |
13
|
|
|
{ |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function getBalance(): string |
17
|
|
|
{ |
18
|
|
|
return ''; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function send(): bool |
22
|
|
|
{ |
23
|
|
|
try { |
24
|
|
|
if ($this->message->usesTemplate()) { |
|
|
|
|
25
|
|
|
$template = $this->message->getTemplate(); |
|
|
|
|
26
|
|
|
$identifier = $template['identifier']; |
27
|
|
|
$params = $template['params']; |
28
|
|
|
$token1 = str_replace(' ', '-', $params['token1'] ?? ''); |
29
|
|
|
$token2 = str_replace(' ', '-', $params['token2'] ?? ''); |
30
|
|
|
$token3 = str_replace(' ', '-', $params['token3'] ?? ''); |
31
|
|
|
|
32
|
|
|
$token10 = $this->replaceExtraSpace($params['token10'] ?? '', 4); |
33
|
|
|
$token20 = $this->replaceExtraSpace($params['token20'] ?? '', 8); |
34
|
|
|
|
35
|
|
|
$result = $this->kavenegarApi->VerifyLookup( |
36
|
|
|
$this->recipient, |
37
|
|
|
$token1, |
38
|
|
|
$token2, |
39
|
|
|
$token3, |
40
|
|
|
$identifier, |
41
|
|
|
null, |
42
|
|
|
$token10, |
43
|
|
|
$token20 |
44
|
|
|
); |
45
|
|
|
} else { |
46
|
|
|
$result = $this->kavenegarApi->Send(null, $this->recipient, $this->message->toString()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->success = true; |
50
|
|
|
$this->from = $result[0]->sender; |
51
|
|
|
$this->message = $result[0]->message; |
52
|
|
|
$this->webserviceResponse = print_r($result, true); |
|
|
|
|
53
|
|
|
} catch (\Exception $exception) { |
54
|
|
|
$this->webserviceResponse = $exception->getMessage(); |
55
|
|
|
$this->success = false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return parent::send(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function replaceExtraSpace($string, $maxSpace) |
62
|
|
|
{ |
63
|
|
|
$spaceCount = substr_count($string, ' '); |
64
|
|
|
if ($spaceCount > $maxSpace) { |
65
|
|
|
$string = strrev(preg_replace('/ /', '-', strrev($string), $spaceCount - $maxSpace)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $string; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|