Completed
Branch 0.4-dev (999b58)
by Evgenij
18:41
created

ReadIoHandler::handleOperation()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
ccs 28
cts 28
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
    public function supports(OperationInterface $operation)
30 73
    {
31
        return $operation instanceof ReadOperation;
32 73
    }
33
34
    /** {@inheritdoc} */
35
    protected function handleOperation(
36 44
        RequestDescriptor $descriptor,
37
        RequestExecutorInterface $executor,
38
        EventHandlerInterface $eventHandler
39
    ) {
40
        $operation = $descriptor->getOperation();
41
        $socket    = $descriptor->getSocket();
42 44
43 44
        $meta    = $executor->socketBag()->getSocketMetaData($socket);
44
        $context = $meta[RequestExecutorInterface::META_USER_CONTEXT];
45
46
        try {
47 44
            /** @var ReadOperation $operation */
48 40
            $response = $socket->read($operation->getFramePicker());
49 40
            switch (true) {
50 1
                case $response instanceof PartialFrame:
51 39
                    return $operation;
52 5
                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
                    );
60 5
61 1
                    $eventHandler->invokeEvent($event);
62 34
                    return new ReadOperation();
63 34
                default:
64 34
                    $event = new ReadEvent(
65 34
                        $executor,
66 34
                        $socket,
67
                        $context,
68 34
                        $response,
69
                        false
70 34
                    );
71 23
72 34
                    $eventHandler->invokeEvent($event);
73 19
                    return $event->getNextOperation();
74 1
            }
75
        } catch (AcceptException $e) {
76
            return new ReadOperation();
77
        }
78
    }
79
}
80