-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBinding.php
68 lines (55 loc) · 1.72 KB
/
Binding.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Nucleus - XMPP Library for PHP
*
* Copyright (C) 2016, Some rights reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
namespace Kadet\Xmpp\Component;
use Kadet\Xmpp\Exception\Protocol\BindingException;
use Kadet\Xmpp\Stanza\Iq;
use Kadet\Xmpp\Stream\Features;
use Kadet\Xmpp\Xml\XmlElement;
use Kadet\Xmpp\XmppClient;
use \Kadet\Xmpp\Utils\filter as with;
class Binding extends Component
{
const XMLNS = 'urn:ietf:params:xml:ns:xmpp-bind';
public function setClient(XmppClient $client)
{
parent::setClient($client);
$client->on('features', function (Features $features) {
return !$this->bind($features);
});
}
public function bind(Features $features)
{
if($features->has(with\element('bind', self::XMLNS))) {
$stanza = new Iq('set');
$bind = $stanza->append(new Iq\Query(self::XMLNS, 'bind'));
if(!$this->_client->jid->isBare()) {
$bind->append(new XmlElement('resource', null, ['content' => $this->_client->jid->resource]));
}
$this->_client->once('element', function(Iq $element) {
$this->handleResult($element);
}, with\stanza\id($stanza->id));
$this->_client->write($stanza);
return true;
}
return false;
}
public function handleResult(Iq $stanza)
{
if($stanza->type === 'error') {
throw BindingException::fromError($this->_client->jid, $stanza->error);
}
$this->_client->bind($stanza->element('bind', self::XMLNS)->element('jid')->innerXml);
}
}