Passed
Push — master ( f5488c...539e01 )
by Jeroen
01:53
created

KickMessage::injectChannel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Jerodev\PhpIrcClient\Messages;
4
5
use Jerodev\PhpIrcClient\Helpers\Event;
6
use Jerodev\PhpIrcClient\IrcChannel;
7
use Jerodev\PhpIrcClient\IrcClient;
8
9
class KickMessage extends IrcMessage
10
{
11
    /** @var IrcChannel */
12
    public $channel;
13
14
    /** @var string */
15
    public $message;
16
17
    /** @var string */
18
    private $target;
19
20
    /** @var string */
21
    public $user;
22
23
    public function __construct(string $message)
24
    {
25
        parent::__construct($message);
26
27
        [$this->target, $this->user] = explode(' ', $this->commandsuffix);
28
        $this->message = $this->payload;
29
    }
30
31
    /**
32
     *  When the bot is kicked form a channel, it might need to auto rejoin.
33
     */
34
    public function handle(IrcClient $client, bool $force = false): void
35
    {
36
        if ($this->handled && !$force) {
37
            return;
38
        }
39
40
        if ($client->getNickname() === $this->user && $client->shouldAutoRejoin()) {
41
            $client->join($this->target);
42
        }
43
    }
44
45
    public function getEvents(): array
46
    {
47
        return [
48
            new Event('kick', [$this->channel, $this->user, $this->message]),
49
        ];
50
    }
51
52
    public function injectChannel(array $channels): void
53
    {
54
        if (array_key_exists($this->target, $channels)) {
55
            $this->channel = $channels[$this->target];
56
        }
57
    }
58
}
59