SslHandshakeIoHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 38
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 1
A handleOperation() 0 20 3
A getHandlerType() 0 4 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\Pipeline;
11
12
use AsyncSockets\Exception\SslHandshakeException;
13
use AsyncSockets\Operation\OperationInterface;
14
use AsyncSockets\Operation\SslHandshakeOperation;
15
use AsyncSockets\RequestExecutor\EventHandlerInterface;
16
use AsyncSockets\RequestExecutor\ExecutionContext;
17
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
18
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
19
20
/**
21
 * Class SslHandshakeIoHandler
22
 */
23
class SslHandshakeIoHandler extends AbstractOobHandler
24
{
25
    /** {@inheritdoc} */
26 1
    public function supports(OperationInterface $operation)
27
    {
28 1
        return $operation instanceof SslHandshakeOperation;
29
    }
30
31
    /** {@inheritdoc} */
32 3
    protected function handleOperation(
33
        OperationInterface $operation,
34
        RequestDescriptor $descriptor,
35
        RequestExecutorInterface $executor,
36
        EventHandlerInterface $eventHandler,
37
        ExecutionContext $executionContext
38
    ) {
39 3
        $socket = $descriptor->getSocket();
40
41
        /** @var SslHandshakeOperation $operation */
42 3
        $resource = $descriptor->getSocket()->getStreamResource();
43 3
        $result   = stream_socket_enable_crypto($resource, true, $operation->getCipher());
44 3
        if ($result === true) {
45 1
            return $operation->getNextOperation();
46 2
        } elseif ($result === false) {
47 1
            throw new SslHandshakeException($socket, 'SSL handshake failed.');
48
        }
49
50 1
        return $operation;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 4
    protected function getHandlerType()
57
    {
58 4
        return RequestDescriptor::RDS_WRITE;
59
    }
60
}
61