Completed
Push — master ( 5f7502...a1b61a )
by Peter
03:44
created

HipChatMessage::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NotificationChannels\HipChat;
4
5
class HipChatMessage
6
{
7
    /**
8
     * HipChat room.
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
     * Create a new instance of HipChatMessage.
58
     *
59
     * @param string $content
60
     * @return static
61
     */
62 1
    public static function create($content = '')
63
    {
64 1
        return new static($content);
65
    }
66
67
    /**
68
     * Create a new instance of HipChatMessage.
69
     *
70
     * @param $content
71
     */
72 18
    public function __construct($content = '')
73
    {
74 18
        $this->content($content);
75 18
    }
76
77
    /**
78
     * Set the HipChat room to send message to.
79
     *
80
     * @param int|string $room
81
     * @return $this
82
     */
83 1
    public function room($room)
84
    {
85 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...
86
87 1
        return $this;
88
    }
89
90
    /**
91
     * Indicate that the notification gives general information.
92
     *
93
     * @return $this
94
     */
95 1
    public function info()
96
    {
97 1
        $this->level = 'info';
98 1
        $this->color = 'gray';
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * Indicate that the notification gives information about a successful operation.
105
     *
106
     * @return $this
107
     */
108 1
    public function success()
109
    {
110 1
        $this->level = 'success';
111 1
        $this->color = 'green';
112
113 1
        return $this;
114
    }
115
116
    /**
117
     * Indicate that the notification gives information about an error.
118
     *
119
     * @return $this
120
     */
121 2
    public function error()
122
    {
123 2
        $this->level = 'error';
124 2
        $this->color = 'red';
125
126 2
        return $this;
127
    }
128
129
    /**
130
     * Set the from label of the HipChat message.
131
     *
132
     * @param  string  $from
133
     * @return $this
134
     */
135 2
    public function from($from)
136
    {
137 2
        $this->from = trim($from);
138
139 2
        return $this;
140
    }
141
142
    /**
143
     * Set HTML format and optionally the content.
144
     *
145
     * @param string $content
146
     * @return $this
147
     */
148 3
    public function html($content = '')
149
    {
150 3
        $this->format = 'html';
151
152 3
        if (! empty($content)) {
153 1
            $this->content($content);
154
        }
155
156 3
        return $this;
157
    }
158
159
    /**
160
     * Set text format and optionally the content.
161
     *
162
     * @param string $content
163
     * @return $this
164
     */
165 2
    public function text($content = '')
166
    {
167 2
        $this->format = 'text';
168
169 2
        if (! empty($content)) {
170 1
            $this->content($content);
171
        }
172
173 2
        return $this;
174
    }
175
176
    /**
177
     * Should a message trigger a user notification in a HipChat client.
178
     *
179
     * @param  bool  $notify
180
     * @return $this
181
     */
182 2
    public function notify($notify = true)
183
    {
184 2
        $this->notify = $notify;
185
186 2
        return $this;
187
    }
188
189
    /**
190
     * Set the content of the message.
191
     * Allowed HTML tags: a, b, i, strong, em, br, img, pre, code, lists, tables.
192
     *
193
     * @param  string  $content
194
     * @return $this
195
     */
196 18
    public function content($content)
197
    {
198 18
        $this->content = trim($content);
199
200 18
        return $this;
201
    }
202
203
    /**
204
     * Set the color for the message.
205
     * Allowed values: yellow, green, red, purple, gray, random.
206
     *
207
     * @param $color
208
     * @return string
209
     */
210 1
    public function color($color)
211
    {
212 1
        $this->color = $color;
213
214 1
        return $this;
215
    }
216
217 1
    public function toArray()
218
    {
219
        return [
220 1
            'from' => $this->from,
221 1
            'message_format' => $this->format,
222 1
            'color' => $this->color,
223 1
            'notify' => $this->notify,
224 1
            'message' => $this->content,
225
        ];
226
    }
227
}
228