Completed
Pull Request — master (#13)
by Abdullah
03:58
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
     *
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(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
     *
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 crate a new ticket.
255
     *
256
     * @param int $id
257
     * @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...
258
     *
259
     * @return $this
260
     */
261
    public function ticket($id)
262 1
    {
263
        $this->ticket = $id;
264 1
265
        return $this;
266 1
    }
267
268
    /**
269
     * Return the comment array.
270
     *
271
     * @see https://developer.zendesk.com/rest_api/docs/core/ticket_audits#audit-events Documentation of ticket comment.
272
     *
273
     * @return array
274
     */
275
    public function getComment()
276 7
    {
277
        $comment = [];
278 7
279
        if ($this->htmlContent !== '') {
280 7
            $comment['html_body'] = $this->htmlContent;
281
        }
282
283
        if ($this->content !== '') {
284 7
            $comment['body'] = $this->content;
285 3
        }
286 3
287
        $comment['public'] = $this->isPublic;
288 7
289
        return $comment;
290 7
    }
291
292
    /**
293
     * @return array
294
     */
295
    public function toArray()
296 7
    {
297
        return [
298
            'subject' => $this->subject,
299 7
            'comment' => $this->getComment(),
300 7
            'requester' => $this->requester,
301 7
            'description' => $this->description,
302 7
            'type' => $this->type,
303 7
            'status' => $this->status,
304 7
            'tags' => $this->tags,
305 7
            'priority' => $this->priority,
306 7
            'custom_fields' => $this->customFields,
307 7
            'group_id' => $this->groupId,
308 7
            'ticket' => $this->ticket,
309 7
        ];
310 7
    }
311
}
312