Completed
Branch 0.4-dev (3e8fde)
by Evgenij
17:21
created

ReadWriteIoHandler::handle()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 16
nop 5
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
11
namespace AsyncSockets\RequestExecutor\Pipeline;
12
13
use AsyncSockets\Operation\OperationInterface;
14
use AsyncSockets\Operation\ReadWriteOperation;
15
use AsyncSockets\RequestExecutor\EventHandlerInterface;
16
use AsyncSockets\RequestExecutor\ExecutionContext;
17
use AsyncSockets\RequestExecutor\IoHandlerInterface;
18
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
19
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
20
21
/**
22
 * Class ReadWriteIoHandler
23
 */
24
class ReadWriteIoHandler implements IoHandlerInterface
25
{
26
    /**
27
     * Io handler
28
     *
29
     * @var IoHandlerInterface
30
     */
31
    private $handler;
32
33
    /**
34
     * Initialize I/O handler
35
     *
36
     * @param IoHandlerInterface $handler New value for Handler
37
     *
38
     * @return void
39
     */
40
    public function setHandler(IoHandlerInterface $handler)
41
    {
42
        $this->handler = $handler;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function supports(OperationInterface $operation)
49
    {
50
        return $operation instanceof ReadWriteOperation;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function handle(
57
        OperationInterface $operation,
58
        RequestDescriptor $descriptor,
59
        RequestExecutorInterface $executor,
60
        EventHandlerInterface $eventHandler,
61
        ExecutionContext $executionContext
62
    ) {
63
        /** @var ReadWriteOperation $operation */
64
        $sequence = $operation->isReadFirst() ?
65
            [$operation->getReadOperation(), $operation->getWriteOperation()] :
66
            [$operation->getWriteOperation(), $operation->getReadOperation()];
67
        $sequence = array_filter($sequence);
68
69
        foreach ($sequence as $op) {
70
            $this->handleOperation(
71
                $op,
72
                $operation,
73
                $descriptor,
74
                $executor,
75
                $eventHandler,
76
                $executionContext
77
            );
78
        }
79
80
        return $operation->getReadOperation() !== null || $operation->getWriteOperation() !== null ?
81
            $operation :
82
            null;
83
    }
84
85
    /**
86
     * Handle read operation
87
     *
88
     * @param OperationInterface       $nestedOperation  Nested I/O operation
89
     * @param ReadWriteOperation       $operation        Operation to process
90
     * @param RequestDescriptor        $descriptor       Request descriptor
91
     * @param RequestExecutorInterface $executor         Executor, processing operation
92
     * @param EventHandlerInterface    $eventHandler     Event handler for this operation
93
     * @param ExecutionContext         $executionContext Execution context
94
     *
95
     * @return void
96
     */
97
    private function handleOperation(
98
        OperationInterface $nestedOperation = null,
99
        ReadWriteOperation $operation,
100
        RequestDescriptor $descriptor,
101
        RequestExecutorInterface $executor,
102
        EventHandlerInterface $eventHandler,
103
        ExecutionContext $executionContext
104
    ) {
105
        if (!$nestedOperation) {
106
            return;
107
        }
108
109
        $next = $this->handler->handle($nestedOperation, $descriptor, $executor, $eventHandler, $executionContext);
110
        if ($next !== $nestedOperation) {
111
            $operation->markCompleted($nestedOperation);
112
        }
113
114
        if (!$next) {
115
            return;
116
        }
117
118
        $operation->scheduleOperation($next);
119
    }
120
}
121