Completed
Branch 0.4-dev (d01bc8)
by Evgenij
02:48
created

SslHandshakeIoHandler::handleOperation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 13
nc 3
nop 3
crap 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\RequestExecutor\EventHandlerInterface;
14
use AsyncSockets\RequestExecutor\IoHandlerInterface;
15
use AsyncSockets\Operation\OperationInterface;
16
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
17
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
18
use AsyncSockets\Operation\SslHandshakeOperation;
19
use AsyncSockets\Socket\SocketInterface;
20
21
/**
22
 * Class SslHandshakeIoHandler
23
 */
24
class SslHandshakeIoHandler extends AbstractOobHandler
25
{
26 1
    /** {@inheritdoc} */
27
    public function supports(OperationInterface $operation)
28 1
    {
29
        return $operation instanceof SslHandshakeOperation;
30
    }
31
32 3
    /** {@inheritdoc} */
33
    protected function handleOperation(
34
        RequestDescriptor $descriptor,
35
        RequestExecutorInterface $executor,
36
        EventHandlerInterface $eventHandler
37
    ) {
38
        $operation = $descriptor->getOperation();
39 3
        $socket    = $descriptor->getSocket();
40 3
41 3
        /** @var SslHandshakeOperation $operation */
42 1
        $resource = $descriptor->getSocket()->getStreamResource();
43 2
        $result   = stream_socket_enable_crypto($resource, true, $operation->getCipher());
44 1
        if ($result === true) {
45
            return $operation->getNextOperation();
46
        } elseif ($result === false) {
47 1
            throw new SslHandshakeException($socket, 'SSL handshake failed.');
48
        }
49
50
        return $operation;
51
    }
52
}
53