Completed
Push — master ( c05eee...8675c0 )
by KwangSeob
15s queued 11s
created

IssueField::setAssigneeAccountId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 13
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 \DateTimeInterface */
27
    public $created;
28
29
    /** @var \DateTimeInterface */
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 \DateTimeInterface|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
            } elseif ($this->assignee->isEmpty()) {
154
                unset($vars['assignee']);
155
            }
156
        }
157
158
        // clear undefined json property
159
        unset($vars['customFields']);
160
161
        // repackaging custom field
162
        if (!empty($this->customFields)) {
163
            foreach ($this->customFields as $key => $value) {
164
                $vars[$key] = $value;
165
            }
166
        }
167
168
        return $vars;
169
    }
170
171
    public function getCustomFields()
172
    {
173
        return $this->customFields;
174
    }
175
176
    public function addCustomField($key, $value)
177
    {
178
        $this->customFields[$key] = $value;
179
180
        return $this;
181
    }
182
183
    public function getProjectKey()
184
    {
185
        return $this->project->key;
186
    }
187
188
    public function getProjectId()
189
    {
190
        return $this->project->id;
191
    }
192
193
    public function setProjectKey($key)
194
    {
195
        $this->project->key = $key;
196
197
        return $this;
198
    }
199
200
    public function setProjectId($id)
201
    {
202
        $this->project->id = $id;
203
204
        return $this;
205
    }
206
207
    public function setSummary($summary)
208
    {
209
        $this->summary = $summary;
210
211
        return $this;
212
    }
213
214
    /**
215
     * set issue reporter name.
216
     *
217
     * @param string $name
218
     *
219
     * @return $this
220
     */
221
    public function setReporterName($name)
222
    {
223
        if (is_null($this->reporter)) {
224
            $this->reporter = new Reporter();
225
        }
226
227
        $this->reporter->name = $name;
228
229
        return $this;
230
    }
231
232
    /**
233
     * set issue reporter accountId.
234
     *
235
     * @param string $accountId
236
     *
237
     * @return $this
238
     */
239
    public function setReporterAccountId($accountId)
240
    {
241
        if (is_null($this->reporter)) {
242
            $this->reporter = new Reporter();
243
        }
244
245
        $this->reporter->accountId = $accountId;
246
247
        return $this;
248
    }
249
250
    /**
251
     * set issue assignee name.
252
     *
253
     * @param string $name
254
     *
255
     * @return $this
256
     */
257
    public function setAssigneeName($name)
258
    {
259
        if (is_null($this->assignee)) {
260
            $this->assignee = new Reporter();
261
        }
262
263
        $this->assignee->name = $name;
264
265
        return $this;
266
    }
267
268
    /**
269
     * set issue assignee accountId.
270
     *
271
     * @param string $accountId
272
     *
273
     * @return $this
274
     */
275
    public function setAssigneeAccountId($accountId)
276
    {
277
        if (is_null($this->assignee)) {
278
            $this->assignee = new Reporter();
279
        }
280
281
        $this->assignee->accountId = $accountId;
282
283
        // REST API V3 must name field set to null.
284
        $this->assignee->name = null;
285
        $this->assignee->setWantUnassigned(true);
286
287
        return $this;
288
    }
289
290
    /**
291
     * set issue priority name.
292
     *
293
     * @param string $name
294
     *
295
     * @return $this
296
     */
297
    public function setPriorityName($name)
298
    {
299
        if (is_null($this->priority)) {
300
            $this->priority = new Priority();
301
        }
302
303
        $this->priority->name = $name;
304
305
        return $this;
306
    }
307
308
    /**
309
     * set issue description.
310
     *
311
     * REST API V3 must use addDescriptionXXXX
312
     *
313
     * @see \JiraRestApi\Issue\IssueFieldV3::addDescriptionHeading
314
     * @see \JiraRestApi\Issue\IssueFieldV3::addDescriptionParagraph
315
     *
316
     * @param string|null $description
317
     *
318
     * @return $this
319
     */
320
    public function setDescription($description)
321
    {
322
        if (!empty($description)) {
323
            $this->description = $description;
324
        }
325
326
        return $this;
327
    }
328
329
    /**
330
     * add a Affects version.
331
     *
332
     * @param mixed string or array $name
333
     *
334
     * @return $this
335
     */
336
    public function addVersion($name)
