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

DelegatingIoHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 4 1
A handle() 0 12 2
A getHandler() 0 10 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
    public function __construct(array $handlers)
38
    {
39
        $this->handlers = $handlers;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function supports(OperationInterface $operation)
46
    {
47
        return $this->getHandler($operation) !== null;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function handle(
54
        OperationInterface $operation,
55
        RequestDescriptor $descriptor,
56
        RequestExecutorInterface $executor,
57
        EventHandlerInterface $eventHandler,
58
        ExecutionContext $executionContext
59
    ) {
60
        $handler = $this->getHandler($operation);
61
        return $handler ?
62
            $handler->handle($operation, $descriptor, $executor, $eventHandler, $executionContext) :
63
            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
    private function getHandler(OperationInterface $operation)
74
    {
75
        foreach ($this->handlers as $handler) {
76
            if ($handler->supports($operation)) {
77
                return $handler;
78
            }
79
        }
80
81
        return null;
82
    }
83
}
84