|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Async sockets |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace AsyncSockets\RequestExecutor\Pipeline; |
|
11
|
|
|
|
|
12
|
|
|
use AsyncSockets\Operation\DelayedOperation; |
|
13
|
|
|
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DelayStage |
|
17
|
|
|
*/ |
|
18
|
|
|
class DelayStage extends AbstractStage |
|
19
|
|
|
{ |
|
20
|
|
|
/** {@inheritdoc} */ |
|
21
|
109 |
|
public function processStage(array $requestDescriptors) |
|
22
|
|
|
{ |
|
23
|
109 |
|
$result = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var RequestDescriptor[] $requestDescriptors */ |
|
26
|
109 |
|
foreach ($requestDescriptors as $requestDescriptor) { |
|
27
|
109 |
|
$operation = $requestDescriptor->getOperation(); |
|
28
|
109 |
|
if ($operation instanceof DelayedOperation) { |
|
29
|
2 |
|
if ($this->checkDelayIsFinished($requestDescriptor)) { |
|
30
|
1 |
|
$requestDescriptor->setOperation($operation->getOriginalOperation()); |
|
31
|
1 |
|
$result[] = $requestDescriptor; |
|
32
|
1 |
|
} |
|
33
|
2 |
|
} else { |
|
34
|
107 |
|
$result[] = $requestDescriptor; |
|
35
|
|
|
} |
|
36
|
109 |
|
} |
|
37
|
|
|
|
|
38
|
109 |
|
return $result; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check whether socket waiting is finished |
|
43
|
|
|
* |
|
44
|
|
|
* @param RequestDescriptor $descriptor Request descriptor to test |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool True if delay is complete, false otherwise |
|
47
|
|
|
*/ |
|
48
|
2 |
|
private function checkDelayIsFinished(RequestDescriptor $descriptor) |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var DelayedOperation $socketOperation */ |
|
51
|
2 |
|
$socketOperation = $descriptor->getOperation(); |
|
52
|
2 |
|
$arguments = $socketOperation->getArguments(); |
|
53
|
2 |
|
array_unshift($arguments, $descriptor->getSocket(), $this->executor); |
|
54
|
|
|
|
|
55
|
2 |
|
return !call_user_func_array( |
|
56
|
2 |
|
$socketOperation->getCallable(), |
|
57
|
|
|
$arguments |
|
58
|
2 |
|
); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|