IrcChannel::getUsers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jerodev\PhpIrcClient;
4
5
use Exception;
6
7
class IrcChannel
8
{
9
    /** @var string */
10
    private $name;
11
12
    /** @var null|string */
13
    private $topic;
14
15
    /** @var string[] */
16
    private $users;
17
18
    public function __construct(string $name)
19
    {
20
        $name = trim($name);
21
        if (empty($name)) {
22
            throw new Exception('Channel name is empty.');
23
        }
24
25
        if ($name[0] !== '#') {
26
            $name = "#$name";
27
        }
28
        $this->name = $name;
29
        $this->users = [];
30
    }
31
32
    /**
33
     *  Fetch the name of the channel, including the `#`.
34
     *
35
     *  @return string
36
     */
37
    public function getName(): string
38
    {
39
        return $this->name;
40
    }
41
42
    /**
43
     *  Get the current channel topic.
44
     *
45
     *  @return null|string
46
     */
47
    public function getTopic()
48
    {
49
        return $this->topic;
50
    }
51
52
    /**
53
     *  Fetch the list of users currently on this channel.
54
     *
55
     *  @return string[]
56
     */
57
    public function getUsers(): array
58
    {
59
        return $this->users;
60
    }
61
62
    /**
63
     *  Set the current channel topic.
64
     *
65
     *  @param string $topic The topic
66
     */
67
    public function setTopic(string $topic): void
68
    {
69
        $this->topic = $topic;
70
    }
71
72
    /**
73
     *  Set the list of active users on the channel.
74
     *  User modes (`+`, `@`) will be removed from the nicknames.
75
     *
76
     *  @param string[] $users An array of user names.
77
     */
78
    public function setUsers(array $users): void
79
    {
80
        $this->users = array_map(function ($user) {
81
            if (in_array($user[0], ['+', '@'])) {
82
                $user = substr($user, 1);
83
            }
84
85
            return $user;
86
        }, $users);
87
    }
88
}
89