Completed
Push — master ( 3baf41...27da68 )
by Anton
01:35
created

RocketChatMessage::getFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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