Passed
Push — master ( ff2040...6f6996 )
by KwangSeob
02:18
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 assignee name.
234
     *
235
     * @param string $name
236
     *
237
     * @return $this
238
     */
239
    public function setAssigneeName($name)
240
    {
241
        if (is_null($this->assignee)) {
242
            $this->assignee = new Reporter();
243
        }
244
245
        $this->assignee->name = $name;
246
247
        return $this;
248
    }
249
250
    /**
251
     * set issue assignee accountId.
252
     *
253
     * @param string $accountId
254
     *
255
     * @return $this
256
     */
257
    public function setAssigneeAccountId($accountId)
258
    {
259
        if (is_null($this->assignee)) {
260
            $this->assignee = new Reporter();
261
        }
262
263
        $this->assignee->accountId = $accountId;
264
265
        // REST API V3 must name field set to null.
266
        $this->assignee->name = null;
267
        $this->assignee->setWantUnassigned(true);
268
269
        return $this;
270
    }
271
272
    /**
273
     * set issue priority name.
274
     *
275
     * @param string $name
276
     *
277
     * @return $this
278
     */
279
    public function setPriorityName($name)
280
    {
281
        if (is_null($this->priority)) {
282
            $this->priority = new Priority();
283
        }
284
285
        $this->priority->name = $name;
286
287
        return $this;
288
    }
289
290
    /**
291
     * set issue description.
292
     *
293
     * REST API V3 must use addDescriptionXXXX
294
     *
295
     * @see \JiraRestApi\Issue\IssueFieldV3::addDescriptionHeading
296
     * @see \JiraRestApi\Issue\IssueFieldV3::addDescriptionParagraph
297
     *
298
     * @param string|null $description
299
     *
300
     * @return $this
301
     */
302
    public function setDescription($description)
303
    {
304
        if (!empty($description)) {
305
            $this->description = $description;
306
        }
307
308
        return $this;
309
    }
310
311
    /**
312
     * add a Affects version.
313
     *
314
     * @param mixed string or array $name
315
     *
316
     * @return $this
317
     */
318
    public function addVersion($name)
319
    {
320
        if (is_null($this->versions)) {
0 ignored issues
show
introduced by
The condition is_null($this->versions) is always false.
Loading history...
321
            $this->versions = [];
322
        }
323
324
        if (is_string($name)) {
325
            array_push($this->versions, new Version($name));
326
        } elseif (is_array($name)) {
327
            foreach ($name as $v) {
328
                array_push($this->versions, new Version($v));
329
            }
330
        }
331
332
        return $this;
333
    }
334
335
    /**
336
     * add issue label.
337
     *
338
     * @param string $label
339
     *
340
     * @return $this
341
     */
342
    public function addLabel($label)
343
    {
344
        if (is_null($this->labels)) {
0 ignored issues
show
introduced by
The condition is_null($this->labels) is always false.
Loading history...
345
            $this->labels = [];
346
        }
347
348
        array_push($this->labels, $label);
349
350
        return $this;
351
    }
352
353
    /**
354
     * set issue type.
355
     *
356
     * @param mixed IssueType or string $name
357
     *
358
     * @return $this
359
     */
360
    public function setIssueType($name)
361
    {
362
        if (is_string($name)) {
363
            if (is_null($this->issuetype)) {
364
                $this->issuetype = new IssueType();
365
            }
366
367
            $this->issuetype->name = $name;
368
        } else {
369
            $this->issuetype = $name;
370
        }
371
372
        return $this;
373
    }
374
375
    public function getIssueType()
376
    {
377
        return $this->issuetype;
378
    }
379
380
    /**
381
     *  set parent issue.
382
     *
383
     * @param string $keyOrId
384
     */
385
    public function setParentKeyOrId($keyOrId)
386
    {
387
        if (is_numeric($keyOrId)) {
388
            $this->parent['id'] = $keyOrId;
389
        } elseif (is_string($keyOrId)) {
0 ignored issues
show
introduced by
The condition is_string($keyOrId) is always true.
Loading history...
390
            $this->parent['key'] = $keyOrId;
391
        }
392
    }
393
394
    public function setParent(Issue $parent)
395
    {
396
        $this->parent = $parent;
397
    }
398
399
    /**
400
     * add issue component.
401
     *
402
     * @param mixed string or array $component
403
     *
404
     * @return $this
405
     */
406
    public function addComponents($component)
407
    {
408
        if (is_null($this->components)) {
0 ignored issues
show
introduced by
The condition is_null($this->components) is always false.
Loading history...
409
            $this->components = [];
410
        }
411
412
        if (is_string($component)) {
413
            array_push($this->components, new Component($component));
414
        } elseif (is_array($component)) {
415
            foreach ($component as $c) {
416
                array_push($this->components, new Component($c));
417
            }
418
        }
419
420
        return $this;
421
    }
422
423
    /**
424
     * set security level.
425
     *
426
     * @param int $id issue's security id
427
     *
428
     * @return $this
429
     */
430
    public function setSecurityId($id)
431
    {
432
        if (empty($this->security)) {
433
            $this->security = new SecurityScheme();
434
        }
435
436
        $this->security->id = $id;
437
438
        return $this;
439
    }
440
441
    /**
442
     * set issue's due date.
443
     *
444
     * @param \DateTimeInterface|null $duedate due date string or DateTimeInterface object
445
     * @param string                  $format  datetime string format.
446
     *
447
     * @return $this
448
     */
449
    public function setDueDate($duedate, $format = 'Y-m-d')
450
    {
451
        if (is_string($duedate)) {
0 ignored issues
show
introduced by
The condition is_string($duedate) is always false.
Loading history...
452
            $this->duedate = $duedate;
453
        } elseif ($duedate instanceof \DateTimeInterface) {
454
            $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...
455
        } else {
456
            $this->duedate = null;
457
        }
458
459
        return $this;
460
    }
461
462
    /**
463
     * set Assignee to Unassigned.
464
     *
465
     * @see https://confluence.atlassian.com/jirakb/how-to-set-assignee-to-unassigned-via-rest-api-in-jira-744721880.html
466
     */
467
    public function setAssigneeToUnassigned()
468
    {
469
        if (is_null($this->assignee)) {
470
            $this->assignee = new Reporter();
471
        }
472
473
        $this->assignee->setWantUnassigned(true);
474
475
        return $this;
476
    }
477
478
    public function setAssigneeToDefault()
479
    {
480
        if (is_null($this->assignee)) {
481
            $this->assignee = new Reporter();
482
        }
483
484
        $this->assignee->name = '-1';
485
486
        return $this;
487
    }
488
}
489