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

Presence::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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\Utils\filter;
21
use Kadet\Xmpp\Xml\XmlElement;
22
23
use function Kadet\Xmpp\Utils\helper\format;
24
25
/**
26
 * Represents Presence Stanza
27
 *
28
 * @package Kadet\Xmpp\Stanza
29
 *
30
 * @property string $show     Presence show type, specifying kind of contact availability.
31
 *                            One of: available, unavailable, chat, dnd, away, xa.
32
 * @property string $status   Presence status message.
33
 * @property int    $priority Presence priority
34
 */
35
class Presence extends Stanza
36
{
37
    const POSSIBLE_SHOW = ['available', 'unavailable', 'chat', 'dnd', 'away', 'xa'];
38
39
    /**
40
     * Presence constructor.
41
     * @param array $options {
42
     *     @var Jid    $from    Jid representing "from" stanza attribute
43
     *     @var Jid    $to      Jid representing "to" stanza attribute
44
     *     @var string $id      Unique id, will be generated if omitted
45
     *     @var string $type    Stanza type
46
     *     @var string $show
47
     *     @var string status
48
     *     @var int    $priority
49
     * }
50
     */
51 4
    public function __construct(array $options = [])
52
    {
53 4
        parent::__construct('presence', $options);
54 4
    }
55
56
    /**
57
     * @return null|string
58
     */
59
    public function getShow()
60
    {
61
        return (string)$this->element('show', 'jabber:client') // return show node content if exist
62
            ?? (in_array($this->type, ['available', 'unavailable']) ? $this->type : null); // or type if available
63
    }
64
65
    /**
66
     * Sets presence show. If $show is "available" or "unavailable" it sets presence type to match show kind.
67
     *
68
     * @param string $show Desired show type.
69
     */
70
    public function setShow(string $show = 'available')
71
    {
72
        if(!in_array($show, self::POSSIBLE_SHOW)) {
73
            throw new InvalidArgumentException(format('$show must be one of: {possible}. {show} given.', [
74
                'possible' => implode(',', self::POSSIBLE_SHOW),
75
                'show'     => $show
76
            ]));
77
        }
78
79
        $predicate = filter\element('show', 'jabber:client');
80
        if(in_array($show, ['available', 'unavailable'])) {
81
            $this->remove($predicate);
82
            $this->type = $show;
83
            return;
84
        }
85
86
        ($this->get($predicate) ?: $this->append(new XmlElement('status', 'jabber:client')))->setContent($show);
87
    }
88
89
    /**
90
     * Gets presence status message.
91
     *
92
     * @return null|string
93
     */
94
    public function getStatus()
95
    {
96
        return (string)$this->element('status') ?? null;
97
    }
98
99
    public function setStatus(string $status = null)
100
    {
101
        $predicate = filter\element('status', 'jabber:client');
102
        if($status === null) {
103
            $this->remove($predicate);
104
            return;
105
        }
106
107
        ($this->get($predicate) ?: $this->append(new XmlElement('status', 'jabber:client')))->setContent($status);
108
    }
109
110
    public function getPriority()
111
    {
112
        return (int)$this->element('priority') ?? null;
113
    }
114
115
    public function setPriority(int $priority = null)
116
    {
117
        $predicate = filter\element('status', 'jabber:client');
118
        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...
119
            $this->remove($predicate);
120
        }
121
122
        $element = $this->has($predicate)
123
            ? $this->element('status', 'jabber:client')
124
            : $this->append(new XmlElement('priority', 'jabber:client'));
125
126
        $element->setContent($priority);
127
    }
128
129
    public static function show(string $show, string $status = null, array $options = [])
130
    {
131
        return new self(array_merge($options, [
132
            'show' => $show,
133
            'status' => $status,
134
        ]));
135
    }
136
}