1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Messenger\Curl; |
4
|
|
|
|
5
|
|
|
use Cronario\AbstractJob; |
6
|
|
|
use Cronario\AbstractWorker; |
7
|
|
|
use Cronario\Logger; |
8
|
|
|
use Messenger\CurlWrapper; |
9
|
|
|
use Messenger\CurlWrapperCurlException; |
10
|
|
|
use Messenger\CurlWrapperException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Worker |
14
|
|
|
* |
15
|
|
|
* @package Messenger\Curl |
16
|
|
|
*/ |
17
|
|
|
class Worker extends AbstractWorker |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
// region HELPERS ******************************************************** |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $what |
25
|
|
|
* @param $where |
26
|
|
|
* |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
1 |
|
protected function isContain($what, $where) |
30
|
|
|
{ |
31
|
1 |
|
if (empty($what)) { |
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
if (empty($where)) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
if (strpos((string) $where, (string) $what) !== false) { |
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// endregion ************************************************************* |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param AbstractJob $job |
50
|
|
|
* |
51
|
|
|
* @throws ResultException |
52
|
|
|
*/ |
53
|
5 |
|
protected function doJob(AbstractJob $job) |
54
|
|
|
{ |
55
|
|
|
/** @var $job Job */ |
56
|
5 |
|
$resultData = null; |
57
|
|
|
|
58
|
|
|
try { |
59
|
5 |
|
$curl = new CurlWrapper(); |
60
|
5 |
|
$curl->request($job->getUrl(), $job->getMethod(), $job->getRequestParams()); |
|
|
|
|
61
|
3 |
|
$response['content'] = $curl->getResponse(); |
|
|
|
|
62
|
3 |
|
$response['info'] = $curl->getTransferInfo(); |
63
|
|
|
|
64
|
5 |
|
} catch (CurlWrapperCurlException $a) { |
65
|
|
|
$job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]); |
66
|
|
|
throw new ResultException(ResultException::ERROR_CURL); |
67
|
2 |
|
} catch (CurlWrapperException $a) { |
68
|
|
|
$job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]); |
69
|
|
|
throw new ResultException(ResultException::ERROR_CURL); |
70
|
2 |
|
} catch (\Exception $a) { |
71
|
2 |
|
$job->addDebug(['CurlWrapperCurlException' => $a->getMessage()]); |
72
|
2 |
|
throw new ResultException(ResultException::ERROR_CURL); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* saving data |
77
|
|
|
*/ |
78
|
3 |
|
if ($job->getSaveContent()) { |
79
|
1 |
|
$resultData['content'] = $response['content']; |
80
|
1 |
|
} |
81
|
|
|
|
82
|
3 |
|
if ($job->getSaveInfo()) { |
83
|
1 |
|
$resultData['info'] = $response['info']; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* analise response |
88
|
|
|
*/ |
89
|
3 |
|
if ($job->getExpectCode() && $job->getExpectCode() != $response['info']['http_code']) { |
90
|
1 |
|
throw new ResultException(ResultException::RETRY_EXPECTED_HTTP_CODE, $resultData); |
91
|
|
|
|
92
|
2 |
|
} elseif ($job->getExpectContent() && !$this->isContain($job->getExpectContent(), $response['content'])) { |
93
|
1 |
|
throw new ResultException(ResultException::RETRY_EXPECTED_CONTENT, $resultData); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* success result |
98
|
|
|
*/ |
99
|
1 |
|
throw new ResultException(ResultException::R_SUCCESS, $resultData); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.