Completed
Push — master ( 7a5aed...4799e1 )
by Kacper
06:25
created

Binding::handleResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 1
b 0
f 1
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right 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\Module;
17
18
19
use Kadet\Xmpp\Stanza\Stanza;
20
use Kadet\Xmpp\Stream\Features;
21
use Kadet\Xmpp\Xml\XmlElement;
22
use Kadet\Xmpp\XmppClient;
23
use \Kadet\Xmpp\Utils\filter as with;
24
25
class Binding extends ClientModule
26
{
27
    const XMLNS = 'urn:ietf:params:xml:ns:xmpp-bind';
28
29
    public function setClient(XmppClient $client)
30
    {
31
        parent::setClient($client);
32
33
        $client->on('features', function (Features $features) {
34
            return !$this->bind($features);
35
        });
36
    }
37
38
    public function bind(Features $features)
39
    {
40
        if($features->has(\Kadet\Xmpp\Utils\filter\element('bind', self::XMLNS))) {
41
            $stanza = new Stanza('iq', ['type' => 'set']);
42
            $bind = $stanza->append(new XmlElement('bind', self::XMLNS));
43
44
            if(!$this->_client->jid->isBare()) {
45
                $bind->append(new XmlElement('resource', null, $this->_client->jid->resource));
46
            }
47
48
            $this->_client->once('element', function(Stanza $element) {
49
                $this->handleResult($element);
50
            }, with\stanza\id($stanza->id));
51
52
            $this->_client->write($stanza);
53
            return true;
54
        }
55
56
        return false;
57
    }
58
59
    public function handleResult(Stanza $stanza)
60
    {
61
        if($stanza->type === 'result') {
62
            $this->_client->bind($stanza->element('bind', self::XMLNS)->element('jid')->innerXml);
63
        }
64
    }
65
}
66