TlsEnabler::startTls()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 4
rs 9.2
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\Component;
17
18
use Kadet\Xmpp\Exception\Protocol\TlsException;
19
use Kadet\Xmpp\Network\SecureStream;
20
use Kadet\Xmpp\Stream\Features;
21
use Kadet\Xmpp\Utils\filter as with;
22
use Kadet\Xmpp\Xml\XmlElement;
23
use Kadet\Xmpp\XmppClient;
24
25
class TlsEnabler extends Component
26
{
27 4
    public function init()
28
    {
29
        $this->_client->on('features', function (Features $features) {
30
            return !$this->startTls($features);
31 4
        }, null, 10);
32
33 4
        $this->_client->on('element', function (XmlElement $element) {
34 2
            return $this->handleTls($element);
35 4
        }, with\element\xmlns(Features\StartTls::XMLNS));
36 4
    }
37
38 4
    public function startTls(Features $features)
39
    {
40 4
        if ($features->startTls) {
41 4
            if ($this->_client->getDecorated() instanceof SecureStream) {
42 2
                $this->_client->write(new Features\StartTls());
43
44 2
                return true; // Stop processing
45 2
            } elseif ($features->startTls->required) {
46 1
                throw new TlsException('Encryption is not available, but server requires it.');
47
            }
48
49 1
            $this->_client->getLogger()->warning('Server offers TLS encryption, but stream is not capable of it.');
50
        }
51
52 1
        return false;
53
    }
54
55 2
    private function handleTls(XmlElement $response)
56
    {
57 2
        if ($response->localName !== 'proceed') {
58
            throw new TlsException('TLS negotiation failed.'); // XMPP does not provide any useful information why it happened
59
        }
60
61
        // this function is called only by event, which can be only fired after instanceof check
62
        /** @noinspection PhpUndefinedMethodInspection */
63 2
        $this->_client->getDecorated()->encrypt(STREAM_CRYPTO_METHOD_TLS_CLIENT);
64 2
        $this->_client->restart();
65
66 2
        $this->_client->state = 'secured';
67 2
    }
68
}
69