|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Messenger\Sms; |
|
4
|
|
|
|
|
5
|
|
|
use Cronario\AbstractJob; |
|
6
|
|
|
use Cronario\AbstractWorker; |
|
7
|
|
|
use Cronario\Exception\WorkerException; |
|
8
|
|
|
|
|
9
|
|
|
class Worker extends AbstractWorker |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
//region Config *********************************************************** |
|
13
|
|
|
|
|
14
|
|
|
protected static $config |
|
15
|
|
|
= [ |
|
16
|
|
|
AbstractWorker::CONFIG_P_MANAGERS_LIMIT => 3, |
|
17
|
|
|
AbstractWorker::CONFIG_P_MANAGER_POOL_SIZE => 5, |
|
18
|
|
|
AbstractWorker::CONFIG_P_MANAGER_IDLE_DIE_DELAY => 5, |
|
19
|
|
|
self::GATEWAY_CLASS_SET => [ |
|
20
|
|
|
'\Messenger\Sms\Alpha\Worker' => [ |
|
21
|
|
|
self::GATEWAY_CLASS_P_REGEX_RECIPIENT => '/^(38)/' |
|
22
|
|
|
], |
|
23
|
|
|
'\Messenger\Sms\Test\Worker' => [ |
|
24
|
|
|
self::GATEWAY_CLASS_P_REGEX_RECIPIENT => '/^(38)/' |
|
25
|
|
|
], |
|
26
|
|
|
], |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
//endregion **************************************************************** |
|
30
|
|
|
|
|
31
|
|
|
//region GATEWAY *********************************************************** |
|
32
|
|
|
|
|
33
|
|
|
const GATEWAY_DISPATCH_CLASS = 'gatewayDispatchClass'; |
|
34
|
|
|
const GATEWAY_CLASS_SET = 'gatewayClassSet'; |
|
35
|
|
|
const GATEWAY_CLASS_P_REGEX_RECIPIENT = 'regexRecipient'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Job $job |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
* @throws WorkerException |
|
42
|
|
|
*/ |
|
43
|
3 |
|
protected function analiseGateway(Job $job) |
|
44
|
|
|
{ |
|
45
|
3 |
|
$gatewayClassSet = self::getConfig(self::GATEWAY_CLASS_SET); |
|
46
|
|
|
|
|
47
|
3 |
|
if (count($gatewayClassSet) === 0) { |
|
48
|
|
|
throw new WorkerException('Empty gateway class set!'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
$gatewayClassScore = []; |
|
52
|
3 |
|
foreach ($gatewayClassSet as $gatewayClass => $gatewayOptions) { |
|
53
|
3 |
|
$gatewayClassScore[$gatewayClass] = 0; |
|
54
|
|
|
|
|
55
|
3 |
|
if (preg_match($gatewayOptions[self::GATEWAY_CLASS_P_REGEX_RECIPIENT], |
|
56
|
3 |
|
$job->getRecipient())) { |
|
57
|
3 |
|
$gatewayClassScore[$gatewayClass]++; |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
// ...... |
|
61
|
3 |
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
$maxClass = array_keys($gatewayClassScore, max($gatewayClassScore))[0]; |
|
64
|
|
|
|
|
65
|
3 |
|
if ($gatewayClassScore[$maxClass] === 0) { |
|
|
|
|
|
|
66
|
|
|
// Transport is bad : $maxClass = 0 , and we have no ideas what return :( |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
return $maxClass; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
//endregion *************************************************************** |
|
73
|
|
|
|
|
74
|
|
|
// region Default Result Data************************************************************** |
|
75
|
|
|
|
|
76
|
|
|
const P_RESULT_DATA_VENDOR_NAME = 'vendor'; |
|
77
|
|
|
const P_RESULT_DATA_VENDOR_ID = 'vendor_id'; |
|
78
|
|
|
const P_RESULT_DATA_SUCCESS = 'success'; |
|
79
|
|
|
const P_RESULT_DATA_ERRORS = 'errors'; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param null $vendor_name |
|
83
|
|
|
* @param null $vendor_id |
|
84
|
|
|
* @param null $success |
|
85
|
|
|
* @param null $errors |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
5 |
|
public static function buildResultDataDefault( |
|
90
|
|
|
$vendor_name = null, |
|
91
|
|
|
$vendor_id = null, |
|
92
|
|
|
$success = null, |
|
93
|
|
|
$errors = null |
|
94
|
|
|
) { |
|
95
|
|
|
return [ |
|
96
|
5 |
|
self::P_RESULT_DATA_VENDOR_NAME => $vendor_name, |
|
97
|
5 |
|
self::P_RESULT_DATA_VENDOR_ID => $vendor_id, |
|
98
|
5 |
|
self::P_RESULT_DATA_SUCCESS => $success, |
|
99
|
5 |
|
self::P_RESULT_DATA_ERRORS => $errors, |
|
100
|
5 |
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
//endregion *************************************************************** |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* if job is sync then we get worker and try execute job |
|
107
|
|
|
* else if job is Async then redirect to other queue |
|
108
|
|
|
* |
|
109
|
|
|
* REMEMBER: Queue name is equal to Worker class name (through all "Cronario") |
|
110
|
|
|
* |
|
111
|
|
|
* @param AbstractJob|Job $job |
|
112
|
|
|
* |
|
113
|
|
|
* @throws ResultException |
|
114
|
|
|
* @throws WorkerException |
|
115
|
|
|
*/ |
|
116
|
3 |
|
protected function doJob(AbstractJob $job) |
|
117
|
|
|
{ |
|
118
|
3 |
|
$gatewayClass = $this->analiseGateway($job); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
3 |
|
if ($job->isSync()) { |
|
121
|
1 |
|
$worker = AbstractWorker::factory($gatewayClass); |
|
122
|
|
|
|
|
123
|
1 |
|
return $worker($job); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
if (!$job->hasAttempt()) { |
|
127
|
1 |
|
throw new ResultException(ResultException::FAILURE_MAX_ATTEMPTS); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// redirect result for new gateway class |
|
131
|
1 |
|
$job->addDebug(['set_gateway' => $gatewayClass]); |
|
132
|
1 |
|
$job->setWorkerClass($gatewayClass)->save(); |
|
133
|
|
|
|
|
134
|
1 |
|
throw new ResultException(ResultException::REDIRECT_GATEWAY_CLASS); |
|
135
|
|
|
} |
|
136
|
|
|
} |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.