Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.23% covered (danger)
35.23%
31 / 88
50.00% covered (danger)
50.00%
7 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
IssueCreateRequest
35.23% covered (danger)
35.23%
31 / 88
50.00% covered (danger)
50.00%
7 / 14
128.70
0.00% covered (danger)
0.00%
0 / 1
 make
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 projectKey
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 summary
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 description
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 descriptionWithParagraphs
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 descriptionDivider
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 descriptionQuote
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 descriptionInfoPanel
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 descriptionWarningPanel
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 descriptionSuccessPanel
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 issueType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 label
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 validate
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
4.59
1<?php
2
3namespace Javidnikoo\LaravelAtlassian\Jira\Features\Issue\Requests;
4
5use Javidnikoo\LaravelAtlassian\Jira\Enums\IssueType;
6use Javidnikoo\LaravelAtlassian\Jira\Exceptions\JiraException;
7
8class IssueCreateRequest
9{
10    protected array $fields = [];
11
12    protected array $descriptionContent = [];
13
14    public static function make(): static
15    {
16        return new static;
17    }
18
19    public function projectKey(string $key): self
20    {
21        $this->fields['project'] = ['key' => $key];
22
23        return $this;
24    }
25
26    public function summary(string $summary): self
27    {
28        $this->fields['summary'] = $summary;
29
30        return $this;
31    }
32
33    /**
34     * Add a simple paragraph of plain text
35     */
36    public function description(string $text): self
37    {
38        $this->descriptionContent[] = [
39            'type' => 'paragraph',
40            'content' => [
41                [
42                    'type' => 'text',
43                    'text' => $text,
44                ],
45            ],
46        ];
47
48        return $this;
49    }
50
51    /**
52     * Add multiple paragraphs at once
53     */
54    public function descriptionWithParagraphs(array $paragraphs): self
55    {
56        foreach ($paragraphs as $text) {
57            $this->descriptionContent[] = [
58                'type' => 'paragraph',
59                'content' => [
60                    ['type' => 'text', 'text' => $text],
61                ],
62            ];
63        }
64
65        return $this;
66    }
67
68    /**
69     * Add a horizontal divider (visual line / <hr>)
70     */
71    public function descriptionDivider(): self
72    {
73        $this->descriptionContent[] = [
74            'type' => 'rule',
75        ];
76
77        return $this;
78    }
79
80    /**
81     * Add a quote block
82     */
83    public function descriptionQuote(string $text): self
84    {
85        $this->descriptionContent[] = [
86            'type' => 'blockquote',
87            'content' => [
88                [
89                    'type' => 'paragraph',
90                    'content' => [['type' => 'text', 'text' => $text]],
91                ],
92            ],
93        ];
94
95        return $this;
96    }
97
98    /**
99     * Add an info panel (blue box)
100     */
101    public function descriptionInfoPanel(string $text): self
102    {
103        $this->descriptionContent[] = [
104            'type' => 'panel',
105            'attrs' => ['panelType' => 'info'],
106            'content' => [
107                [
108                    'type' => 'paragraph',
109                    'content' => [['type' => 'text', 'text' => $text]],
110                ],
111            ],
112        ];
113
114        return $this;
115    }
116
117    /**
118     * Add a warning panel (yellow box)
119     */
120    public function descriptionWarningPanel(string $text): self
121    {
122        $this->descriptionContent[] = [
123            'type' => 'panel',
124            'attrs' => ['panelType' => 'warning'],
125            'content' => [
126                [
127                    'type' => 'paragraph',
128                    'content' => [['type' => 'text', 'text' => $text]],
129                ],
130            ],
131        ];
132
133        return $this;
134    }
135
136    /**
137     * Add a success panel (green box) - bonus
138     */
139    public function descriptionSuccessPanel(string $text): self
140    {
141        $this->descriptionContent[] = [
142            'type' => 'panel',
143            'attrs' => ['panelType' => 'success'],
144            'content' => [
145                [
146                    'type' => 'paragraph',
147                    'content' => [['type' => 'text', 'text' => $text]],
148                ],
149            ],
150        ];
151
152        return $this;
153    }
154
155    public function issueType(IssueType|string $type): self
156    {
157        $this->fields['issuetype'] = ['name' => $type instanceof IssueType ? $type->value : $type];
158
159        return $this;
160    }
161
162    public function label(string $label): self
163    {
164        $this->fields['labels'][] = $label;
165
166        return $this;
167    }
168
169    public function toArray(): array
170    {
171        $this->validate();
172
173        if (! empty($this->descriptionContent)) {
174            $this->fields['description'] = [
175                'type' => 'doc',
176                'version' => 1,
177                'content' => $this->descriptionContent,
178            ];
179        }
180
181        return ['fields' => $this->fields];
182    }
183
184    protected function validate(): void
185    {
186        if (empty($this->fields['project']['key'])) {
187            throw new JiraException('Project key is required.');
188        }
189
190        if (empty($this->fields['summary'])) {
191            throw new JiraException('Summary is required.');
192        }
193
194        if (empty($this->fields['issuetype']['name'])) {
195            throw new JiraException('Issue type is required.');
196        }
197    }
198}