Completed
Pull Request — master (#3)
by
unknown
02:03
created

RocketChatMessage::clearAttachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NotificationChannels\RocketChat;
4
5
class RocketChatMessage
6
{
7
    /**
8
     * RocketChat channel id.
9
     *
10
     * @var string
11
     */
12
    public $channel = '';
13
14
    /**
15
     * A user or app access token.
16
     *
17
     * @var string
18
     */
19
    public $from = '';
20
21
    /**
22
     * The text content of the message.
23
     *
24
     * @var string
25
     */
26
    public $content = '';
27
28
    /**
29
     * The alias name of the message.
30
     *
31
     * @var string
32
     */
33
    public $alias = '';
34
35
    /**
36
     * The avatar emoji of the message.
37
     *
38
     * @var string
39
     */
40
    public $emoji = '';
41
42
    /**
43
     * The avatar image of the message.
44
     *
45
     * @var string
46
     */
47
    public $avatar = '';
48
49
    /**
50
     * Attachments of the message.
51
     *
52
     * @var RocketChatAttachment[]
53
     */
54
    public $attachments = [];
55
56
    /**
57
     * Create a new instance of RocketChatMessage.
58
     *
59
     * @param  string  $content
60
     * @return static
61
     */
62 4
    public static function create($content = ''): self
63
    {
64 4
        return new static($content);
65
    }
66
67
    /**
68
     * Create a new instance of RocketChatMessage.
69
     *
70
     * @param $content
71
     */
72 14
    public function __construct($content = '')
73
    {
74 14
        $this->content($content);
75 14
    }
76
77
    /**
78
     * Set the sender's access token.
79
     *
80
     * @param  string  $accessToken
81
     * @return $this
82
     */
83 3
    public function from($accessToken): self
84
    {
85 3
        $this->from = $accessToken;
86
87 3
        return $this;
88
    }
89
90
    /**
91
     * Set the RocketChat room the message should be sent to.
92
     *
93
     * @param  string $channel
94
     * @return $this
95
     */
96 2
    public function to($channel): self
97
    {
98 2
        $this->channel = $channel;
99
100 2
        return $this;
101
    }
102
103
    /**
104
     * Set the sender's alias.
105
     *
106
     * @param  string $alias
107
     * @return $this
108
     */
109 1
    public function alias(string $alias): self
110
    {
111 1
        $this->alias = $alias;
112
113 1
        return $this;
114
    }
115
116
    /**
117
     * Set the sender's emoji.
118
     *
119
     * @param  string $emoji
120
     * @return $this
121
     */
122 1
    public function emoji(string $emoji): self
123
    {
124 1
        $this->emoji = $emoji;
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * Set the sender's avatar.
131
     *
132
     * @param  string $avatar
133
     * @return $this
134
     */
135 1
    public function avatar(string $avatar): self
136
    {
137 1
        $this->avatar = $avatar;
138
139 1
        return $this;
140
    }
141
142
    /**
143
     * Set the content of the RocketChat message. Supports GitHub flavoured markdown.
144
     *
145
     * @param  string  $content
146
     * @return $this
147
     */
148 14
    public function content($content): self
149
    {
150 14
        $this->content = $content;
151
152 14
        return $this;
153
    }
154
155
    /**
156
     * Add an attachment to the message.
157
     *
158
     * @param array|RocketChatAttachment $attachment
159
     *
160
     * @return $this
161
     */
162 4
    public function attachment($attachment): self
163
    {
164 4
        if (! ($attachment instanceof RocketChatAttachment)) {
165 1
            $attachment = new RocketChatAttachment($attachment);
166
        }
167
168 4
        $this->attachments[] = $attachment;
169
170 4
        return $this;
171
    }
172
173
    /**
174
     * Add multiple attachments to the message.
175
     *
176
     * @param array $attachments
177
     * @return $this
178
     */
179 2
    public function attachments(array $attachments): self
180
    {
181 2
        foreach ($attachments as $attachment) {
182 2
            $this->attachment($attachment);
183
        }
184
185 2
        return $this;
186
    }
187
188
    /**
189
     * clear all attachments.
190
     *
191
     * @return $this
192
     */
193 1
    public function clearAttachments(): self
194
    {
195 1
        $this->attachments = [];
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * Get an array representation of the RocketChatMessage.
202
     *
203
     * @return array
204
     */
205 1
    public function toArray(): array
206
    {
207 1
        $attachments = [];
208 1
        foreach ($this->attachments as $attachment) {
209
            $attachments[] = $attachment->toArray();
210
        }
211
212 1
        $message = array_filter([
213 1
            'text' => $this->content,
214 1
            'channel' => $this->room,
0 ignored issues
show
Bug introduced by
The property room does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
215
            'alias' => $this->alias,
216
            'emoji' => $this->emoji,
217
            'avatar' => $this->avatar,
218
            'attachments' => $attachments,
219
        ]);
220
221
        return $message;
222
    }
223
}
224