Completed
Branch 0.4-dev (f2f961)
by Evgenij
02:48
created

AbstractOobHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 85
ccs 29
cts 29
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 39 4
A getInvokeState() 0 6 2
handleOperation() 0 5 ?
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2016, 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\Event\ReadEvent;
13
use AsyncSockets\Frame\RawFramePicker;
14
use AsyncSockets\Operation\OperationInterface;
15
use AsyncSockets\RequestExecutor\EventHandlerInterface;
16
use AsyncSockets\RequestExecutor\IoHandlerInterface;
17
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
18
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
19
20
/**
21
 * Class AbstractOobHandler
22
 */
23
abstract class AbstractOobHandler implements IoHandlerInterface
24
{
25
    /**
26
     * Operation to descriptor state map
27
     *
28
     * @var array
29
     */
30
    private static $stateMap = [
31
        OperationInterface::OPERATION_READ  => RequestDescriptor::RDS_READ,
32
        OperationInterface::OPERATION_WRITE => RequestDescriptor::RDS_WRITE,
33
    ];
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 100
    final public function handle(
39
        RequestDescriptor $descriptor,
40
        RequestExecutorInterface $executor,
41
        EventHandlerInterface $eventHandler
42
    ) {
43 100
        $result = null;
44
45 100
        if ($descriptor->hasState(RequestDescriptor::RDS_OOB)) {
46 10
            $descriptor->clearState(RequestDescriptor::RDS_OOB);
47
48 10
            $picker = new RawFramePicker();
49 10
            $socket = $descriptor->getSocket();
50 10
            $meta   = $descriptor->getMetadata();
51 10
            $frame  = $socket->read($picker, true);
52 10
            $event  = new ReadEvent(
53 10
                $executor,
54 10
                $socket,
55 10
                $meta[RequestExecutorInterface::META_USER_CONTEXT],
56 10
                $frame,
57
                true
58 10
            );
59 10
            $event->nextIs($descriptor->getOperation());
60 10
            $eventHandler->invokeEvent($event);
61
62 10
            $result = $event->getNextOperation();
63 10
            if ($event->getNextOperation() !== $descriptor->getOperation()) {
64 5
                return $result;
65
            }
66 5
        }
67
68 95
        $state = $this->getInvokeState($descriptor->getOperation());
69 95
        if ($descriptor->hasState($state)) {
70 90
            $descriptor->clearState($state);
71
72 90
            $result = $this->handleOperation($descriptor, $executor, $eventHandler);
73 57
        }
74
75 62
        return $result;
76
    }
77
78
    /**
79
     * Return state which should invoke this operation
80
     *
81
     * @param OperationInterface $operation Operation object
82
     *
83
     * @return int RequestDescriptor::RDS_* constant
84
     */
85 95
    private function getInvokeState(OperationInterface $operation)
86
    {
87 95
        $type = $operation->getType();
88
89 95
        return isset(self::$stateMap[$type]) ? self::$stateMap[$type] : 0;
90
    }
91
92
    /**
93
     * Process given operation
94
     *
95
     * @param RequestDescriptor        $descriptor Request descriptor
96
     * @param RequestExecutorInterface $executor Executor, processing operation
97
     * @param EventHandlerInterface    $eventHandler Event handler for this operation
98
     *
99
     * @return OperationInterface|null Next operation to pass in socket. Return null,
100
     *      if next operation is not required. Return $operation parameter, if operation is not completed yet
101
     */
102
    abstract protected function handleOperation(
103
        RequestDescriptor $descriptor,
104
        RequestExecutorInterface $executor,
105
        EventHandlerInterface $eventHandler
106
    );
107
}
108