Completed
Branch master (1d1574)
by Evgenij
18:38
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\RequestExecutor\EventHandlerInterface;
14
use AsyncSockets\RequestExecutor\IoHandlerInterface;
15
use AsyncSockets\Operation\OperationInterface;
16
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
17
use AsyncSockets\Operation\SslHandshakeOperation;
18
use AsyncSockets\Socket\SocketInterface;
19
20
/**
21
 * Class SslHandshakeIoHandler
22
 */
23
class SslHandshakeIoHandler implements IoHandlerInterface
24
{
25
    /** {@inheritdoc} */
26 1
    public function supports(OperationInterface $operation)
27
    {
28 1
        return $operation instanceof SslHandshakeOperation;
29
    }
30
31
    /** {@inheritdoc} */
32 3
    public function handle(
33
        OperationInterface $operation,
34
        SocketInterface $socket,
35
        RequestExecutorInterface $executor,
36
        EventHandlerInterface $eventHandler
37
    ) {
38
        /** @var SslHandshakeOperation $operation */
39 3
        $resource = $socket->getStreamResource();
40 3
        $result   = stream_socket_enable_crypto($resource, true, $operation->getCipher());
41 3
        if ($result === true) {
42 1
            return $operation->getNextOperation();
43 2
        } elseif ($result === false) {
44 1
            throw new SslHandshakeException($socket, 'SSL handshake failed.');
45
        }
46
47 1
        return $operation;
48
    }
49
}
50