Completed
Push — master ( 5f3a6c...fcd09b )
by Kacper
05:50
created

Error   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getBy() 0 4 1
A setBy() 0 4 1
A getType() 0 4 1
A setType() 0 12 2
A getCondition() 0 7 1
A setCondition() 0 4 1
A getText() 0 7 1
A setText() 0 11 2
A __construct() 0 14 3
A _definedCondition() 0 15 2
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\Stanza;
17
18
19
use Kadet\Xmpp\Exception\InvalidArgumentException;
20
use Kadet\Xmpp\Exception\NotImplementedException;
21
use Kadet\Xmpp\Xml\XmlElement;
22
use Kadet\Xmpp\Utils\filter;
23
24
use function Kadet\Xmpp\Utils\helper\format;
25
26
/**
27
 * Class Error
28
 * @package Kadet\Xmpp\Stanza
29
 * @see     http://xmpp.org/rfcs/rfc6120.html#stanzas-error
30
 *
31
 * @property string $type      Error type, describes how to deal with event
32
 * @property string $by        Error generator name
33
 * @property string $condition Error defined condition, equivalent of code
34
 * @property string $text      Textual description of error
35
 */
36
class Error extends XmlElement
37
{
38
    const XMLNS = 'urn:ietf:params:xml:ns:xmpp-stanzas';
39
40
    /**
41
     * @return string
42
     */
43
    public function getBy(): string
44
    {
45
        return $this->getAttribute('by');
46
    }
47
48
    /**
49
     * @param string $by
50
     */
51
    public function setBy(string $by)
0 ignored issues
show
Unused Code introduced by
The parameter $by is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        $this->setAttribute('by');
0 ignored issues
show
Bug introduced by
The call to setAttribute() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getType(): string
60
    {
61
        return $this->getAttribute('type');
62
    }
63
64
    /**
65
     * @param string $type
66
     */
67
    public function setType(string $type)
68
    {
69
        if(!in_array($type, ['auth', 'cancel', 'continue', 'modify', 'wait'])) {
70
            throw new InvalidArgumentException(
71
                format("Error type must be 'auth', 'cancel', 'continue', 'modify' or 'wait', '{type}' given.", [
72
                    'type' => $type
73
                ])
74
            );
75
        }
76
77
        $this->setAttribute('type');
0 ignored issues
show
Bug introduced by
The call to setAttribute() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getCondition(): string
84
    {
85
        return $this->get(filter\all(
86
            filter\xmlns(self::XMLNS),
87
            filter\not(filter\tag('text'))
88
        ))->localName;
89
    }
90
91
    /**
92
     * @param string $condition
93
     */
94
    public function setCondition(string $condition)
0 ignored issues
show
Unused Code introduced by
The parameter $condition is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        throw new NotImplementedException("Condition setting awaits for implementation"); // todo: implement
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getText(): string
103
    {
104
        return (string)$this->get(filter\all(
105
            filter\xmlns(self::XMLNS),
106
            filter\tag('text')
107
        ));
108
    }
109
110
    /**
111
     * @param string $text
112
     */
113
    public function setText(string $text)
114
    {
115
        if(!$this->text) {
116
            $this->append(new XmlElement('text', self::XMLNS));
117
        }
118
119
        $this->get(filter\all(
120
            filter\xmlns('urn:ietf:params:xml:ns:xmpp-stanzas'),
121
            filter\tag('text')
122
        ))->innerXml = $text;
123
    }
124
125
    /**
126
     * XmlElement constructor
127
     * @param string|XmlElement $condition
128
     * @param string $description
129
     * @param array  $options
130
     */
131
    public function __construct(string $condition, string $description = null, array $options = [])
132
    {
133
        $content = [
134
            !$condition instanceof XmlElement ? $this->_definedCondition($condition) : $condition
135
        ];
136
137
        if($description !== null) {
138
            $content[] = new XmlElement('text', self::XMLNS, ['content' => $description]);
139
        }
140
141
        parent::__construct('error', null, array_merge_recursive($options, [
142
            'content' => $content
143
        ]));
144
    }
145
146
    private function _definedCondition(string $condition) : XmlElement
147
    {
148
        if(!in_array($condition, [
149
            'bad-request', 'conflict', 'feature-not-implemented', 'forbidden',
150
            'gone', 'internal-server-error', 'item-not-found', 'jid-malformed',
151
            'not-acceptable', 'not-allowed', 'not-authorized', 'policy-violation',
152
            'recipient-unavailable', 'redirect', 'registration-required',
153
            'remote-server-not-found', 'remote-server-timeout', 'resource-constraint',
154
            'service-unavailable', 'subscription-required', 'undefined-condition'
155
        ])) {
156
            throw new InvalidArgumentException('Condition must be one of conditions specified by RFC 6120: http://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions');
157
        }
158
159
        return new XmlElement($condition, self::XMLNS);
160
    }
161
}
162