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

SslHandshakeIoHandler::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 12
nc 3
nop 4
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 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