Completed
Pull Request — master (#13)
by Abdullah
03:31
created

ZendeskMessage::ticket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 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 1
     *
51
     * @return static
52 1
     */
53
    public static function create($subject = '', $description = '')
54
    {
55
        return new static($subject, $description);
56
    }
57
58 6
    /**
59
     * @param string $subject
60 6
     */
61 6
    public function __construct($subject = '', $description = '')
62 6
    {
63 6
        $this->subject = $subject;
64
        $this->description = $description;
65
        $this->content = $description;
66
    }
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 2
     *
88
     * @return $this
89 2
     */
90 2
    public function from($name, $email)
91 2
    {
92
        $this->requester = [
93
            'name' => $name,
94 2
            'email' => $email,
95
        ];
96
97
        return $this;
98
    }
99
100
    /**
101
     * Set the content message.
102
     *
103
     * @param $content
104 2
     *
105
     * @return $this
106 2
     */
107
    public function content($content)
108 2
    {
109
        $this->content = $content;
110
111
        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 2
     *
133
     * @return $this
134 2
     */
135
    public function description($description)
136 2
    {
137
        $this->description = $description;
138
139
        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
    public function type($type)
151
    {
152
        if (! in_array($type, ['problem', 'incident', 'question', 'task'])) {
153
            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
    public function priority($priority)
169
    {
170
        if (! in_array($priority, ['urgent', 'high', 'normal', 'low'])) {
171
            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
    public function status($status)
185
    {
186
        if (! in_array($status, ['new', 'open', 'pending', 'hold', 'solved', 'closed'])) {
187
            throw CouldNotCreateMessage::invalidStatus($status);
188
        }
189
        $this->status = $status;
190
191
        return $this;
192
    }
193 1
194
    /**
195 1
     * Set the message to be public.
196
     *
197 1
     * @return $this
198
     */
199
    public function visible()
200
    {
201
        $this->isPublic = true;
202
203
        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(array $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
     * @param string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
243
     *
244
     * @return $this
245
     */
246
    public function group($id)
247
    {
248
        $this->groupId = $id;
249
250
        return $this;
251
    }
252
253
    /**
254 6
     * Set the ticket id. If it set the system will update
255
     * the ticket rather than crate a new ticket.
256 6
     *
257
     * @param int $id
258 6
     * @param string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
259
     *
260
     * @return $this
261
     */
262 6
    public function ticket($id)
263 2
    {
264
        $this->ticket = $id;
265
266 6
        return $this;
267
    }
268 6
269
    /**
270
     * Return the comment array.
271
     *
272
     * @see https://developer.zendesk.com/rest_api/docs/core/ticket_audits#audit-events Documentation of ticket comment.
273
     *
274 6
     * @return array
275
     */
276
    public function getComment()
277 6
    {
278 6
        $comment = [];
279 6
280 6
        if ($this->htmlContent !== '') {
281 6
            $comment['html_body'] = $this->htmlContent;
282 6
        }
283 6
284 6
        if ($this->content !== '') {
285 6
            $comment['body'] = $this->content;
286 6
        }
287
288
        $comment['public'] = $this->isPublic;
289
290
        return $comment;
291
    }
292
293
    /**
294
     * @return array
295
     */
296
    public function toArray()
297
    {
298
        return [
299
            'subject' => $this->subject,
300
            'comment' => $this->getComment(),
301
            'requester' => $this->requester,
302
            'description' => $this->description,
303
            'type' => $this->type,
304
            'status' => $this->status,
305
            'tags' => $this->tags,
306
            'priority' => $this->priority,
307
            'custom_fields' => $this->customFields,
308
            'group_id' => $this->groupId,
309
            'ticket' => $this->ticket,
310
        ];
311
    }
312
}
313