1 | <?php |
||
25 | class IoStage extends AbstractTimeAwareStage |
||
26 | { |
||
27 | /** |
||
28 | * Handlers for processing I/O |
||
29 | * |
||
30 | * @var IoHandlerInterface |
||
31 | */ |
||
32 | private $ioHandler; |
||
33 | |||
34 | /** |
||
35 | * IoStage constructor. |
||
36 | * |
||
37 | * @param RequestExecutorInterface $executor Request executor |
||
38 | * @param EventCaller $eventCaller Event caller |
||
39 | * @param ExecutionContext $executionContext Execution context |
||
40 | * @param IoHandlerInterface $ioHandler Operation handler |
||
41 | */ |
||
42 | 148 | public function __construct( |
|
51 | |||
52 | /** {@inheritdoc} */ |
||
53 | 114 | public function processStage(array $requestDescriptors) |
|
74 | |||
75 | /** |
||
76 | * Resolves I/O operation type and process it |
||
77 | * |
||
78 | * @param RequestDescriptor $requestDescriptor Operation object |
||
79 | * |
||
80 | * @return IoHandlerInterface Flag, whether operation is complete |
||
81 | * @throws \LogicException |
||
82 | */ |
||
83 | 95 | private function requireIoHandler(RequestDescriptor $requestDescriptor) |
|
84 | { |
||
85 | 95 | $operation = $requestDescriptor->getOperation(); |
|
86 | 95 | if (!$this->ioHandler->supports($operation)) { |
|
87 | 2 | throw new \LogicException('There is no handler able to process ' . get_class($operation) . ' operation.'); |
|
88 | } |
||
89 | |||
90 | 93 | return $this->ioHandler; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Process I/O |
||
95 | * |
||
96 | * @param RequestDescriptor $requestDescriptor |
||
97 | * @param IoHandlerInterface $ioHandler |
||
98 | * |
||
99 | * @return OperationInterface |
||
100 | */ |
||
101 | 93 | private function handleIoOperation(RequestDescriptor $requestDescriptor, IoHandlerInterface $ioHandler) |
|
102 | { |
||
103 | try { |
||
104 | 93 | $this->eventCaller->setCurrentOperation($requestDescriptor); |
|
105 | 93 | $result = $ioHandler->handle( |
|
106 | 93 | $requestDescriptor->getOperation(), |
|
107 | 93 | $requestDescriptor, |
|
108 | 93 | $this->executor, |
|
109 | 93 | $this->eventCaller, |
|
110 | 93 | $this->executionContext |
|
111 | 93 | ); |
|
112 | 55 | $this->eventCaller->clearCurrentOperation(); |
|
113 | |||
114 | 55 | return $result ?: NullOperation::getInstance(); |
|
115 | 38 | } catch (NetworkSocketException $e) { |
|
116 | 6 | $this->callExceptionSubscribers($requestDescriptor, $e); |
|
117 | 6 | return NullOperation::getInstance(); |
|
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Fill next operation in given object and return flag indicating whether operation is required |
||
123 | * |
||
124 | * @param RequestDescriptor $requestDescriptor Request descriptor object |
||
125 | * @param OperationInterface $nextOperation Next operation object |
||
126 | * |
||
127 | * @return bool True if given operation is complete |
||
128 | */ |
||
129 | 61 | private function resolveNextOperation( |
|
151 | |||
152 | /** |
||
153 | * Set connection finish time and fire socket if it was not connected |
||
154 | * |
||
155 | * @param RequestDescriptor $requestDescriptor |
||
156 | * |
||
157 | * @return bool True, if there was no error, false if operation should be stopped |
||
158 | */ |
||
159 | 108 | private function setConnectionFinishTime(RequestDescriptor $requestDescriptor) |
|
177 | } |
||
178 |