Completed
Push — master ( 6634cb...f6cba5 )
by Kacper
06:10
created

Stanza   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 39
cts 45
cp 0.8667
rs 10
c 0
b 0
f 0
wmc 21
lcom 2
cbo 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getError() 0 4 1
A getXmlCollocations() 0 6 1
A getType() 0 4 1
A __construct() 0 5 1
A getFrom() 0 8 3
A getTo() 0 8 3
A getId() 0 4 1
A setFrom() 0 8 3
A setTo() 0 8 3
A setType() 0 4 1
A setId() 0 4 1
A regenerateId() 0 4 1
A response() 0 10 1
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\Jid;
20
use Kadet\Xmpp\Xml\XmlElement;
21
use Kadet\Xmpp\Xml\XmlFactoryCollocations;
22
23
/**
24
 * Class Stanza
25
 * @package Kadet\Xmpp\Stanza
26
 *
27
 * @property Jid|null $from  Jid representing "from" stanza attribute
28
 * @property Jid|null $to    Jid representing "to" stanza attribute
29
 * @property string   $type  Stanza type
30
 * @property string   $id    Unique stanza id
31
 * @property Error    $error Error details
32
 */
33
class Stanza extends XmlElement implements XmlFactoryCollocations
34
{
35
    /** @var bool|Jid */
36
    private $_from = false;
37
    /** @var bool|Jid */
38
    private $_to   = false;
39
40
    /**
41
     * Stanza constructor.
42
     * @param string   $kind    Stanza kind. According to XMPP RFC, one of "iq", "message", "presence"
43
     * @param array    $options {
44
     *     @var Jid    $from    Jid representing "from" stanza attribute
45
     *     @var Jid    $to      Jid representing "to" stanza attribute
46
     *     @var string $id      Unique id, will be generated if omitted
47
     *     @var string $type    Stanza type
48
     * }
49
     */
50 7
    public function __construct(string $kind, array $options = [])
51
    {
52 7
        $this->regenerateId($kind);
53 7
        parent::__construct($kind, 'jabber:client', $options);
54 7
    }
55
56 1
    public function getFrom()
57
    {
58 1
        if((string)$this->_from !== $this->hasAttribute('from')) {
59 1
            $this->_from = $this->hasAttribute('from') ? new Jid($this->getAttribute('from')) : null;
60
        }
61
62 1
        return $this->_from;
63
    }
64
65 1
    public function getTo()
66
    {
67 1
        if((string)$this->_to !== $this->hasAttribute('to')) {
68 1
            $this->_to = $this->hasAttribute('to') ? new Jid($this->getAttribute('to')) : null;
69
        }
70
71 1
        return $this->_to;
72
    }
73
74
    public function getType()
75
    {
76
        return $this->getAttribute('type');
77
    }
78
79 4
    public function getId()
80
    {
81 4
        return $this->getAttribute('id');
82
    }
83
84
    public function getError()
85
    {
86
        return $this->element('error');
87
    }
88
89 2
    public function setFrom($from)
90
    {
91 2
        if($from instanceof Jid) {
92 1
            $this->_from = $from instanceof Jid ? $from : new Jid($from);
93
        }
94
95 2
        $this->setAttribute('from', (string)$from);
96 2
    }
97
98 4
    public function setTo($to)
99
    {
100 4
        if($to instanceof Jid) {
101 3
            $this->_to = $to instanceof Jid ? $to : new Jid($to);
102
        }
103
104 4
        $this->setAttribute('to', (string)$to);
105 4
    }
106
107 7
    public function setType(string $type)
108
    {
109 7
        $this->setAttribute('type', $type);
110 7
    }
111
112 7
    public function setId(string $id)
113
    {
114 7
        $this->setAttribute('id', $id);
115 7
    }
116
117 7
    public function regenerateId(string $prefix = null)
118
    {
119 7
        $this->id = uniqid($prefix, true);
120 7
    }
121
122 1
    public function response()
123
    {
124 1
        $response = static::plain($this->fullName, $this->namespace);
125
126 1
        $response->to   = $this->from;
127 1
        $response->from = $this->to;
128 1
        $response->id   = $this->id;
129
130 1
        return $response;
131
    }
132
133
    public static function getXmlCollocations() : array
134
    {
135
        return [
136
            [ Error::class, 'name' => 'error', 'uri' => 'jabber:client' ],
137
        ];
138
    }
139
}
140