Completed
Push — develop ( fe6a9b...b413f3 )
by Fabian
29:04 queued 04:14
created

Options::setAuthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\Connection\ConnectionInterface;
40
use Fabiang\Xmpp\Protocol\ImplementationInterface;
41
use Fabiang\Xmpp\Protocol\DefaultImplementation;
42
use Psr\Log\LoggerInterface;
43
44
/**
45
 * Xmpp connection options.
46
 *
47
 * @package Xmpp
48
 */
49
class Options
50
{
51
52
    /**
53
     *
54
     * @var ImplementationInterface
55
     */
56
    protected $implementation;
57
58
    /**
59
     *
60
     * @var string
61
     */
62
    protected $address;
63
64
    /**
65
     * Connection object.
66
     *
67
     * @var ConnectionInterface
68
     */
69
    protected $connection;
70
71
    /**
72
     * PSR-3 Logger interface.
73
     *
74
     * @var LoggerInterface
75
     */
76
    protected $logger;
77
78
    /**
79
     *
80
     * @var string
81
     */
82
    protected $to;
83
84
    /**
85
     *
86
     * @var string
87
     */
88
    protected $username;
89
90
    /**
91
     *
92
     * @var string
93
     */
94
    protected $password;
95
96
    /**
97
     *
98
     * @var string
99
     */
100
    protected $jid;
101
102
    /**
103
     *
104
     * @var boolean
105
     */
106
    protected $authenticated = false;
107
108
    /**
109
     *
110
     * @var array
111
     */
112
    protected $users = array();
113
114
    /**
115
     * Timeout for connection.
116
     *
117
     * @var integer
118
     */
119
    protected $timeout = 30;
120
121
    /**
122
     * Authentication methods.
123
     *
124
     * @var array
125
     */
126
    protected $authenticationClasses = array(
127
        'digest-md5' => '\\Fabiang\\Xmpp\\EventListener\\Stream\\Authentication\\DigestMd5',
128
        'plain'      => '\\Fabiang\\Xmpp\\EventListener\\Stream\\Authentication\\Plain'
129
    );
130
131
132
    /**
133
     * Options used to create a stream context
134
     *
135
     * @var array
136
     */
137
    protected $contextOptions = array();
138
139
140
    /**
141
     * Constructor.
142
     *
143
     * @param string $address Server address
144
     */
145
    public function __construct($address = null)
146
    {
147
        if (null !== $address) {
148
            $this->setAddress($address);
149
        }
150
    }
151
152
    /**
153
     * Get protocol implementation.
154
     *
155
     * @return ImplementationInterface
156
     */
157
    public function getImplementation()
158
    {
159
        if (null === $this->implementation) {
160
            $this->setImplementation(new DefaultImplementation());
161
        }
162
163
        return $this->implementation;
164
    }
165
166
    /**
167
     * Set protocol implementation.
168
     *
169
     * @param ImplementationInterface $implementation
170
     * @return $this
171
     */
172
    public function setImplementation(ImplementationInterface $implementation)
173
    {
174
        $this->implementation = $implementation;
175
        return $this;
176
    }
177
178
    /**
179
     * Get server address.
180
     *
181
     * @return string
182
     */
183
    public function getAddress()
184
    {
185
        return $this->address;
186
    }
187
188
    /**
189
     * Set server address.
190
     *
191
     * When a address is passed this setter also calls setTo with the hostname part of the address.
192
     *
193
     * @param string $address Server address
194
     * @return $this
195
     */
196
    public function setAddress($address)
197
    {
198
        $this->address = (string) $address;
199
        if (false !== ($host = parse_url($address, PHP_URL_HOST))) {
200
            $this->setTo($host);
201
        }
202
        return $this;
203
    }
204
205
    /**
206
     * Get connection object.
207
     *
208
     * @return ConnectionInterface
209
     */
210
    public function getConnection()
211
    {
212
        return $this->connection;
213
    }
214
215
    /**
216
     * Set connection object.
217
     *
218
     * @param ConnectionInterface $connection
219
     * @return $this
220
     */
221
    public function setConnection(ConnectionInterface $connection)
222
    {
223
        $this->connection = $connection;
224
        return $this;
225
    }
226
227
    /**
228
     * Get logger instance.
229
     *
230
     * @return LoggerInterface
231
     */
232
    public function getLogger()
233
    {
234
        return $this->logger;
235
    }
236
237
    /**
238
     * Set logger instance.
239
     *
240
     * @param \Psr\Log\LoggerInterface $logger PSR-3 Logger
241
     * @return $this
242
     */
243
    public function setLogger(LoggerInterface $logger)
244
    {
245
        $this->logger = $logger;
246
        return $this;
247
    }
248
249
    /**
250
     * Get server name.
251
     *
252
     * @return string
253
     */
254
    public function getTo()
255
    {
256
        return $this->to;
257
    }
258
259
    /**
260
     * Set server name.
261
     *
262
     * This value is send to the server in requests as to="" attribute.
263
     *
264
     * @param string $to
265
     * @return $this
266
     */
267
    public function setTo($to)
268
    {
269
        $this->to = (string) $to;
270
        return $this;
271
    }
272
273
    /**
274
     * Get username.
275
     *
276
     * @return string
277
     */
278
    public function getUsername()
279
    {
280
        return $this->username;
281
    }
282
283
    /**
284
     * Set username.
285
     *
286
     * @param string $username
287
     * @return $this
288
     */
289
    public function setUsername($username)
290
    {
291
        $this->username = (string) $username;
292
        return $this;
293
    }
294
295
    /**
296
     * Get resource.
297
     *
298
     * @return string
299
     */
300
    public function getResource()
301
    {
302
        $username = $this->getUsername();
303
        $username = explode('/', $username);
304
        return isset($username[1]) ? $username[1] : '';
305
    }
306
307
    /**
308
     * Get password.
309
     *
310
     * @return string
311
     */
312
    public function getPassword()
313
    {
314
        return $this->password;
315
    }
316
317
    /**
318
     * Set password.
319
     *
320
     * @param string $password
321
     * @return $this
322
     */
323
    public function setPassword($password)
324
    {
325
        $this->password = (string) $password;
326
        return $this;
327
    }
328
329
    /**
330
     * Get users jid.
331
     *
332
     * @return string
333
     */
334
    public function getJid()
335
    {
336
        return $this->jid;
337
    }
338
339
    /**
340
     * Set users jid.
341
     *
342
     * @param string $jid
343
     * @return $this
344
     */
345
    public function setJid($jid)
346
    {
347
        $this->jid = (string) $jid;
348
        return $this;
349
    }
350
351
    /**
352
     * Is user authenticated.
353
     *
354
     * @return boolean
355
     */
356
    public function isAuthenticated()
357
    {
358
        return $this->authenticated;
359
    }
360
361
    /**
362
     * Set authenticated.
363
     *
364
     * @param boolean $authenticated Flag
365
     * @return $this
366
     */
367
    public function setAuthenticated($authenticated)
368
    {
369
        $this->authenticated = (bool) $authenticated;
370
        return $this;
371
    }
372
373
    /**
374
     * Get users.
375
     *
376
     * @return Protocol\User\User[]
377
     */
378
    public function getUsers()
379
    {
380
        return $this->users;
381
    }
382
383
    /**
384
     * Set users.
385
     *
386
     * @param array $users User list
387
     * @return $this
388
     */
389
    public function setUsers(array $users)
390
    {
391
        $this->users = $users;
392
        return $this;
393
    }
394
395
    /**
396
     * Get authentication classes.
397
     *
398
     * @return array
399
     */
400
    public function getAuthenticationClasses()
401
    {
402
        return $this->authenticationClasses;
403
    }
404
405
    /**
406
     *
407
     * @param array $authenticationClasses Authentication classes
408
     * @return $this
409
     */
410
    public function setAuthenticationClasses(array $authenticationClasses)
411
    {
412
        $this->authenticationClasses = $authenticationClasses;
413
        return $this;
414
    }
415
416
    /**
417
     * Get timeout for connection.
418
     *
419
     * @return integer
420
     */
421
    public function getTimeout()
422
    {
423
        return $this->timeout;
424
    }
425
426
    /**
427
     * Set timeout for connection.
428
     *
429
     * @param integer $timeout Seconds
430
     * @return \Fabiang\Xmpp\Options
431
     */
432
    public function setTimeout($timeout)
433
    {
434
        $this->timeout = (int) $timeout;
435
        return $this;
436
    }
437
438
    /**
439
     * Get context options for connection
440
     *
441
     * @return array
442
     */
443
    public function getContextOptions()
444
    {
445
        return $this->contextOptions;
446
    }
447
448
    /**
449
     *  Set context options for connection
450
     *
451
     * @param array $contextOptions
452
     * @return \Fabiang\Xmpp\Options
453
     */
454
    public function setContextOptions($contextOptions)
455
    {
456
        $this->contextOptions = (array) $contextOptions;
457
        return $this;
458
    }
459
}
460