Completed
Push — master ( 08f84c...665c45 )
by Peter
16:12 queued 14:15
created

HipChatMessage::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
crap 2
1
<?php
2
3
namespace NotificationChannels\HipChat;
4
5
class HipChatMessage
6
{
7
    /**
8
     * The HipChat room identifier.
9
     *
10
     * @var string
11
     */
12
    public $room = '';
13
14
    /**
15
     * A label to be shown in addition to the sender's name.
16
     *
17
     * @var string
18
     */
19
    public $from = '';
20
21
    /**
22
     * The format of the notification (text, html).
23
     *
24
     * @var string
25
     */
26
    public $format = 'text';
27
28
    /**
29
     * Should a message trigger a user notification in a HipChat client.
30
     *
31
     * @var bool
32
     */
33
    public $notify = false;
34
35
    /**
36
     * The "level" of the notification (info, success, error).
37
     *
38
     * @var string
39
     */
40
    public $level = 'info';
41
42
    /**
43
     * The color of the notification (yellow, green, red, purple, gray, random).
44
     *
45
     * @var string
46
     */
47
    public $color = 'gray';
48
49
    /**
50
     * The text content of the message.
51
     *
52
     * @var string
53
     */
54
    public $content = '';
55
56
    /**
57
     * An instance of Card object.
58
     *
59
     * @var Card
60
     */
61
    public $card;
62
63
    /**
64
     * The message id to to attach this notification to, for example if this notification is
65
     * in response to a particular message. For supported clients, this will display the
66
     * notification in the context of the referenced message specified by attach_to parameter.
67
     * If this is not possible to attach the notification, it will be rendered as an unattached
68
     * notification. The message must be in the same room as that the notification is sent to.
69
     *
70
     * @var string
71
     */
72
    public $attachTo;
73
74
    /**
75
     * Create a new instance of HipChatMessage.
76
     *
77
     * @param string $content
78
     * @return static
79
     */
80 19
    public static function create($content = '')
81
    {
82 19
        return new static($content);
83
    }
84
85
    /**
86
     * Create a new instance of HipChatMessage.
87
     *
88
     * @param $content
89
     */
90 22
    public function __construct($content = '')
91
    {
92 22
        $this->content($content);
93 22
    }
94
95
    /**
96
     * Set the HipChat room to send message to.
97
     *
98
     * @param int|string $room
99
     * @return $this
100
     */
101 1
    public function room($room)
102
    {
103 1
        $this->room = $room;
0 ignored issues
show
Documentation Bug introduced by
It seems like $room can also be of type integer. However, the property $room is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
104
105 1
        return $this;
106
    }
107
108
    /**
109
     * Indicate that the notification gives general information.
110
     *
111
     * @return $this
112
     */
113 1
    public function info()
114
    {
115 1
        $this->level = 'info';
116 1
        $this->color = 'gray';
117
118 1
        return $this;
119
    }
120
121
    /**
122
     * Indicate that the notification gives information about a successful operation.
123
     *
124
     * @return $this
125
     */
126 1
    public function success()
127
    {
128 1
        $this->level = 'success';
129 1
        $this->color = 'green';
130
131 1
        return $this;
132
    }
133
134
    /**
135
     * Indicate that the notification gives information about an error.
136
     *
137
     * @return $this
138
     */
139 2
    public function error()
140
    {
141 2
        $this->level = 'error';
142 2
        $this->color = 'red';
143
144 2
        return $this;
145
    }
146
147
    /**
148
     * Set the from label of the HipChat message.
149
     *
150
     * @param  string  $from
151
     * @return $this
152
     */
153 2
    public function from($from)
154
    {
155 2
        $this->from = trim($from);
156
157 2
        return $this;
158
    }
159
160
    /**
161
     * Set HTML format and optionally the content.
162
     *
163
     * @param string $content
164
     * @return $this
165
     */
166 3
    public function html($content = '')
167
    {
168 3
        $this->format = 'html';
169
170 3
        if (! empty($content)) {
171 2
            $this->content($content);
172
        }
173
174 3
        return $this;
175
    }
176
177
    /**
178
     * Set text format and optionally the content.
179
     *
180
     * @param string $content
181
     * @return $this
182
     */
183 2
    public function text($content = '')
184
    {
185 2
        $this->format = 'text';
186
187 2
        if (! empty($content)) {
188 1
            $this->content($content);
189
        }
190
191 2
        return $this;
192
    }
193
194
    /**
195
     * Should a message trigger a user notification in a HipChat client.
196
     *
197
     * @param  bool  $notify
198
     * @return $this
199
     */
200 2
    public function notify($notify = true)
201
    {
202 2
        $this->notify = $notify;
203
204 2
        return $this;
205
    }
206
207
    /**
208
     * Set the content of the message.
209
     * Allowed HTML tags: a, b, i, strong, em, br, img, pre, code, lists, tables.
210
     *
211
     * @param  string  $content
212
     * @return $this
213
     */
214 22
    public function content($content)
215
    {
216 22
        $this->content = trim($content);
217
218 22
        return $this;
219
    }
220
221
    /**
222
     * Set the color for the message.
223
     * Allowed values: yellow, green, red, purple, gray, random.
224
     *
225
     * @param $color
226
     * @return string
227
     */
228 1
    public function color($color)
229
    {
230 1
        $this->color = $color;
231
232 1
        return $this;
233
    }
234
235
    /**
236
     * Sets the Card.
237
     *
238
     * @param Card|\Closure|null $card
239
     * @return $this
240
     */
241 3 View Code Duplication
    public function card($card)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243 3
        if ($card instanceof Card) {
244 2
            $this->card = $card;
245
246 2
            return $this;
247
        }
248
249 1
        if ($card instanceof \Closure) {
250 1
            $card($new = new Card());
251 1
            $this->card = $new;
252
253 1
            return $this;
254
        }
255
256
        throw new \InvalidArgumentException('Invalid Card type. Expected '.Card::class.' or '.\Closure::class.'.');
257
    }
258
259
    /**
260
     * Sets the id of the "parent" message to attach this notification to.
261
     *
262
     * @param $id
263
     * @return $this
264
     */
265 2
    public function attachTo($id)
266
    {
267 2
        $this->attachTo = trim($id);
268
269 2
        return $this;
270
    }
271
272
    /**
273
     * Get an array representation of the HipChatMessage.
274
     *
275
     * @return array
276
     */
277 2
    public function toArray()
278
    {
279 2
        $message = array_filter([
280 2
            'from' => $this->from,
281 2
            'message_format' => $this->format,
282 2
            'color' => $this->color,
283 2
            'notify' => $this->notify,
284 2
            'message' => $this->content,
285 2
            'attach_to' => $this->attachTo,
286
        ]);
287
288 2
        if (! empty($this->card)) {
289 1
            $message['card'] = $this->card->toArray();
290
        }
291
292 2
        return $message;
293
    }
294
}
295