Presence   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getShow() 0 5 2
A setShow() 0 18 4
A getStatus() 0 4 1
A setStatus() 0 10 3
A getPriority() 0 4 1
A setPriority() 0 13 3
A show() 0 7 1
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2017, 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
use Kadet\Xmpp\Exception\InvalidArgumentException;
19
use Kadet\Xmpp\Jid;
20
use Kadet\Xmpp\Stanza\Presence\Show;
21
use Kadet\Xmpp\Utils\filter;
22
use Kadet\Xmpp\Xml\XmlElement;
23
24
use function Kadet\Xmpp\Utils\helper\format;
25
26
/**
27
 * Represents Presence Stanza
28
 *
29
 * @package Kadet\Xmpp\Stanza
30
 *
31
 * @property string $show     Presence show type, specifying kind of contact availability.
32
 *                            One of: available, unavailable, chat, dnd, away, xa.
33
 * @property string $status   Presence status message.
34
 * @property int    $priority Presence priority
35
 */
36
class Presence extends Stanza
37
{
38
    /**
39
     * Presence constructor.
40
     * @param array $options {
41
     *     @var Jid    $from    Jid representing "from" stanza attribute
42
     *     @var Jid    $to      Jid representing "to" stanza attribute
43
     *     @var string $id      Unique id, will be generated if omitted
44
     *     @var string $type    Stanza type
45
     *     @var string $show
46
     *     @var string status
47
     *     @var int    $priority
48
     * }
49
     */
50 13
    public function __construct(array $options = [])
51
    {
52 13
        parent::__construct('presence', $options);
53 13
    }
54
55
    /**
56
     * @return null|string
57
     */
58 4
    public function getShow()
59
    {
60 4
        return (string)$this->element('show', 'jabber:client')->innerXml // return show node content if exist
61 4
            ?? (in_array($this->type, ['available', 'unavailable']) ? $this->type : null); // or type if available
62
    }
63
64
    /**
65
     * Sets presence show. If $show is "available" or "unavailable" it sets presence type to match show kind.
66
     *
67
     * @param string $show Desired show type.
68
     */
69 6
    public function setShow(string $show = 'available')
70
    {
71 6
        if(!Show::valid($show)) {
72
            throw new InvalidArgumentException(format('$show must be one of: {possible}. {show} given.', [
73
                'possible' => implode(', ', Show::available()),
74
                'show'     => $show
75
            ]));
76
        }
77
78 6
        $predicate = filter\element('show', 'jabber:client');
79 6
        if(in_array($show, ['available', 'unavailable'])) {
80 2
            $this->remove($predicate);
81 2
            $this->type = $show;
82 2
            return;
83
        }
84
85 4
        ($this->get($predicate) ?: $this->append(new XmlElement('show', 'jabber:client')))->setContent($show);
86 4
    }
87
88
    /**
89
     * Gets presence status message.
90
     *
91
     * @return null|string
92
     */
93 1
    public function getStatus()
94
    {
95 1
        return (string)$this->element('status')->innerXml ?? null;
96
    }
97
98 1
    public function setStatus(string $status = null)
99
    {
100 1
        $predicate = filter\element('status', 'jabber:client');
101 1
        if($status === null) {
102
            $this->remove($predicate);
103
            return;
104
        }
105
106 1
        ($this->get($predicate) ?: $this->append(new XmlElement('status', 'jabber:client')))->setContent($status);
107 1
    }
108
109 1
    public function getPriority()
110
    {
111 1
        return (int)$this->element('priority')->innerXml ?? null;
112
    }
113
114 1
    public function setPriority(int $priority = null)
115
    {
116 1
        $predicate = filter\element('status', 'jabber:client');
117 1
        if(!$priority) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $priority of type null|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
118
            $this->remove($predicate);
119
        }
120
121 1
        $element = $this->has($predicate)
122
            ? $this->element('status', 'jabber:client')
123 1
            : $this->append(new XmlElement('priority', 'jabber:client'));
124
125 1
        $element->setContent((string)$priority);
126 1
    }
127
128
    public static function show(string $show, string $status = null, array $options = [])
129
    {
130
        return new self(array_merge($options, [
131
            'show' => $show,
132
            'status' => $status,
133
        ]));
134
    }
135
}