Completed
Push — master ( 9edfc5...0d667f )
by Evgenij
03:15
created

DelegatingIoHandler::getHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
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\RequestExecutor\EventHandlerInterface;
15
use AsyncSockets\RequestExecutor\ExecutionContext;
16
use AsyncSockets\RequestExecutor\IoHandlerInterface;
17
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
18
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
19
20
/**
21
 * Class DelegatingIoHandler
22
 */
23
class DelegatingIoHandler implements IoHandlerInterface
24
{
25
    /**
26
     * List of nested handlers
27
     *
28
     * @var IoHandlerInterface[]
29
     */
30
    private $handlers;
31
32
    /**
33
     * DelegatingIoHandler constructor.
34
     *
35
     * @param IoHandlerInterface[] $handlers List of nested handlers
36
     */
37 153
    public function __construct(array $handlers)
38
    {
39 153
        $this->handlers = $handlers;
40 153
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 93
    public function supports(OperationInterface $operation)
46
    {
47 93
        return $this->getHandler($operation) !== null;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 96
    public function handle(
54
        OperationInterface $operation,
55
        RequestDescriptor $descriptor,
56
        RequestExecutorInterface $executor,
57
        EventHandlerInterface $eventHandler,
58
        ExecutionContext $executionContext
59
    ) {
60 96
        $handler = $this->getHandler($operation);
61
        return $handler ?
62 96
            $handler->handle($operation, $descriptor, $executor, $eventHandler, $executionContext) :
63 58
            null;
64
    }
65
66
    /**
67
     * Return handler for given operation if there is any
68
     *
69
     * @param OperationInterface $operation Operation to get handler for
70
     *
71
     * @return IoHandlerInterface|null
72
     */
73 97
    private function getHandler(OperationInterface $operation)
74
    {
75 97
        foreach ($this->handlers as $handler) {
76 97
            if ($handler->supports($operation)) {
77 96
                return $handler;
78
            }
79 43
        }
80
81 1
        return null;
82
    }
83
}
84