Completed
Pull Request — master (#7)
by Anton
01:11
created

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