Completed
Push — master ( 1b605f...95b7c3 )
by Dan
06:00
created

Message::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Nopolabs\Yabot\Message;
4
5
use Nopolabs\Yabot\Helpers\SlackTrait;
6
use Nopolabs\Yabot\Slack\Client;
7
use Slack\Channel;
8
use Slack\User;
9
10
class Message
11
{
12
    use SlackTrait;
13
14
    public $data;
15
16
    /** @var User */
17
    protected $user;
18
19
    /** @var Channel */
20
    protected $channel;
21
22
    /** @var bool */
23
    protected $handled;
24
25
    /** @var string */
26
    protected $formattedText;
27
28
    /** @var string */
29
    protected $pluginText;
30
31
    public function __construct(
32
        Client $slackClient,
33
        array $data,
34
        string $formattedText,
35
        User $user = null,
36
        Channel $channel)
37
    {
38
        $this->setSlack($slackClient);
39
        $this->data = $data;
40
        $this->user = $user;
41
        $this->channel = $channel;
42
        $this->handled = false;
43
        $this->formattedText = $formattedText;
44
    }
45
46
    public function getData() : array
47
    {
48
        return $this->data;
49
    }
50
51
    public function getText() : string
52
    {
53
        return $this->data['text'];
54
    }
55
56
    public function getFormattedText() : string
57
    {
58
        return $this->formattedText;
59
    }
60
61
    public function setPluginText(string $text)
62
    {
63
        $this->pluginText = $text;
64
    }
65
66
    public function getPluginText()
67
    {
68
        return $this->pluginText;
69
    }
70
71
    public function getChannel()
72
    {
73
        return $this->channel;
74
    }
75
76
    public function getUser()
77
    {
78
        return $this->user;
79
    }
80
81
    public function getThreadTs()
82
    {
83
        return $this->data['thread_ts'] ?? $this->data['ts'];
84
    }
85
86
    public function isSelf() : bool
87
    {
88
        return $this->user === $this->getAuthedUser();
89
    }
90
91
    public function isBot() : bool
92
    {
93
        if (isset($this->data['bot_id'])) {
94
            return true;
95
        }
96
97
        if (isset($this->user->data['is_bot'])) {
98
            return (bool) $this->user->data['is_bot'];
99
        }
100
101
        return false;
102
    }
103
104
    public function hasAttachments()
105
    {
106
        return isset($this->data['attachments']) && count($this->data['attachments']) > 0;
107
    }
108
109
    public function getAttachments()
110
    {
111
        return $this->hasAttachments() ? $this->data['attachments'] : [];
112
    }
113
114
    public function reply(string $text, array $additionalParameters = [])
115
    {
116
        $this->say($text, $this->getChannel(), $additionalParameters);
117
    }
118
119
    public function thread(string $text, array $additionalParameters = [])
120
    {
121
        $additionalParameters['thread_ts'] = $this->getThreadTs();
122
        $this->reply($text, $additionalParameters);
123
    }
124
125
    public function isHandled() : bool
126
    {
127
        return $this->handled;
128
    }
129
130
    public function setHandled(bool $handled)
131
    {
132
        $this->handled = $handled;
133
    }
134
135
    public function getUsername()
136
    {
137
        $user = $this->getUser();
138
139
        return $user ? $user->getUsername() : null;
140
    }
141
142
    public function getChannelName()
143
    {
144
        return $this->getChannel()->getName();
145
    }
146
}