RemoveFinishedSocketsEventHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
namespace AsyncSockets\RequestExecutor;
11
12
use AsyncSockets\Event\Event;
13
use AsyncSockets\Event\EventType;
14
use AsyncSockets\Socket\SocketInterface;
15
16
/**
17
 * Class RemoveFinishedSocketsEventHandler
18
 */
19
class RemoveFinishedSocketsEventHandler implements EventHandlerInterface
20
{
21
    /**
22
     * Target handler
23
     *
24
     * @var EventHandlerInterface
25
     */
26
    private $handler;
27
28
    /**
29
     * RemoveFinishedSocketsEventHandler constructor.
30
     *
31
     * @param EventHandlerInterface $handler Original event handler
32
     */
33 5
    public function __construct(EventHandlerInterface $handler = null)
34
    {
35 5
        $this->handler = $handler;
36 5
    }
37
38
    /** {@inheritdoc} */
39 5
    public function invokeEvent(
40
        Event $event,
41
        RequestExecutorInterface $executor,
42
        SocketInterface $socket,
43
        ExecutionContext $context
44
    ) {
45 5
        if ($this->handler) {
46 4
            $this->handler->invokeEvent($event, $executor, $socket, $context);
47 4
        }
48
49 5
        if ($event->getType() === EventType::FINALIZE) {
50 4
            $bag = $event->getExecutor()->socketBag();
51 4
            if ($bag->hasSocket($socket)) {
52 1
                $bag->removeSocket($socket);
53 1
            }
54 4
        }
55 5
    }
56
}
57