Message   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 3
dl 0
loc 138
rs 10
c 0
b 0
f 0

19 Methods

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