Completed
Push — master ( 528f7d...f4b721 )
by Kacper
04:19
created

XmppClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 108
rs 10
c 1
b 0
f 0
ccs 0
cts 38
cp 0
wmc 10
lcom 1
cbo 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A connect() 0 6 1
A getJid() 0 4 1
A handleConnect() 0 10 1
A handleFeatures() 0 10 2
A setConnector() 0 17 3
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;
17
18
19
use Kadet\Xmpp\Exception\InvalidArgumentException;
20
use Kadet\Xmpp\Network\Connector;
21
use Kadet\Xmpp\Stream\Features;
22
use Kadet\Xmpp\Xml\XmlElementFactory;
23
use Kadet\Xmpp\Xml\XmlParser;
24
use React\EventLoop\LoopInterface;
25
26
/**
27
 * Class XmppClient
28
 * @package Kadet\Xmpp
29
 *
30
 * @property-read Jid $jid Client's jid (Jabber Identifier) address.
31
 */
32
class XmppClient extends XmppStream
33
{
34
    const SASL_NAMESPACE = 'urn:ietf:params:xml:ns:xmpp-sasl';
35
36
    /**
37
     * Connector used to instantiate stream connection to server.
38
     *
39
     * @var Connector
40
     */
41
    protected $_connector;
42
43
    /**
44
     * Client's jid (Jabber Identifier) address.
45
     *
46
     * @var Jid
47
     */
48
    protected $_jid;
49
50
    /**
51
     * Client's password used in authorisation.
52
     *
53
     * @var string
54
     */
55
    protected $_password;
56
57
58
    /**
59
     * XmppClient constructor.
60
     * @param Jid                     $jid
61
     * @param string                  $password
62
     * @param Connector|LoopInterface $connector
63
     * @param XmlParser|null          $parser
64
     * @param string                  $lang
65
     */
66
    public function __construct(Jid $jid, string $password, $connector = null, XmlParser $parser = null, $lang = 'en')
67
    {
68
        parent::__construct(
69
            $parser ?: new XmlParser(new XmlElementFactory()),
70
            null, // will be set by event
71
            $lang
72
        );
73
74
        $this->_jid = $jid;
75
76
        $this->setConnector($connector);
77
        $this->connect();
78
79
        $this->_connector->on('connect', function(...$arguments) {
80
            return $this->emit('connect', $arguments);
81
        });
82
    }
83
84
    public function connect()
85
    {
86
        $this->getLogger()->debug("Connecting to {$this->_jid->domain}");
87
88
        $this->_connector->connect();
89
    }
90
91
    public function getJid()
92
    {
93
        return $this->_jid;
94
    }
95
96
    private function handleConnect($stream)
97
    {
98
        $this->exchangeStream($stream);
99
100
        $this->getLogger()->info("Connected to {$this->_jid->domain}");
101
        $this->start([
102
            'from' => (string)$this->_jid,
103
            'to'   => $this->_jid->domain
104
        ]);
105
    }
106
107
    protected function handleFeatures(Features $element)
108
    {
109
        if(!parent::handleFeatures($element)) {
110
            return false;
111
        }
112
113
        \Kadet\Xmpp\Utils\helper\dd($element->getMechanisms());
114
115
        return true;
116
    }
117
118
119
    /**
120
     * @param $connector
121
     */
122
    protected function setConnector($connector)
123
    {
124
        if ($connector instanceof LoopInterface) {
125
            $this->_connector = new Connector\TcpXmppConnector($this->_jid->domain, $connector);
126
        } elseif ($connector instanceof Connector) {
127
            $this->_connector = $connector;
128
        } else {
129
            throw new InvalidArgumentException(sprintf(
130
                '$connector must be either %s, or %s instance %s given.',
131
                LoopInterface::class, Connector::class, \Kadet\Xmpp\Utils\helper\typeof($connector)
132
            ));
133
        }
134
135
        $this->_connector->on('connect', function($stream) {
136
            $this->handleConnect($stream);
137
        });
138
    }
139
}
140