Completed
Push — master ( f665df...a43901 )
by KwangSeob
02:15
created

IssueField::setDueDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JiraRestApi\Issue;
4
5
use JiraRestApi\ClassSerialize;
6
7
class IssueField implements \JsonSerializable
8
{
9
    use ClassSerialize;
10
11
    /** @var string */
12
    public $summary;
13
14
    /** @var array */
15
    public $progress;
16
17
    /** @var \JiraRestApi\Issue\TimeTracking */
18
    public $timeTracking;
19
20
    /** @var \JiraRestApi\Issue\IssueType */
21
    public $issuetype;
22
23
    /** @var Reporter|null */
24
    public $reporter;
25
26
    /** @var \DateTime */
27
    public $created;
28
29
    /** @var \DateTime */
30
    public $updated;
31
32
    /** @var string|null */
33
    public $description;
34
35
    /** @var \JiraRestApi\Issue\Priority|null */
36
    public $priority;
37
38
    /** @var \JiraRestApi\Issue\IssueStatus */
39
    public $status;
40
41
    /** @var array */
42
    public $labels;
43
44
    /** @var \JiraRestApi\Project\Project */
45
    public $project;
46
47
    /** @var string|null */
48
    public $environment;
49
50
    /** @var \JiraRestApi\Issue\Component[] */
51
    public $components;
52
53
    /** @var \JiraRestApi\Issue\Comments */
54
    public $comment;
55
56
    /** @var object */
57
    public $votes;
58
59
    /** @var object|null */
60
    public $resolution;
61
62
    /** @var array */
63
    public $fixVersions;
64
65
    /** @var \JiraRestApi\Issue\Reporter|null */
66
    public $creator;
67
68
    /** @var object|null */
69
    public $watches;
70
71
    /** @var object|null */
72
    public $worklog;
73
74
    /** @var \JiraRestApi\Issue\Reporter|null */
75
    public $assignee;
76
77
    /** @var \JiraRestApi\Issue\Version[] */
78
    public $versions;
79
80
    /** @var \JiraRestApi\Issue\Attachment[] */
81
    public $attachment;
82
83
    /** @var string|null */
84
    public $aggregatetimespent;
85
86
    /** @var string|null */
87
    public $timeestimate;
88
89
    /** @var string|null */
90
    public $aggregatetimeoriginalestimate;
91
92
    /** @var string|null */
93
    public $resolutiondate;
94
95
    /** @var \DateTime|null */
96
    public $duedate;
97
98
    /** @var array */
99
    public $issuelinks;
100
101
    /** @var \JiraRestApi\Issue\Issue[] */
102
    public $subtasks;
103
104
    /** @var int */
105
    public $workratio;
106
107
    /** @var object|null */
108
    public $aggregatetimeestimate;
109
110
    /** @var object|null */
111
    public $aggregateprogress;
112
113
    /** @var object|null */
114
    public $lastViewed;
115
116
    /** @var object|null */
117
    public $timeoriginalestimate;
118
119
    /** @var object|null */
120
    public $parent;
121
122
    /** @var array|null */
123
    public $customFields;
124
125
    /** @var \JiraRestApi\Issue\SecurityScheme|null */
126
    public $security;
127
128
    public function __construct($updateIssue = false)
129
    {
130
        if ($updateIssue != true) {
131
            $this->project = new \JiraRestApi\Project\Project();
132
133
            $this->assignee = new Reporter();
134
            // priority maybe empty.
135
            //$this->priority = new Priority();
136
137
            $this->issuetype = new IssueType();
138
        }
139
    }
140
141
    public function jsonSerialize()
142
    {
143
        $vars = array_filter(get_object_vars($this), function ($var) {
144
            return !is_null($var);
145
        });
146
147
        // if assignee property has empty value then remove it.
148
        // @see https://github.com/lesstif/php-jira-rest-client/issues/126
149
        // @see https://github.com/lesstif/php-jira-rest-client/issues/177
150
        if (! empty($this->assignee)) {
151
            // do nothing
152
            if ($this->assignee->isWantUnassigned() === true ) {
153
154
            } elseif ($this->assignee->isEmpty()) {
155
                unset($vars['assignee']);
156
            }
157
        }
158
159
        // clear undefined json property
160
        unset($vars['customFields']);
161
162
        // repackaging custom field
163
        if (!empty($this->customFields)) {
164
            foreach ($this->customFields as $key => $value) {
165
                $vars[$key] = $value;
166
            }
167
        }
168
169
        return $vars;
170
    }
171
172
    public function getCustomFields()
173
    {
174
        return $this->customFields;
175
    }
176
177
    public function addCustomField($key, $value)
178
    {
179
        $this->customFields[$key] = $value;
180
181
        return $this;
182
    }
183
184
    public function getProjectKey()
185
    {
186
        return $this->project->key;
187
    }
188
189
    public function getProjectId()
190
    {
191
        return $this->project->id;
192
    }
193
194
    public function setProjectKey($key)
195
    {
196
        $this->project->key = $key;
197
198
        return $this;
199
    }
200
201
    public function setProjectId($id)
202
    {
203
        $this->project->id = $id;
204
205
        return $this;
206
    }
207
208
    public function setSummary($summary)
209
    {
210
        $this->summary = $summary;
211
212
        return $this;
213
    }
214
215
    /**
216
     * set issue reporter name.
217
     *
218
     * @param string $name
219
     *
220
     * @return $this
221
     */
222
    public function setReporterName($name)
223
    {
224
        if (is_null($this->reporter)) {
225
            $this->reporter = new Reporter();
226
        }
227
228
        $this->reporter->name = $name;
229
230
        return $this;
231
    }
232
233
    /**
234
     * set issue assignee name.
235
     *
236
     * @param string $name
237
     *
238
     * @return $this
239
     */
240
    public function setAssigneeName($name)
241
    {
242
        if (is_null($this->assignee)) {
243
            $this->assignee = new Reporter();
244
        }
245
246
        $this->assignee->name = $name;
247
248
        return $this;
249
    }
250
251
    /**
252
     * set issue priority name.
253
     *
254
     * @param string $name
255
     *
256
     * @return $this
257
     */
258
    public function setPriorityName($name)
259
    {
260
        if (is_null($this->priority)) {
261
            $this->priority = new Priority();
262
        }
263
264
        $this->priority->name = $name;
265
266
        return $this;
267
    }
268
269
    /**
270
     * @param string|null $description
271
     *
272
     * @return $this
273
     */
274
    public function setDescription($description)
275
    {
276
        if (!empty($description)) {
277
            $this->description = $description;
278
        }
279
280
        return $this;
281
    }
282
283
    /**
284
     * add a Affects version.
285
     *
286
     * @param mixed string or array $name
287
     *
288
     * @return $this
289
     */
290
    public function addVersion($name)
291
    {
292
        if (is_null($this->versions)) {
0 ignored issues
show
introduced by
The condition is_null($this->versions) is always false.
Loading history...
293
            $this->versions = [];
294
        }
295
296
        if (is_string($name)) {
297
            array_push($this->versions, new Version($name));
298
        } elseif (is_array($name)) {
299
            foreach ($name as $v) {
300
                array_push($this->versions, new Version($v));
301
            }
302
        }
303
304
        return $this;
305
    }
306
307
    /**
308
     * add issue label.
309
     *
310
     * @param string $label
311
     *
312
     * @return $this
313
     */
314
    public function addLabel($label)
315
    {
316
        if (is_null($this->labels)) {
0 ignored issues
show
introduced by
The condition is_null($this->labels) is always false.
Loading history...
317
            $this->labels = [];
318
        }
319
320
        array_push($this->labels, $label);
321
322
        return $this;
323
    }
324
325
    /**
326
     * set issue type.
327
     *
328
     * @param mixed IssueType or string $name
329
     *
330
     * @return $this
331
     */
332
    public function setIssueType($name)
333
    {
334
        if (is_string($name)) {
335
            if (is_null($this->issuetype)) {
336
                $this->issuetype = new IssueType();
337
            }
338
339
            $this->issuetype->name = $name;
340
        } else {
341
            $this->issuetype = $name;
342
        }
343
344
        return $this;
345
    }
346
347
    public function getIssueType()
348
    {
349
        return $this->issuetype;
350
    }
351
352
    /**
353
     *  set parent issue.
354
     *
355
     * @param string $keyOrId
356
     */
357
    public function setParentKeyOrId($keyOrId)
358
    {
359
        if (is_numeric($keyOrId)) {
360
            $this->parent['id'] = $keyOrId;
361
        } elseif (is_string($keyOrId)) {
0 ignored issues
show
introduced by
The condition is_string($keyOrId) is always true.
Loading history...
362
            $this->parent['key'] = $keyOrId;
363
        }
364
    }
365
366
    public function setParent(Issue $parent)
367
    {
368
        $this->parent = $parent;
369
    }
370
371
    /**
372
     * add issue component.
373
     *
374
     * @param mixed string or array $component
375
     *
376
     * @return $this
377
     */
378
    public function addComponents($component)
379
    {
380
        if (is_null($this->components)) {
0 ignored issues
show
introduced by
The condition is_null($this->components) is always false.
Loading history...
381
            $this->components = [];
382
        }
383
384
        if (is_string($component)) {
385
            array_push($this->components, new Component($component));
386
        } elseif (is_array($component)) {
387
            foreach ($component as $c) {
388
                array_push($this->components, new Component($c));
389
            }
390
        }
391
392
        return $this;
393
    }
394
395
    /**
396
     * set security level.
397
     *
398
     * @param int $id issue's security id
399
     *
400
     * @return $this
401
     */
402
    public function setSecurityId($id)
403
    {
404
        if (is_empty($this->security)) {
0 ignored issues
show
Bug introduced by
The function is_empty was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

404
        if (/** @scrutinizer ignore-call */ is_empty($this->security)) {
Loading history...
405
            $this->security = new SecurityScheme();
406
        }
407
408
        $this->security->id = $id;
409
410
        return $this;
411
    }
412
413
    /**
414
     * set issue's due date.
415
     *
416
     * @param \DateTime|null $duedate due date string or DateTime object
417
     * @param string         $format  datetime string format.
418
     *
419
     * @return $this
420
     */
421
    public function setDueDate($duedate, $format = 'Y-m-d')
422
    {
423
        if (is_string($duedate)) {
0 ignored issues
show
introduced by
The condition is_string($duedate) is always false.
Loading history...
424
            $this->duedate = $duedate;
425
        } elseif ($duedate instanceof \DateTime) {
426
            $this->duedate = $duedate->format($format);
0 ignored issues
show
Documentation Bug introduced by
It seems like $duedate->format($format) of type string is incompatible with the declared type null|DateTime of property $duedate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
427
        } else {
428
            $this->duedate = null;
429
        }
430
431
        return $this;
432
    }
433
434
    /**
435
     * set Assignee to Unassigned
436
     *
437
     * @see https://confluence.atlassian.com/jirakb/how-to-set-assignee-to-unassigned-via-rest-api-in-jira-744721880.html
438
     */
439
    public function setAssigneeToUnassigned()
440
    {
441
        if (is_null($this->assignee)) {
442
            $this->assignee = new Reporter();
443
        }
444
445
        $this->assignee->setWantUnassigned(true);
446
447
        return $this;
448
    }
449
450
    public function setAssigneeToDefault()
451
    {
452
        if (is_null($this->assignee)) {
453
            $this->assignee = new Reporter();
454
        }
455
456
        $this->assignee->name = "-1";
457
458
        return $this;
459
    }
460
}
461