Client   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 133
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A setupImplementation() 0 12 1
A connect() 0 4 1
A disconnect() 0 4 1
A send() 0 5 1
A getEventManager() 0 4 1
A setEventManager() 0 5 1
A getOptions() 0 4 1
A getConnection() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2014 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2014 Fabian Grutschus. All rights reserved.
33
 * @license   BSD
34
 * @link      http://github.com/fabiang/xmpp
35
 */
36
37
namespace Fabiang\Xmpp;
38
39
use Fabiang\Xmpp\Options;
40
use Fabiang\Xmpp\Connection\ConnectionInterface;
41
use Fabiang\Xmpp\Connection\Socket;
42
use Fabiang\Xmpp\Protocol\ProtocolImplementationInterface;
43
use Fabiang\Xmpp\Event\EventManagerAwareInterface;
44
use Fabiang\Xmpp\Event\EventManagerInterface;
45
use Fabiang\Xmpp\Event\EventManager;
46
use Fabiang\Xmpp\EventListener\Logger;
47
48
/**
49
 * Xmpp connection client.
50
 *
51
 * @package Xmpp
52
 */
53
class Client implements EventManagerAwareInterface
54
{
55
56
    /**
57
     * Eventmanager.
58
     *
59
     * @var EventManagerInterface
60
     */
61
    protected $eventManager;
62
63
    /**
64
     * Options.
65
     *
66
     * @var Options
67
     */
68
    protected $options;
69
70
    /**
71
     * @var ConnectionInterface
72
     */
73
    protected $connection;
74
75
    /**
76
     * Constructor.
77
     *
78
     * @param Options               $options      Client options
79
     * @param EventManagerInterface $eventManager Event manager
80
     */
81 6
    public function __construct(Options $options, EventManagerInterface $eventManager = null)
82
    {
83
        // create default connection
84 6
        if (null !== $options->getConnection()) {
85 6
            $connection = $options->getConnection();
86 6
        } else {
87 3
            $connection = Socket::factory($options);
88 3
            $options->setConnection($connection);
89
        }
90 6
        $this->options    = $options;
91 6
        $this->connection = $connection;
92
93 6
        if (null === $eventManager) {
94 3
            $eventManager = new EventManager();
95 3
        }
96 6
        $this->eventManager = $eventManager;
97
98 6
        $this->setupImplementation();
99 6
    }
100
101
    /**
102
     * Setup implementation.
103
     *
104
     * @return void
105
     */
106 3
    protected function setupImplementation()
107
    {
108 3
        $this->connection->setEventManager($this->eventManager);
109 3
        $this->connection->setOptions($this->options);
110
111 3
        $implementation = $this->options->getImplementation();
112 3
        $implementation->setEventManager($this->eventManager);
113 3
        $implementation->setOptions($this->options);
114 3
        $implementation->register();
115
116 3
        $implementation->registerListener(new Logger());
117 3
    }
118
119
    /**
120
     * Connect to server.
121
     *
122
     * @return void
123
     */
124 3
    public function connect()
125
    {
126 3
        $this->connection->connect();
127 3
    }
128
129
    /**
130
     * Disconnect from server.
131
     *
132
     * @return void
133
     */
134 3
    public function disconnect()
135
    {
136 3
        $this->connection->disconnect();
137 3
    }
138
139
    /**
140
     * Send data to server.
141
     *
142
     * @param ProtocolImplementationInterface $interface Interface
143
     * @return void
144
     */
145 3
    public function send(ProtocolImplementationInterface $interface)
146
    {
147 3
        $data = $interface->toString();
148 3
        $this->connection->send($data);
149 3
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154 3
    public function getEventManager()
155
    {
156 3
        return $this->eventManager;
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162 3
    public function setEventManager(EventManagerInterface $eventManager)
163
    {
164 3
        $this->eventManager = $eventManager;
165 3
        return $this;
166
    }
167
168
    /**
169
     * Get options.
170
     *
171
     * @return Options
172
     */
173 3
    public function getOptions()
174
    {
175 3
        return $this->options;
176
    }
177
178
    /**
179
     * @return ConnectionInterface
180
     */
181 3
    public function getConnection()
182
    {
183 3
        return $this->connection;
184
    }
185
}
186