1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Messenger\Sms\Infobip; |
4
|
|
|
|
5
|
|
|
use Cronario\AbstractJob; |
6
|
|
|
use Cronario\AbstractWorker; |
7
|
|
|
use Messenger\Sms\Job; |
8
|
|
|
use Messenger\Sms\ResultException; |
9
|
|
|
use Messenger\Sms\Worker as SmsWorker; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Worker |
13
|
|
|
* |
14
|
|
|
* @package Messenger\Sms\Infobip |
15
|
|
|
*/ |
16
|
|
|
class Worker extends AbstractWorker |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
protected static $config |
20
|
|
|
= [ |
21
|
|
|
AbstractWorker::CONFIG_P_MANAGERS_LIMIT => 3, |
22
|
|
|
AbstractWorker::CONFIG_P_MANAGER_POOL_SIZE => 5, |
23
|
|
|
SmsWorker::GATEWAY_DISPATCH_CLASS => '\\Messenger\\Sms\\Worker', |
24
|
|
|
self::CONFIG_P_CLIENT => [ |
25
|
|
|
'login' => 'xxx', |
26
|
|
|
'password' => 'xxx', |
27
|
|
|
], |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
// region CLIENT ******************************************************** |
31
|
|
|
|
32
|
|
|
const CONFIG_P_CLIENT = 'client'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \infobip\SmsClient |
36
|
|
|
*/ |
37
|
|
|
protected $transport; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return \infobip\SmsClient |
41
|
|
|
*/ |
42
|
1 |
View Code Duplication |
protected function getTransport() |
|
|
|
|
43
|
|
|
{ |
44
|
1 |
|
if (null === $this->transport) { |
45
|
1 |
|
$clientConfig = self::getConfig(self::CONFIG_P_CLIENT); |
46
|
1 |
|
$this->transport = new \infobip\SmsClient($clientConfig['login'], $clientConfig['password']); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
return $this->transport; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AbstractJob|Job $job |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
2 |
|
protected function send(AbstractJob $job) |
58
|
|
|
{ |
59
|
|
|
$result = [ |
60
|
2 |
|
'errors' => [], |
61
|
2 |
|
'vendor_id' => null, |
62
|
2 |
|
]; |
63
|
|
|
|
64
|
|
|
try { |
65
|
2 |
|
$transport = $this->getTransport(); |
66
|
|
|
|
67
|
2 |
|
$smsMessage = new \infobip\models\SMSRequest(); |
68
|
|
|
|
69
|
2 |
|
$smsMessage->senderAddress = $job->getSender(); |
|
|
|
|
70
|
2 |
|
$smsMessage->address = $job->getRecipient(); |
|
|
|
|
71
|
2 |
|
$smsMessage->message = $job->getText(); |
|
|
|
|
72
|
|
|
|
73
|
2 |
|
$response = $transport->sendSMS($smsMessage); |
74
|
|
|
|
75
|
1 |
|
$result['vendor_id'] = $response->clientCorrelator; |
76
|
|
|
if ($response->exception != null) { |
77
|
|
|
$result['errors'][] = $response->exception; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
} catch (\Exception $ex) { |
81
|
2 |
|
$result['errors'][] = $ex->getMessage(); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
// endregion ************************************************************* |
87
|
|
|
|
88
|
|
|
// region VALIDATE ******************************************************** |
89
|
|
|
|
90
|
2 |
View Code Duplication |
protected function validateJobParams(Job $job) |
|
|
|
|
91
|
|
|
{ |
92
|
2 |
|
if (empty($job->getSender())) { |
93
|
|
|
throw new ResultException(ResultException::ERROR_PARAM_SENDER); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
if (empty($job->getRecipient())) { |
97
|
|
|
throw new ResultException(ResultException::ERROR_PARAM_RECIPIENT); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
if (empty($job->getText())) { |
101
|
|
|
throw new ResultException(ResultException::ERROR_PARAM_TEXT); |
102
|
|
|
} |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
// endregion ************************************************************* |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param AbstractJob|Job $job |
109
|
|
|
* |
110
|
|
|
* @throws ResultException |
111
|
|
|
*/ |
112
|
2 |
|
protected function doJob(AbstractJob $job) |
113
|
|
|
{ |
114
|
2 |
|
$this->validateJobParams($job); |
|
|
|
|
115
|
|
|
|
116
|
2 |
|
$resultData = SmsWorker::buildResultDataDefault(__NAMESPACE__); |
117
|
|
|
|
118
|
|
|
try { |
119
|
2 |
|
$response = $this->send($job); |
120
|
|
|
|
121
|
2 |
|
$resultData[SmsWorker::P_RESULT_DATA_SUCCESS] = count($response['errors']) == 0; |
122
|
2 |
|
$resultData[SmsWorker::P_RESULT_DATA_VENDOR_ID] = $response['vendor_id']; |
123
|
2 |
|
$resultData[SmsWorker::P_RESULT_DATA_ERRORS] = $response['errors']; |
124
|
|
|
|
125
|
2 |
|
$job->addDebug(['vendor_response' => $response]); |
126
|
|
|
|
127
|
2 |
|
} catch (\Exception $ex) { |
128
|
|
|
$job->addDebug(['exception' => $ex->getMessage()]); |
129
|
|
|
throw new ResultException(ResultException::ERROR_TRANSPORT); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
2 |
View Code Duplication |
if (false === $resultData[SmsWorker::P_RESULT_DATA_SUCCESS]) { |
|
|
|
|
134
|
|
|
|
135
|
2 |
|
if (!$job->isSync()) { |
136
|
|
|
// redirect result for new root gateway class |
137
|
|
|
$gatewayClass = static::getConfig(SmsWorker::GATEWAY_DISPATCH_CLASS); |
138
|
|
|
$job->addDebug(['set_gateway' => $gatewayClass]); |
139
|
|
|
$job->setWorkerClass($gatewayClass)->save(); |
140
|
|
|
throw new ResultException(ResultException::RETRY_GATEWAY_DISPATCH_CLASS); |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
throw new ResultException(ResultException::R_FAILURE, $resultData); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
throw new ResultException(ResultException::R_SUCCESS, $resultData); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
|
|
|
154
|
|
|
|
155
|
|
|
|
156
|
|
|
|
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.