TcpStream::encrypt()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, Some rights reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Network;
17
18
use React\Stream\Stream;
19
20
class TcpStream extends Stream implements SecureStream
21
{
22
    private $_secured = false;
23
24
    public function encrypt(int $type = STREAM_CRYPTO_METHOD_ANY_CLIENT) : bool
25
    {
26
        if ($this->_secured) {
27
            return true;
28
        }
29
30
        $result = true;
31
        $result &= stream_set_blocking($this->stream, 1);
32
        $result &= stream_socket_enable_crypto($this->stream, true, $type);
33
        $result &= stream_set_blocking($this->stream, 0);
34
35
        return $this->_secured = $result;
36
    }
37
38
    public function decrypt() : bool
39
    {
40
        if (!$this->_secured) {
41
            return true;
42
        }
43
44
        $result = true;
45
        $result &= stream_set_blocking($this->stream, 1);
46
        $result &= stream_socket_enable_crypto($this->stream, false);
47
        $result &= stream_set_blocking($this->stream, 0);
48
49
        return !($this->_secured = !$result);
50
    }
51
}
52