Completed
Branch 0.4-dev (999b58)
by Evgenij
18:41
created

SslHandshakeIoHandler::handleOperation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 8
cts 8
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\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
    public function supports(OperationInterface $operation)
26 1
    {
27
        return $operation instanceof SslHandshakeOperation;
28 1
    }
29
30
    /** {@inheritdoc} */
31
    protected function handleOperation(
32 3
        RequestDescriptor $descriptor,
33
        RequestExecutorInterface $executor,
34
        EventHandlerInterface $eventHandler
35
    ) {
36
        $operation = $descriptor->getOperation();
37
        $socket    = $descriptor->getSocket();
38
39 3
        /** @var SslHandshakeOperation $operation */
40 3
        $resource = $descriptor->getSocket()->getStreamResource();
41 3
        $result   = stream_socket_enable_crypto($resource, true, $operation->getCipher());
42 1
        if ($result === true) {
43 2
            return $operation->getNextOperation();
44 1
        } elseif ($result === false) {
45
            throw new SslHandshakeException($socket, 'SSL handshake failed.');
46
        }
47 1
48
        return $operation;
49
    }
50
}
51