Completed
Push — master ( a00622...08b3e1 )
by Martin
14:53
created

RawMessage::prefix()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 16
nop 0
dl 0
loc 11
rs 8.8571
1
<?php
2
declare(strict_types=1);
3
4
namespace Spires\Irc\Message\Inbound;
5
6
class RawMessage extends \Spires\Irc\Message\Outbound\RawMessage
7
{
8
    protected $nickname;
9
    protected $username;
10
    protected $hostname;
11
    protected $serverName;
12
13
    public function __construct(
14
        string $nickname,
15
        string $username,
16
        string $hostname,
17
        string $serverName,
18
        string $command,
19
        string $params
20
    ) {
21
        $this->nickname = $nickname;
22
        $this->username = $username;
23
        $this->hostname = $hostname;
24
        $this->serverName = $serverName;
25
26
        parent::__construct($command, $params);
27
    }
28
29
    public static function from(RawMessage $message)
30
    {
31
        return new static(
32
            $message->nickname(),
33
            $message->username(),
34
            $message->hostname(),
35
            $message->serverName(),
36
            $message->command(),
37
            $message->params()
38
        );
39
    }
40
41
    public function nickname() : string
42
    {
43
        return $this->nickname;
44
    }
45
46
    public function username() : string
47
    {
48
        return $this->username;
49
    }
50
51
    public function hostname() : string
52
    {
53
        return $this->hostname;
54
    }
55
56
    public function serverName() : string
57
    {
58
        return $this->serverName;
59
    }
60
61
    public function prefix()
62
    {
63
        $prefix = '';
64
65
        $prefix .= empty($this->nickname) ? '' : ":{$this->nickname}";
66
        $prefix .= empty($this->username) ? '' : "!{$this->username}";
67
        $prefix .= empty($this->hostname) ? '' : "@{$this->hostname}";
68
        $prefix .= empty($this->serverName) ? '' : ":{$this->serverName}";
69
70
        return trim($prefix);
71
    }
72
73
    public function __toString() : string
74
    {
75
        return trim($this->prefix() . ' ' . trim($this->command() . ' ' . $this->params));
76
    }
77
78
    public function toArray() : array
79
    {
80
        return [
81
            'nickname' => $this->nickname,
82
            'username' => $this->username,
83
            'hostname' => $this->hostname,
84
            'serverName' => $this->serverName,
85
            'command' => $this->command,
86
            'params' => $this->params,
87
        ];
88
    }
89
}
90