Completed
Push — master ( 5b0ae6...08f84c )
by Peter
07:37
created

HipChatMessage::attachTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
ccs 0
cts 0
cp 0
cc 1
eloc 3
nc 1
nop 1
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 1
63
    /**
64 1
     * 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 18
    public $attachTo;
73
74 18
    /**
75 18
     * Create a new instance of HipChatMessage.
76
     *
77
     * @param string $content
78
     * @return static
79
     */
80
    public static function create($content = '')
81
    {
82
        return new static($content);
83 1
    }
84
85 1
    /**
86
     * Create a new instance of HipChatMessage.
87 1
     *
88
     * @param $content
89
     */
90
    public function __construct($content = '')
91
    {
92
        $this->content($content);
93
    }
94
95 1
    /**
96
     * Set the HipChat room to send message to.
97 1
     *
98 1
     * @param int|string $room
99
     * @return $this
100 1
     */
101
    public function room($room)
102
    {
103
        $this->room = $room;
0 ignored issues
show
Documentation Bug introduced by Mohamed Said
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
        return $this;
106
    }
107
108 1
    /**
109
     * Indicate that the notification gives general information.
110 1
     *
111 1
     * @return $this
112
     */
113 1
    public function info()
114
    {
115
        $this->level = 'info';
116
        $this->color = 'gray';
117
118
        return $this;
119
    }
120
121 2
    /**
122
     * Indicate that the notification gives information about a successful operation.
123 2
     *
124 2
     * @return $this
125
     */
126 2
    public function success()
127
    {
128
        $this->level = 'success';
129
        $this->color = 'green';
130
131
        return $this;
132
    }
133
134
    /**
135 2
     * Indicate that the notification gives information about an error.
136
     *
137 2
     * @return $this
138
     */
139 2
    public function error()
140
    {
141
        $this->level = 'error';
142
        $this->color = 'red';
143
144
        return $this;
145
    }
146
147
    /**
148 3
     * Set the from label of the HipChat message.
149
     *
150 3
     * @param  string  $from
151
     * @return $this
152 3
     */
153 1
    public function from($from)
154
    {
155
        $this->from = trim($from);
156 3
157
        return $this;
158
    }
159
160
    /**
161
     * Set HTML format and optionally the content.
162
     *
163
     * @param string $content
164
     * @return $this
165 2
     */
166
    public function html($content = '')
167 2
    {
168
        $this->format = 'html';
169 2
170 1
        if (! empty($content)) {
171
            $this->content($content);
172
        }
173 2
174
        return $this;
175
    }
176
177
    /**
178
     * Set text format and optionally the content.
179
     *
180
     * @param string $content
181
     * @return $this
182 2
     */
183
    public function text($content = '')
184 2
    {
185
        $this->format = 'text';
186 2
187
        if (! empty($content)) {
188
            $this->content($content);
189
        }
190
191
        return $this;
192
    }
193
194
    /**
195
     * Should a message trigger a user notification in a HipChat client.
196 18
     *
197
     * @param  bool  $notify
198 18
     * @return $this
199
     */
200 18
    public function notify($notify = true)
201
    {
202
        $this->notify = $notify;
203
204
        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 1
     *
211
     * @param  string  $content
212 1
     * @return $this
213
     */
214 1
    public function content($content)
215
    {
216
        $this->content = trim($content);
217 1
218
        return $this;
219
    }
220 1
221 1
    /**
222 1
     * Set the color for the message.
223 1
     * Allowed values: yellow, green, red, purple, gray, random.
224 1
     *
225
     * @param $color
226
     * @return string
227
     */
228
    public function color($color)
229
    {
230
        $this->color = $color;
231
232
        return $this;
233
    }
234
235
    /**
236
     * Sets the Card.
237
     *
238
     * @param Card|\Closure|null $card
239
     * @return $this
240
     */
241 View Code Duplication
    public function card($card)
0 ignored issues
show
Duplication introduced by Peter Matseykanets
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
        if ($card instanceof Card) {
244
            $this->card = $card;
245
            return $this;
246
        }
247
248
        if ($card instanceof \Closure) {
249
            $card($new = new Card());
250
            $this->card = $new;
251
            return $this;
252
        }
253
254
        throw new \InvalidArgumentException('Invalid Card type. Expected '.Card::class.' or '.\Closure::class.'.');
255
    }
256
257
    /**
258
     * Sets the id of the "parent" message to attach this notification to.
259
     *
260
     * @param $id
261
     * @return $this
262
     */
263
    public function attachTo($id)
264
    {
265
        $this->attachTo = trim($id);
266
267
        return $this;
268
    }
269
270
    /**
271
     * Get an array representation of the HipChatMessage.
272
     *
273
     * @return array
274
     */
275
    public function toArray()
276
    {
277
        $message = array_filter([
278
            'from' => $this->from,
279
            'message_format' => $this->format,
280
            'color' => $this->color,
281
            'notify' => $this->notify,
282
            'message' => $this->content,
283
            'attach_to' => $this->attachTo,
284
        ]);
285
286
        if (! empty($this->card)) {
287
            $message['card'] = $this->card->toArray();
288
        }
289
290
        return $message;
291
    }
292
}
293