Completed
Pull Request — master (#17)
by
unknown
04:01
created

HipChatMessage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
     * The HipChat API token.
16
     *
17
     * @var string
18
     */
19
    public $token = null;
20
21
    /**
22
     * A label to be shown in addition to the sender's name.
23
     *
24
     * @var string
25
     */
26
    public $from = '';
27
28
    /**
29
     * The format of the notification (text, html).
30
     *
31
     * @var string
32
     */
33
    public $format = 'text';
34
35
    /**
36
     * Should a message trigger a user notification in a HipChat client.
37
     *
38
     * @var bool
39
     */
40
    public $notify = false;
41
42
    /**
43
     * The "level" of the notification (info, success, error).
44
     *
45
     * @var string
46
     */
47
    public $level = 'info';
48
49
    /**
50
     * The color of the notification (yellow, green, red, purple, gray, random).
51
     *
52
     * @var string
53
     */
54
    public $color = 'gray';
55
56
    /**
57
     * The text content of the message.
58
     *
59
     * @var string
60
     */
61
    public $content = '';
62
63
    /**
64
     * An instance of Card object.
65
     *
66
     * @var Card
67
     */
68
    public $card;
69
70
    /**
71
     * The message id to to attach this notification to, for example if this notification is
72
     * in response to a particular message. For supported clients, this will display the
73
     * notification in the context of the referenced message specified by attach_to parameter.
74
     * If this is not possible to attach the notification, it will be rendered as an unattached
75
     * notification. The message must be in the same room as that the notification is sent to.
76
     *
77
     * @var string
78
     */
79
    public $attachTo;
80
81
    /**
82
     * Create a new instance of HipChatMessage.
83
     *
84
     * @param string $content
85
     * @return static
86
     */
87 19
    public static function create($content = '')
88
    {
89 19
        return new static($content);
90
    }
91
92
    /**
93
     * Create a new instance of HipChatMessage.
94
     *
95
     * @param $content
96
     */
97 22
    public function __construct($content = '')
98
    {
99 22
        $this->content($content);
100 22
    }
101
102
    /**
103
     * Set the HipChat room to send message to.
104
     *
105
     * @param int|string $room
106
     * @return $this
107
     */
108 1
    public function room($room)
109
    {
110 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...
111
112 1
        return $this;
113
    }
114
115
    /**
116
     * Set the HipChat token to use for this message.
117
     *
118
     * @param string $token
119
     * @return $this
120
     */
121
    public function token($token)
122
    {
123
        $this->token = $token;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Indicate that the notification gives general information.
130
     *
131
     * @return $this
132
     */
133 1
    public function info()
134
    {
135 1
        $this->level = 'info';
136 1
        $this->color = 'gray';
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * Indicate that the notification gives information about a successful operation.
143
     *
144
     * @return $this
145
     */
146 1
    public function success()
147
    {
148 1
        $this->level = 'success';
149 1
        $this->color = 'green';
150
151 1
        return $this;
152
    }
153
154
    /**
155
     * Indicate that the notification gives information about an error.
156
     *
157
     * @return $this
158
     */
159 2
    public function error()
160
    {
161 2
        $this->level = 'error';
162 2
        $this->color = 'red';
163
164 2
        return $this;
165
    }
166
167
    /**
168
     * Set the from label of the HipChat message.
169
     *
170
     * @param  string  $from
171
     * @return $this
172
     */
173 2
    public function from($from)
174
    {
175 2
        $this->from = trim($from);
176
177 2
        return $this;
178
    }
179
180
    /**
181
     * Set HTML format and optionally the content.
182
     *
183
     * @param string $content
184
     * @return $this
185
     */
186 3
    public function html($content = '')
187
    {
188 3
        $this->format = 'html';
189
190 3
        if (! empty($content)) {
191 2
            $this->content($content);
192 2
        }
193
194 3
        return $this;
195
    }
196
197
    /**
198
     * Set text format and optionally the content.
199
     *
200
     * @param string $content
201
     * @return $this
202
     */
203 2
    public function text($content = '')
204
    {
205 2
        $this->format = 'text';
206
207 2
        if (! empty($content)) {
208 1
            $this->content($content);
209 1
        }
210
211 2
        return $this;
212
    }
213
214
    /**
215
     * Should a message trigger a user notification in a HipChat client.
216
     *
217
     * @param  bool  $notify
218
     * @return $this
219
     */
220 2
    public function notify($notify = true)
221
    {
222 2
        $this->notify = $notify;
223
224 2
        return $this;
225
    }
226
227
    /**
228
     * Set the content of the message.
229
     * Allowed HTML tags: a, b, i, strong, em, br, img, pre, code, lists, tables.
230
     *
231
     * @param  string  $content
232
     * @return $this
233
     */
234 22
    public function content($content)
235
    {
236 22
        $this->content = trim($content);
237
238 22
        return $this;
239
    }
240
241
    /**
242
     * Set the color for the message.
243
     * Allowed values: yellow, green, red, purple, gray, random.
244
     *
245
     * @param $color
246
     * @return string
247
     */
248 1
    public function color($color)
249
    {
250 1
        $this->color = $color;
251
252 1
        return $this;
253
    }
254
255
    /**
256
     * Sets the Card.
257
     *
258
     * @param Card|\Closure|null $card
259
     * @return $this
260
     */
261 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...
262
    {
263 3
        if ($card instanceof Card) {
264 2
            $this->card = $card;
265
266 2
            return $this;
267
        }
268
269 1
        if ($card instanceof \Closure) {
270 1
            $card($new = new Card());
271 1
            $this->card = $new;
272
273 1
            return $this;
274
        }
275
276
        throw new \InvalidArgumentException('Invalid Card type. Expected '.Card::class.' or '.\Closure::class.'.');
277
    }
278
279
    /**
280
     * Sets the id of the "parent" message to attach this notification to.
281
     *
282
     * @param $id
283
     * @return $this
284
     */
285 2
    public function attachTo($id)
286
    {
287 2
        $this->attachTo = trim($id);
288
289 2
        return $this;
290
    }
291
292
    /**
293
     * Get an array representation of the HipChatMessage.
294
     *
295
     * @return array
296
     */
297 2
    public function toArray()
298
    {
299 2
        $message = array_filter([
300 2
            'from' => $this->from,
301 2
            'message_format' => $this->format,
302 2
            'color' => $this->color,
303 2
            'notify' => $this->notify,
304 2
            'message' => $this->content,
305 2
            'attach_to' => $this->attachTo,
306 2
        ]);
307
308 2
        if (! empty($this->card)) {
309 1
            $message['card'] = $this->card->toArray();
310 1
        }
311
312 2
        return $message;
313
    }
314
}
315