Completed
Pull Request — master (#3)
by
unknown
01:41
created

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