ZendeskMessage   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 304
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 71.05%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 304
ccs 54
cts 76
cp 0.7105
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 6 1
A subject() 0 6 1
A from() 0 9 1
A content() 0 6 1
A htmlContent() 0 6 1
A description() 0 6 1
A type() 0 9 2
A priority() 0 9 2
A status() 0 9 2
A visible() 0 6 1
A tag() 0 6 1
A customField() 0 9 1
A group() 0 6 1
A ticket() 0 6 1
A getComment() 0 16 3
A toArray() 0 16 1
1
<?php
2
3
namespace NotificationChannels\Zendesk;
4
5
use NotificationChannels\Zendesk\Exceptions\CouldNotCreateMessage;
6
7
class ZendeskMessage
8
{
9
    /** @var int|null */
10
    protected $ticket;
11
12
    /** @var string */
13
    protected $subject;
14
15
    /** @var array */
16
    protected $requester = [];
17
18
    /** @var string */
19
    protected $description = '';
20
21
    /** @var string */
22
    protected $type;
23
24
    /** @var string */
25
    protected $status = 'new';
26
27
    /** @var array */
28
    protected $tags = [];
29
30
    /** @var string */
31
    protected $content = '';
32
33
    /** @var string */
34
    protected $htmlContent = '';
35
36
    /** @var bool */
37
    protected $isPublic = false;
38
39
    /** @var string */
40
    protected $priority = 'normal';
41
42
    /** @var array */
43
    protected $customFields = [];
44
45
    /** @var int */
46
    protected $groupId = '';
47
48
    /**
49
     * @param string $subject
50
     *
51
     * @return static
52
     */
53 1
    public static function create($subject = '', $description = '')
54
    {
55 1
        return new static($subject, $description);
56
    }
57
58
    /**
59
     * @param string $subject
60
     */
61 10
    public function __construct($subject = '', $description = '')
62
    {
63 10
        $this->subject = $subject;
64 10
        $this->description = $description;
65 10
        $this->content = $description;
66 10
    }
67
68
    /**
69
     * Set the ticket subject.
70
     *
71
     * @param $subject
72
     *
73
     * @return $this
74
     */
75
    public function subject($subject)
76
    {
77
        $this->subject = $subject;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Set the ticket customer name.
84
     *
85
     * @param string $name
86
     * @param string $email
87
     *
88
     * @return $this
89
     */
90 2
    public function from($name, $email)
91
    {
92 2
        $this->requester = [
93 2
            'name' => $name,
94 2
            'email' => $email,
95
        ];
96
97 2
        return $this;
98
    }
99
100
    /**
101
     * Set the content message.
102
     *
103
     * @param $content
104
     *
105
     * @return $this
106
     */
107 3
    public function content($content)
108
    {
109 3
        $this->content = $content;
110
111 3
        return $this;
112
    }
113
114
    /**
115
     * Set the HTML content of message.
116
     *
117
     * @param string $html
118
     *
119
     * @return $this
120
     */
121
    public function htmlContent($html)
122
    {
123
        $this->htmlContent = $html;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Set the description.
130
     *
131
     * @param string $description
132
     *
133
     * @return $this
134
     */
135 2
    public function description($description)
136
    {
137 2
        $this->description = $description;
138
139 2
        return $this;
140
    }
141
142
    /**
143
     * Set the ticket type.
144
     * Allowed values are problem, incident, question, or task.
145
     *
146
     * @param string $type
147
     *
148
     * @return $this
149
     */
150 1
    public function type($type)
151
    {
152 1
        if (! in_array($type, ['problem', 'incident', 'question', 'task'])) {
153 1
            throw CouldNotCreateMessage::invalidType($type);
154
        }
155
        $this->type = $type;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Set the ticket priority.
162
     * Allowed values are urgent, high, normal, or low.
163
     *
164
     * @param string $priority
165
     *
166
     * @return $this
167
     */
168 1
    public function priority($priority)
169
    {
170 1
        if (! in_array($priority, ['urgent', 'high', 'normal', 'low'])) {
171 1
            throw CouldNotCreateMessage::invalidPriority($priority);
172
        }
173
        $this->priority = $priority;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Set the ticket status.
180
     * Allowed values are new, open, pending, hold, solved or closed.
181
     *
182
     * @return $this
183
     */
184 2
    public function status($status)
185
    {
186 2
        if (! in_array($status, ['new', 'open', 'pending', 'hold', 'solved', 'closed'])) {
187 1
            throw CouldNotCreateMessage::invalidStatus($status);
188
        }
189 1
        $this->status = $status;
190
191 1
        return $this;
192
    }
193
194
    /**
195
     * Set the message to be public.
196
     *
197
     * @return $this
198
     */
199 2
    public function visible()
200
    {
201 2
        $this->isPublic = true;
202
203 2
        return $this;
204
    }
205
206
    /**
207
     * Add a tag to the ticket.
208
     *
209
     * @param string $tag
210
     *
211
     * @return $this
212
     */
213
    public function tag($tag)
214
    {
215
        $this->tags[] = $tag;
216
217
        return $this;
218
    }
219
220
    /**
221
     * Set the value of custom field in the new ticket.
222
     *
223
     * @param int $id
224
     * @param string $value
225
     *
226
     * @return $this
227
     */
228
    public function customField($id, $value)
229
    {
230
        $this->customFields[] = [
231
            'id' => $id,
232
            'value' => $value,
233
        ];
234
235
        return $this;
236
    }
237
238
    /**
239
     * Set the value of group id.
240
     *
241
     * @param int $id
242
     *
243
     * @return $this
244
     */
245
    public function group($id)
246
    {
247
        $this->groupId = $id;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Set the ticket id. If it set the system will update
254
     * the ticket rather than create a new ticket.
255
     *
256
     * @param int $id
257
     *
258
     * @return $this
259
     */
260 1
    public function ticket($id)
261
    {
262 1
        $this->ticket = $id;
263
264 1
        return $this;
265
    }
266
267
    /**
268
     * Return the comment array.
269
     *
270
     * @see https://developer.zendesk.com/rest_api/docs/core/ticket_audits#audit-events Documentation of ticket comment.
271
     *
272
     * @return array
273
     */
274 7
    public function getComment()
275
    {
276 7
        $comment = [];
277
278 7
        if ($this->htmlContent !== '') {
279
            $comment['html_body'] = $this->htmlContent;
280
        }
281
282 7
        if ($this->content !== '') {
283 3
            $comment['body'] = $this->content;
284
        }
285
286 7
        $comment['public'] = $this->isPublic;
287
288 7
        return $comment;
289
    }
290
291
    /**
292
     * @return array
293
     */
294 7
    public function toArray()
295
    {
296
        return [
297 7
            'subject' => $this->subject,
298 7
            'comment' => $this->getComment(),
299 7
            'requester' => $this->requester,
300 7
            'description' => $this->description,
301 7
            'type' => $this->type,
302 7
            'status' => $this->status,
303 7
            'tags' => $this->tags,
304 7
            'priority' => $this->priority,
305 7
            'custom_fields' => $this->customFields,
306 7
            'group_id' => $this->groupId,
307 7
            'ticket' => $this->ticket,
308
        ];
309
    }
310
}
311