Completed
Pull Request — master (#3)
by
unknown
06:12
created

RocketChatMessage::attachment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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