PrivmsgMessage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jerodev\PhpIrcClient\Messages;
4
5
use Jerodev\PhpIrcClient\Helpers\Event;
6
use Jerodev\PhpIrcClient\IrcChannel;
7
8
class PrivmsgMessage extends IrcMessage
9
{
10
    /** @var IrcChannel */
11
    public $channel;
12
13
    /** @var string */
14
    public $message;
15
16
    /** @var string */
17
    public $target;
18
19
    /** @var string */
20
    public $user;
21
22
    public function __construct(string $message)
23
    {
24
        parent::__construct($message);
25
        $this->user = strstr($this->source, '!', true);
26
        $this->target = $this->commandsuffix;
27
        $this->message = $this->payload;
28
    }
29
30
    public function getEvents(): array
31
    {
32
        $events = [];
33
        if ($this->target[0] === '#') {
34
            $events = [
35
                new Event('message', [$this->user, $this->channel, $this->message]),
36
                new Event("message$this->target", [$this->user, $this->channel, $this->message]),
37
            ];
38
        } else {
39
            $events = [
40
                new Event('privmsg', [$this->user, $this->target, $this->message]),
41
            ];
42
        }
43
44
        return $events;
45
    }
46
47
    public function injectChannel(array $channels): void
48
    {
49
        if (array_key_exists($this->target, $channels)) {
50
            $this->channel = $channels[$this->target];
51
        }
52
    }
53
}
54