Completed
Push — master ( 569933...98080f )
by Philip
03:39
created

Issue::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Manavo\DoneDone;
4
5
class Issue
6
{
7
    /**
8
     * @var Client
9
     */
10
    private $client;
11
12
    /**
13
     * @var int
14
     */
15
    private $projectId;
16
17
    /**
18
     * @var int
19
     */
20
    private $id;
21
22
    private $title = null;
23
    private $priorityLevel = null;
24
    private $fixer = null;
25
    private $tester = null;
26
    private $description = null;
27
    private $dueDate = null;
28
    private $attachments = [];
29
    private $userIdsToCc = null;
30
    private $tags = null;
31
32
    /**
33
     * @param Client $client
34
     * @param int    $projectId
35
     * @param int    $id
36
     */
37 66
    function __construct($client = null, $projectId = null, $id = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
    {
39 66
        $this->client = $client;
40 66
        $this->projectId = $projectId;
41 66
        $this->id = $id;
42 66
    }
43
44
    /**
45
     * @param string $description
46
     */
47 3
    public function setDescription($description)
48
    {
49 3
        $this->description = $description;
50 3
    }
51
52
    /**
53
     * @param int $fixer
54
     */
55 3
    public function setFixer($fixer)
56
    {
57 3
        $this->fixer = $fixer;
58 3
    }
59
60
    /**
61
     * @param int $priorityLevel
62
     */
63 3
    public function setPriorityLevel($priorityLevel)
64
    {
65 3
        $this->priorityLevel = $priorityLevel;
66 3
    }
67
68
    /**
69
     * @param int $tester
70
     */
71 3
    public function setTester($tester)
72
    {
73 3
        $this->tester = $tester;
74 3
    }
75
76
    /**
77
     * @param string $title
78
     */
79 3
    public function setTitle($title)
80
    {
81 3
        $this->title = $title;
82 3
    }
83
84
    /**
85
     * Due date. Accepts Unix timestamp, or a formatted date string.
86
     *
87
     * Examples: 2014-09-20 16:40:31, 1411231231
88
     *
89
     * @param string|int $dueDate
90
     */
91 6
    public function setDueDate($dueDate)
92
    {
93 6
        if (is_numeric($dueDate)) {
94 3
            $dueDate = date('Y-m-d H:i:s', $dueDate);
95 3
        }
96 6
        $this->dueDate = $dueDate;
97 6
    }
98
99
    /**
100
     * Add an attachment to the issue
101
     *
102
     * @param string $file
103
     */
104 3
    public function addAttachment($file)
105
    {
106 3
        $this->attachments[] = $file;
107 3
    }
108
109
    /**
110
     * Set user IDs to CC. Can be an array of IDs, a comma separated list of
111
     * IDs, or just a single ID.
112
     *
113
     * @param array|string|int $ids
114
     */
115 6
    public function setUserIdsToCc($ids)
116
    {
117 6
        if (is_array($ids)) {
118 3
            $ids = implode(',', $ids);
119 3
        }
120
121 6
        $this->userIdsToCc = $ids;
122 6
    }
123
124
    /**
125
     * Set tags. Can be an array of tags, a comma separated list of
126
     * tags, or just a single tag.
127
     *
128
     * @param array|string $tags
129
     */
130 6
    public function setTags($tags)
131
    {
132 6
        if (is_array($tags)) {
133 3
            $tags = implode(',', $tags);
134 3
        }
135
136 6
        $this->tags = $tags;
137 6
    }
138
139
    /**
140
     * Add a new comment to the issue
141
     *
142
     * @param Comment $comment
143
     *
144
     * @return array
145
     */
146 3
    public function addComment($comment)
147
    {
148 3
        return $this->client->post(
149 3
            sprintf(
150 3
                'projects/%d/issues/%d/comments',
151 3
                $this->projectId,
152 3
                $this->id
153 3
            ),
154 3
            $comment->toArray()
155 3
        );
156
    }
157
158
    /**
159
     * Get a list of people who can be cc’d or assigned as the fixer or tester to the issue
160
     *
161
     * @return array
162
     */
163 3
    public function availableReassignees()
164
    {
165 3
        return $this->client->get(
166 3
            sprintf(
167 3
                'projects/%d/issues/%d/people/available_for_reassignment',
168 3
                $this->projectId,
169 3
                $this->id
170 3
            )
171 3
        );
172
    }
173
174
    /**
175
     * Get the details of this issue
176
     *
177
     * @return array
178
     */
179
    public function get()
180
    {
181
        return $this->client->get(
182
            sprintf('projects/%d/issues/%d', $this->projectId, $this->id)
183
        );
184
    }
185
186
    /**
187
     * Get a list of issue statuses the authenticated user may update the issue
188
     *
189
     * @return array
190
     */
191 3
    public function availableStatuses()
192
    {
193 3
        return $this->client->get(
194 3
            sprintf(
195 3
                'projects/%d/issues/%d/statuses/available_to_change_to',
196 3
                $this->projectId,
197 3
                $this->id
198 3
            )
199 3
        );
200
    }
201
202
    /**
203
     * Update the status of this issue
204
     *
205
     * @param int         $newStatus
206
     * @param string|null $comment
207
     * @param array       $attachments
208
     *
209
     * @return array
210
     */
211 3
    public function updateStatus(
212
        $newStatus,
213
        $comment = null,
214
        $attachments = []
215
    ) {
216
        $data = [
217 3
            'new_status_id' => $newStatus,
218 3
        ];
219
220 3
        return $this->update('status', $data, $comment, $attachments);
221
    }
222
    /**
223
     * Update the priority level of this issue
224
     *
225
     * @param int         $newLevel
226
     * @param string|null $comment
227
     * @param array       $attachments
228
     *
229
     * @return array
230
     */
231 3
    public function updatePriorityLevel(
232
        $newLevel,
233
        $comment = null,
234
        $attachments = []
235
    ) {
236
        $data = [
237 3
            'new_priority_level_id' => $newLevel,
238 3
        ];
239
240 3
        return $this->update('priority_level', $data, $comment, $attachments);
241
    }
242
243
    /**
244
     * Update the tester of this issue
245
     *
246
     * @param int         $newTester
247
     * @param string|null $comment
248
     * @param array       $attachments
249
     *
250
     * @return array
251
     */
252 3
    public function updateTester(
253
        $newTester,
254
        $comment = null,
255
        $attachments = []
256
    ) {
257
        $data = [
258 3
            'new_tester_id' => $newTester,
259 3
        ];
260
261 3
        return $this->update('tester', $data, $comment, $attachments);
262
    }
263
264
    /**
265
     * Update the fixer of this issue
266
     *
267
     * @param int         $newFixer
268
     * @param string|null $comment
269
     * @param array       $attachments
270
     *
271
     * @return array
272
     */
273 6
    public function updateFixer(
274
        $newFixer,
275
        $comment = null,
276
        $attachments = []
277
    ) {
278
        $data = [
279 6
            'new_fixer_id' => $newFixer,
280 6
        ];
281
282 6
        return $this->update('fixer', $data, $comment, $attachments);
283
    }
284
285
    /**
286
     * @param string $endpoint
287
     * @param array  $data
288
     * @param string $comment
289
     * @param array  $attachments
290
     *
291
     * @return array
292
     */
293 15
    private function update($endpoint, $data, $comment, $attachments)
294
    {
295 15
        if ($comment) {
296 3
            $data['comment'] = $comment;
297 3
        }
298
299 15
        foreach ($attachments as $index => $attachment) {
300
            $data['attachment-' . $index] = fopen($attachment, 'r');
301 15
        }
302
303 15
        return $this->client->put(
304 15
            sprintf(
305 15
                'projects/%d/issues/%d/%s',
306 15
                $this->projectId,
307 15
                $this->id,
308
                $endpoint
309 15
            ),
310
            $data
311 15
        );
312
    }
313
314
    /**
315
     * @return array
316
     */
317 42
    public function toArray()
318
    {
319
        $data = [
320 42
            'title'             => $this->title,
321 42
            'priority_level_id' => $this->priorityLevel,
322 42
            'fixer_id'          => $this->fixer,
323 42
            'tester_id'         => $this->tester,
324 42
        ];
325
326 42
        if ($this->description) {
327 3
            $data['description'] = $this->description;
328 3
        }
329
330 42
        if ($this->userIdsToCc) {
331 6
            $data['user_ids_to_cc'] = $this->userIdsToCc;
332 6
        }
333
334 42
        if ($this->dueDate) {
335 6
            $data['due_date'] = $this->dueDate;
336 6
        }
337
338 42
        if ($this->tags) {
339 6
            $data['tags'] = $this->tags;
340 6
        }
341
342 42
        foreach ($this->attachments as $index => $attachment) {
343 3
            $data['attachment-' . $index] = fopen($attachment, 'r');
344 42
        }
345
346 42
        return $data;
347
    }
348
349
}
350