Test Setup Failed
Pull Request — master (#48)
by
unknown
07:50
created

Message::isAsUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
namespace Slack\Message;
3
4
use Slack\ClientObject;
5
6
/**
7
 * Represents a chat message and its data.
8
 */
9
class Message extends ClientObject
10
{
11
    /**
12
     * Gets the message text.
13
     *
14
     * @return string
15
     */
16 2
    public function getText()
17
    {
18 2
        return $this->data['text'];
19
    }
20
21
    /**
22
     * Checks if Markdown is enabled for the message text.
23
     *
24
     * @return bool
25
     */
26
    public function isMarkdownEnabled()
27
    {
28
        return isset($this->data['mrkdwn']) ? $this->data['mrkdwn'] == true : true;
29
    }
30
31
    /**
32
     * Checks if the message has attachments.
33
     *
34
     * @return bool True if the message has attachments, otherwise false.
35
     */
36 3
    public function hasAttachments()
37
    {
38 3
        return isset($this->data['attachments']) && count($this->data['attachments']) > 0;
39
    }
40
41
    /**
42
     * Gets all message attachments.
43
     *
44
     * @return Attachment[]
45
     */
46 2
    public function getAttachments()
47
    {
48 2
        return isset($this->data['attachments']) ? $this->data['attachments'] : [];
49
    }
50
51
    /**
52
     * Gets the channel the message is in.
53
     *
54
     * @return \React\Promise\PromiseInterface
55
     */
56 2
    public function getChannel()
57
    {
58 2
        return $this->client->getChannelById($this->data['channel']);
59
    }
60
61
    /**
62
     * Gets the user that sent the message.
63
     *
64
     * @return \React\Promise\PromiseInterface
65
     */
66 2
    public function getUser()
67
    {
68 2
        return $this->client->getUserById($this->data['user']);
69
    }
70
71
    /**
72
     * Send message as user or not.
73
     * By default sends as user.
74
     *
75
     * @return bool
76
     */
77
    public function isAsUser()
78
    {
79
        return isset($this->data['as_user']) ? $this->data['as_user'] : true;
80
    }
81
82
    /**
83
     * Get message's user display name
84
     *
85
     * @return string
86
     */
87
    public function getUsername()
88
    {
89
        return isset($this->data['username']) ? $this->data['username'] : '';
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 View Code Duplication
    public function jsonUnserialize(array $data)
96
    {
97
        if (!isset($this->data['attachments'])) {
98
            return;
99
        }
100
101
        for ($i = 0; $i < count($this->data['attachments']); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
102
            $this->data['attachments'][$i] = Attachment::fromData($this->data['attachments'][$i]);
103
        }
104
    }
105
}
106