RocketChatMessage   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 1
dl 0
loc 188
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 4 1
A getChannel() 0 4 1
A getFrom() 0 4 1
A from() 0 6 1
A to() 0 6 1
A alias() 0 6 1
A emoji() 0 6 1
A avatar() 0 6 1
A content() 0 6 1
A attachment() 0 10 2
A attachments() 0 8 2
A toArray() 0 17 2
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 create(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 15
    public function __construct(string $content = '')
47
    {
48 15
        $this->content($content);
49 15
    }
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 15
    public function content(string $content): self
134
    {
135 15
        $this->content = $content;
136
137 15
        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 3
    public function attachment($attachment): self
147
    {
148 3
        if (! ($attachment instanceof RocketChatAttachment)) {
149 1
            $attachment = new RocketChatAttachment($attachment);
150
        }
151
152 3
        $this->attachments[] = $attachment;
153
154 3
        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 1
    public function attachments(array $attachments): self
164
    {
165 1
        foreach ($attachments as $attachment) {
166 1
            $this->attachment($attachment);
167
        }
168
169 1
        return $this;
170
    }
171
172
    /**
173
     * Get an array representation of the RocketChatMessage.
174
     *
175
     * @return array
176
     */
177 12
    public function toArray(): array
178
    {
179 12
        $attachments = [];
180
181 12
        foreach ($this->attachments as $attachment) {
182 3
            $attachments[] = $attachment->toArray();
183
        }
184
185 12
        return array_filter([
186 12
            'text' => $this->content,
187 12
            'channel' => $this->channel,
188 12
            'alias' => $this->alias,
189 12
            'emoji' => $this->emoji,
190 12
            'avatar' => $this->avatar,
191 12
            'attachments' => $attachments,
192
        ]);
193
    }
194
}
195