337
    {
338
        if (is_null($this->versions)) {
0 ignored issues
show
introduced by
The condition is_null($this->versions) is always false.
Loading history...
339
            $this->versions = [];
340
        }
341
342
        if (is_string($name)) {
343
            array_push($this->versions, new Version($name));
344
        } elseif (is_array($name)) {
345
            foreach ($name as $v) {
346
                array_push($this->versions, new Version($v));
347
            }
348
        }
349
350
        return $this;
351
    }
352
353
    /**
354
     * add issue label.
355
     *
356
     * @param string $label
357
     *
358
     * @return $this
359
     */
360
    public function addLabel($label)
361
    {
362
        if (is_null($this->labels)) {
0 ignored issues
show
introduced by
The condition is_null($this->labels) is always false.
Loading history...
363
            $this->labels = [];
364
        }
365
366
        array_push($this->labels, $label);
367
368
        return $this;
369
    }
370
371
    /**
372
     * set issue type.
373
     *
374
     * @param mixed IssueType or string $name
375
     *
376
     * @return $this
377
     */
378
    public function setIssueType($name)
379
    {
380
        if (is_string($name)) {
381
            if (is_null($this->issuetype)) {
382
                $this->issuetype = new IssueType();
383
            }
384
385
            $this->issuetype->name = $name;
386
        } else {
387
            $this->issuetype = $name;
388
        }
389
390
        return $this;
391
    }
392
393
    public function getIssueType()
394
    {
395
        return $this->issuetype;
396
    }
397
398
    /**
399
     *  set parent issue.
400
     *
401
     * @param string $keyOrId
402
     */
403
    public function setParentKeyOrId($keyOrId)
404
    {
405
        if (is_numeric($keyOrId)) {
406
            $this->parent['id'] = $keyOrId;
407
        } elseif (is_string($keyOrId)) {
0 ignored issues
show
introduced by
The condition is_string($keyOrId) is always true.
Loading history...
408
            $this->parent['key'] = $keyOrId;
409
        }
410
411
        return $this;
412
    }
413
414
    public function setParent(Issue $parent)
415
    {
416
        $this->parent = $parent;
417
    }
418
419
    /**
420
     * add issue component.
421
     *
422
     * @param mixed string or array $component
423
     *
424
     * @return $this
425
     */
426
    public function addComponents($component)
427
    {
428
        if (is_null($this->components)) {
0 ignored issues
show
introduced by
The condition is_null($this->components) is always false.
Loading history...
429
            $this->components = [];
430
        }
431
432
        if (is_string($component)) {
433
            array_push($this->components, new Component($component));
434
        } elseif (is_array($component)) {
435
            foreach ($component as $c) {
436
                array_push($this->components, new Component($c));
437
            }
438
        }
439
440
        return $this;
441
    }
442
443
    /**
444
     * set security level.
445
     *
446
     * @param int $id issue's security id
447
     *
448
     * @return $this
449
     */
450
    public function setSecurityId($id)
451
    {
452
        if (empty($this->security)) {
453
            $this->security = new SecurityScheme();
454
        }
455
456
        $this->security->id = $id;
457
458
        return $this;
459
    }
460
461
    /**
462
     * set issue's due date.
463
     *
464
     * @param \DateTimeInterface|null $duedate due date string or DateTimeInterface object
465
     * @param string                  $format  datetime string format.
466
     *
467
     * @return $this
468
     */
469
    public function setDueDate($duedate, $format = 'Y-m-d')
470
    {
471
        if (is_string($duedate)) {
0 ignored issues
show
introduced by
The condition is_string($duedate) is always false.
Loading history...
472
            $this->duedate = $duedate;
473
        } elseif ($duedate instanceof \DateTimeInterface) {
474
            $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 DateTimeInterface|null 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...
475
        } else {
476
            $this->duedate = null;
477
        }
478
479
        return $this;
480
    }
481
482
    /**
483
     * set Assignee to Unassigned.
484
     *
485
     * @see https://confluence.atlassian.com/jirakb/how-to-set-assignee-to-unassigned-via-rest-api-in-jira-744721880.html
486
     */
487
    public function setAssigneeToUnassigned()
488
    {
489
        if (is_null($this->assignee)) {
490
            $this->assignee = new Reporter();
491
        }
492
493
        $this->assignee->setWantUnassigned(true);
494
495
        return $this;
496
    }
497
498
    public function setAssigneeToDefault()
499
    {
500
        if (is_null($this->assignee)) {
501
            $this->assignee = new Reporter();
502
        }
503
504
        $this->assignee->name = '-1';
505
506
        return $this;
507
    }
508
}
509