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

ReadIoHandler::handleOperation()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
ccs 31
cts 31
cp 1
rs 8.5806
cc 4
eloc 33
nc 10
nop 3
crap 4
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\AcceptEvent;
13
use AsyncSockets\Event\ReadEvent;
14
use AsyncSockets\Exception\AcceptException;
15
use AsyncSockets\Frame\AcceptedFrame;
16
use AsyncSockets\Frame\PartialFrame;
17
use AsyncSockets\Operation\OperationInterface;
18
use AsyncSockets\Operation\ReadOperation;
19
use AsyncSockets\RequestExecutor\EventHandlerInterface;
20
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
21
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
22
23
/**
24
 * Class ReadIoHandler
25
 */
26
class ReadIoHandler extends AbstractOobHandler
27
{
28
    /** {@inheritdoc} */
29 73
    public function supports(OperationInterface $operation)
30
    {
31 73
        return $operation instanceof ReadOperation;
32
    }
33
34
    /** {@inheritdoc} */
35 44
    protected function handleOperation(
36
        RequestDescriptor $descriptor,
37
        RequestExecutorInterface $executor,
38
        EventHandlerInterface $eventHandler
39
    ) {
40 44
        $operation = $descriptor->getOperation();
41 44
        $socket    = $descriptor->getSocket();
42
43 44
        $meta    = $executor->socketBag()->getSocketMetaData($socket);
44 44
        $context = $meta[RequestExecutorInterface::META_USER_CONTEXT];
45
46
        try {
47
            /** @var ReadOperation $operation */
48 44
            $response = $socket->read($operation->getFramePicker());
49 40
            switch (true) {
50 40
                case $response instanceof PartialFrame:
51 1
                    return $operation;
52 39
                case $response instanceof AcceptedFrame:
53 5
                    $event = new AcceptEvent(
54 5
                        $executor,
55 5
                        $socket,
56 5
                        $context,
57 5
                        $response->getClientSocket(),
58 5
                        $response->getRemoteAddress()
59 5
                    );
60
61 5
                    $eventHandler->invokeEvent($event);
62 1
                    return new ReadOperation();
63 34
                default:
64 34
                    $event = new ReadEvent(
65 34
                        $executor,
66 34
                        $socket,
67 34
                        $context,
68 34
                        $response,
69
                        false
70 34
                    );
71
72 34
                    $eventHandler->invokeEvent($event);
73 23
                    return $event->getNextOperation();
74 34
            }
75 19
        } catch (AcceptException $e) {
76 1
            return new ReadOperation();
77
        }
78
    }
79
}
80