Completed
Push — master ( 1d1574...e2543c )
by Evgenij
03:26
created

SslHandshakeIoHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 4
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10

2 Methods

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