BindingException::_conditionDescription()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, Some rights 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\Exception\Protocol;
17
18
19
use Exception;
20
use Kadet\Xmpp\Jid;
21
use Kadet\Xmpp\Stanza\Error;
22
23
class BindingException extends StanzaException
24
{
25
    /**
26
     * Construct the exception. Note: The message is NOT binary safe.
27
     *
28
     * @param Jid       $jid
29
     * @param Error     $error
30
     * @param Exception $previous [optional] The previous exception used for the exception chaining. Since 5.3.0
31
     * @return static
32
     * @since    5.1.0
33
     */
34
    public static function fromError(Jid $jid, Error $error, Exception $previous = null)
35
    {
36
        return new static($error, \Kadet\Xmpp\Utils\helper\format("Cannot bind {resource} for {bare}. {condition}", [
37
            'resource'  => $jid->resource ?: "no resource",
38
            'bare'      => (string)$jid->bare(),
39
            'condition' => self::_conditionDescription($error)
40
        ]), 0, $previous);
41
    }
42
43
    private static function _conditionDescription(Error $error)
44
    {
45
        if(!empty($error->text)) {
46
            return $error->text;
47
        }
48
49
        return [
50
            'bad-request' => 'Bad Request: "{resource}" is not valid XMPP resource identifier.',
51
            'conflict'    => 'Conflict: {bare}/{resource} is already in use.'
52
        ][$error->condition] ?? 'Unknown reason: '.$error->condition;
53
    }
54
}
55