Completed
Push — master ( a378ab...305f92 )
by Philip
08:21 queued 05:36
created

Issue   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 333
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 53.39%

Importance

Changes 12
Bugs 2 Features 1
Metric Value
wmc 29
c 12
b 2
f 1
lcom 2
cbo 2
dl 0
loc 333
ccs 63
cts 118
cp 0.5339
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setDescription() 0 4 1
A setFixer() 0 4 1
A setPriorityLevel() 0 4 1
A setTester() 0 4 1
A setTitle() 0 4 1
A addAttachment() 0 4 1
A __construct() 0 6 1
A setDueDate() 0 7 2
A setUserIdsToCc() 0 8 2
A setTags() 0 8 2
B toArray() 0 31 6
A addComment() 0 11 1
A updateStatus() 0 11 1
A updatePriorityLevel() 0 11 1
A updateTester() 0 11 1
A updateFixer() 0 11 1
A update() 0 20 3
A availableReassignees() 0 10 1
A availableStatuses() 0 10 1
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 42
    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 42
        $this->client = $client;
40 42
        $this->projectId = $projectId;
41 42
        $this->id = $id;
42 42
    }
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
    public function addComment($comment)
147
    {
148
        return $this->client->post(
149
            sprintf(
150
                'projects/%d/issues/%d/comments',
151
                $this->projectId,
152
                $this->id
153
            ),
154
            $comment->toArray()
155
        );
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
    public function availableReassignees()
164
    {
165
        return $this->client->get(
166
            sprintf(
167
                'projects/%d/issues/%d/people/available_for_reassignment',
168
                $this->projectId,
169
                $this->id
170
            )
171
        );
172
    }
173
174
    /**
175
     * Get a list of issue statuses the authenticated user may update the issue
176
     *
177
     * @return array
178
     */
179
    public function availableStatuses()
180
    {
181
        return $this->client->get(
182
            sprintf(
183
                'projects/%d/issues/%d/statuses/available_to_change_to',
184
                $this->projectId,
185
                $this->id
186
            )
187
        );
188
    }
189
190
    /**
191
     * Update the status of this issue
192
     *
193
     * @param int         $newStatus
194
     * @param string|null $comment
195
     * @param array       $attachments
196
     *
197
     * @return array
198
     */
199
    public function updateStatus(
200
        $newStatus,
201
        $comment = null,
202
        $attachments = []
203
    ) {
204
        $data = [
205
            'new_status_id' => $newStatus,
206
        ];
207
208
        return $this->update('status', $data, $comment, $attachments);
209
    }
210
    /**
211
     * Update the priority level of this issue
212
     *
213
     * @param int         $newLevel
214
     * @param string|null $comment
215
     * @param array       $attachments
216
     *
217
     * @return array
218
     */
219
    public function updatePriorityLevel(
220
        $newLevel,
221
        $comment = null,
222
        $attachments = []
223
    ) {
224
        $data = [
225
            'new_priority_level_id' => $newLevel,
226
        ];
227
228
        return $this->update('priority_level', $data, $comment, $attachments);
229
    }
230
231
    /**
232
     * Update the tester of this issue
233
     *
234
     * @param int         $newTester
235
     * @param string|null $comment
236
     * @param array       $attachments
237
     *
238
     * @return array
239
     */
240
    public function updateTester(
241
        $newTester,
242
        $comment = null,
243
        $attachments = []
244
    ) {
245
        $data = [
246
            'new_tester_id' => $newTester,
247
        ];
248
249
        return $this->update('tester', $data, $comment, $attachments);
250
    }
251
252
    /**
253
     * Update the fixer of this issue
254
     *
255
     * @param int         $newFixer
256
     * @param string|null $comment
257
     * @param array       $attachments
258
     *
259
     * @return array
260
     */
261
    public function updateFixer(
262
        $newFixer,
263
        $comment = null,
264
        $attachments = []
265
    ) {
266
        $data = [
267
            'new_fixer_id' => $newFixer,
268
        ];
269
270
        return $this->update('fixer', $data, $comment, $attachments);
271
    }
272
273
    /**
274
     * @param string $endpoint
275
     * @param array  $data
276
     * @param string $comment
277
     * @param array  $attachments
278
     *
279
     * @return array
280
     */
281
    private function update($endpoint, $data, $comment, $attachments)
282
    {
283
        if ($comment) {
284
            $data['comment'] = $comment;
285
        }
286
287
        foreach ($attachments as $index => $attachment) {
288
            $data['attachment-' . $index] = fopen($attachment, 'r');
289
        }
290
291
        return $this->client->put(
292
            sprintf(
293
                'projects/%d/issues/%d/%s',
294
                $this->projectId,
295
                $this->id,
296
                $endpoint
297
            ),
298
            $data
299
        );
300
    }
301
302
    /**
303
     * @return array
304
     */
305 42
    public function toArray()
306
    {
307
        $data = [
308 42
            'title'             => $this->title,
309 42
            'priority_level_id' => $this->priorityLevel,
310 42
            'fixer_id'          => $this->fixer,
311 42
            'tester_id'         => $this->tester,
312 42
        ];
313
314 42
        if ($this->description) {
315 3
            $data['description'] = $this->description;
316 3
        }
317
318 42
        if ($this->userIdsToCc) {
319 6
            $data['user_ids_to_cc'] = $this->userIdsToCc;
320 6
        }
321
322 42
        if ($this->dueDate) {
323 6
            $data['due_date'] = $this->dueDate;
324 6
        }
325
326 42
        if ($this->tags) {
327 6
            $data['tags'] = $this->tags;
328 6
        }
329
330 42
        foreach ($this->attachments as $index => $attachment) {
331 3
            $data['attachment-' . $index] = fopen($attachment, 'r');
332 42
        }
333
334 42
        return $data;
335
    }
336
337
}
338