Completed
Push — develop-3.0 ( 5ab583...f20237 )
by Mohamed
06:33
created

ProjectController::projectMainViewTabs()   D

Complexity

Conditions 17
Paths 270

Size

Total Lines 63
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 17

Importance

Changes 6
Bugs 3 Features 0
Metric Value
c 6
b 3
f 0
dl 0
loc 63
ccs 40
cts 40
cp 1
rs 4.4941
cc 17
eloc 46
nc 270
nop 4
crap 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ProjectController::getCreated() 0 12 1
A ProjectController::getNotes() 0 13 1
B ProjectController::indexView() 0 16 6

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Http\Controllers;
13
14
use Illuminate\Http\Request;
15
use Tinyissue\Form\FilterIssue as FilterForm;
16
use Tinyissue\Form\Note as NoteForm;
17
use Tinyissue\Form\Project as Form;
18
use Tinyissue\Http\Requests\FormRequest;
19
use Tinyissue\Model\Project;
20
use Tinyissue\Model\Project\Issue;
21
use Tinyissue\Model\Project\Note;
22
use Tinyissue\Services\Exporter;
23
24
/**
25
 * ProjectController is the controller class for managing request related to a project.
26
 *
27
 * @author Mohamed Alsharaf <[email protected]>
28
 */
29
class ProjectController extends Controller
30
{
31
    /**
32
     * Display project issues kanban view.
33
     *
34
     * @param Project $project
35
     *
36
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
37
     */
38
    public function getKanban(Project $project)
39
    {
40
        $columns = $project->getKanbanTagsForUser($this->getLoggedUser());
41
        $issues = $project->getIssuesGroupByTags($columns);
0 ignored issues
show
Documentation Bug introduced by
The method getIssuesGroupByTags does not exist on object<Tinyissue\Model\Project>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
42
43
44
        return view('project.issues-kanban', [
45
            'sidebar' => 'project',
46
            'columns' => $columns,
47
            'issues' => $issues,
48
            'project' => $project,
49
            'open_issues_count' => $project->countOpenIssues($this->getLoggedUser()),
50
            'closed_issues_count' => $project->countClosedIssues($this->getLoggedUser()),
51
        ]);
52
    }
53
54
    /**
55
     * Display activity for a project.
56
     *
57
     * @param Project $project
58
     *
59
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
60
     */
61
    public function getIndex(Project $project)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $activities = $project->getRecentActivities($this->getLoggedUser());
64
65
        return $this->indexView([
66
            'activities'          => $activities,
67
            'notes_count'         => $project->countNotes(),
68
            'open_issues_count'   => $project->countOpenIssues($this->getLoggedUser()),
69
            'closed_issues_count' => $project->countClosedIssues($this->getLoggedUser()),
70
        ], 'activity', $project);
71
    }
72
73
    /**
74
     * Display issues for a project.
75
     *
76
     * @param FilterForm $filterForm
77
     * @param Request    $request
78
     * @param Project    $project
79
     * @param int        $status
80
     *
81
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
82
     */
83
    public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN)
84
    {
85
        if ($status === Issue::STATUS_OPEN) {
86
            return $this->getOpenIssues($filterForm, $request, $project);
87
        }
88
89
        return $this->getClosedIssues($filterForm, $request, $project);
90
    }
91
92
    /**
93
     * Display open issues.
94
     *
95
     * @param FilterForm $filterForm
96
     * @param Request    $request
97
     * @param Project    $project
98
     *
99
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
100
     */
101
    public function getOpenIssues(FilterForm $filterForm, Request $request, Project $project)
102
    {
103
        $issues = $project->getIssuesForLoggedUser(Issue::STATUS_OPEN, $request->all());
104
105
        return $this->indexView([
106
            'notes_count'         => $project->countNotes(),
107
            'issues'              => $issues,
108
            'filterForm'          => $filterForm,
109
            'open_issues_count'   => $issues->count(),
110
            'closed_issues_count' => $project->countClosedIssues($this->getLoggedUser()),
111
        ], 'open_issues', $project);
112
    }
113
114
    /**
115
     * Display closed issues.
116
     *
117
     * @param FilterForm $filterForm
118
     * @param Request    $request
119
     * @param Project    $project
120
     *
121
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
122
     */
123
    public function getClosedIssues(FilterForm $filterForm, Request $request, Project $project)
124
    {
125
        $issues = $project->getIssuesForLoggedUser(Issue::STATUS_CLOSED, $request->all());
126
127
        return $this->indexView([
128
            'notes_count'         => $project->countNotes(),
129
            'issues'              => $issues,
130
            'filterForm'          => $filterForm,
131
            'open_issues_count'   => $project->countOpenIssues($this->getLoggedUser()),
132
            'closed_issues_count' => $issues->count(),
133
        ], 'closed_issues', $project);
134
    }
135
136
    /**
137
     * Display issues assigned to current user for a project.
138
     *
139
     * @param Project $project
140
     *
141
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
142
     */
143
    public function getAssigned(Project $project)
144
    {
145
        $issues = $project->getAssignedOrCreatedIssues($this->getLoggedUser());
146
147
        return $this->indexView([
148
            'notes_count'           => $project->countNotes(),
149
            'open_issues_count'     => $project->countOpenIssues($this->getLoggedUser()),
150
            'closed_issues_count'   => $project->countClosedIssues($this->getLoggedUser()),
151
            'assigned_issues_count' => $issues->count(),
152
            'issues'                => $issues,
153
        ], 'activity', $project);
154
    }
155
156
    /**
157
     * Display issues created to current user for a project.
158
     *
159
     * @param Project $project
160
     *
161
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
162
     */
163
    public function getCreated(Project $project)
164
    {
165
        $issues = $project->getAssignedOrCreatedIssues($this->getLoggedUser());
166
167
        return $this->indexView([
168
            'notes_count'           => $project->countNotes(),
169
            'open_issues_count'     => $project->countOpenIssues($this->getLoggedUser()),
170
            'closed_issues_count'   => $project->countClosedIssues($this->getLoggedUser()),
171
            'assigned_issues_count' => $issues->count(),
172
            'issues'                => $issues,
173
        ], 'issue_created_by_you', $project);
174
    }
175
176
    /**
177
     * Display notes for a project.
178
     *
179
     * @param Project  $project
180
     * @param NoteForm $form
181
     *
182
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
183
     */
184
    public function getNotes(Project $project, NoteForm $form)
185
    {
186
        $notes = $project->getNotes();
187
188
        return $this->indexView([
189
            'notes_count'         => $project->countNotes(),
190
            'open_issues_count'   => $project->countOpenIssues($this->getLoggedUser()),
191
            'closed_issues_count' => $project->countClosedIssues($this->getLoggedUser()),
192
            'notes'               => $notes,
193
            'notes_count'         => $notes->count(),
194
            'noteForm'            => $form,
195
        ], 'notes', $project);
196
    }
197
198
    /**
199
     * @param mixed   $data
200
     * @param string  $active
201
     * @param Project $project
202
     *
203
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
204
     */
205
    protected function indexView($data, $active, Project $project)
206
    {
207
        // For logged users that is not a normal user
208
        if (!array_key_exists('assigned_issues_count', $data) && $this->isLoggedIn() && !$this->isLoggedNormalUser()) {
209
            $data['assigned_issues_count'] = $project->countAssignedIssues($this->getLoggedUser());
210
        } elseif ($this->isLoggedNormalUser() && $project->isPrivateInternal()) {
211
            $data['created_issues_count'] = $project->countCreatedIssues($this->getLoggedUser());
212
        }
213
214
        $data['sidebar']           = 'project';
215
        $data['active']            = $active;
216
        $data['project']           = $project;
217
        $data['usersCanFixIssues'] = $project->getUsersCanFixIssue();
218
219
        return view('project.index', $data);
220
    }
221
222
    /**
223
     * Edit the project.
224
     *
225
     * @param Project $project
226
     * @param Form    $form
227
     *
228
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
229
     */
230
    public function getEdit(Project $project, Form $form)
231
    {
232
        return view('project.edit', [
233
            'form'    => $form,
234
            'project' => $project,
235
            'sidebar' => 'project',
236
        ]);
237
    }
238
239
    /**
240
     * To update project details.
241
     *
242
     * @param Project             $project
243
     * @param FormRequest\Project $request
244
     *
245
     * @return \Illuminate\Http\RedirectResponse
246
     */
247
    public function postEdit(Project $project, FormRequest\Project $request)
248
    {
249
        // Delete the project
250
        if ($request->has('delete-project')) {
251
            $project->updater()->delete();
252
253
            return redirect('projects')
254
                ->with('notice', trans('tinyissue.project_has_been_deleted'));
255
        }
256
257
        $project->updater()->update($request->all());
258
259
        return redirect($project->to())
260
            ->with('notice', trans('tinyissue.project_has_been_updated'));
261
    }
262
263
    /**
264
     * Ajax: returns list of users that are not in the project.
265
     *
266
     * @param Project $project
267
     *
268
     * @return \Symfony\Component\HttpFoundation\Response
269
     */
270
    public function getInactiveUsers(Project $project)
271
    {
272
        $users = $project->getNotMembers()->dropdown('fullname');
273
274
        return response()->json($users);
275
    }
276
277
    /**
278
     * Ajax: add user to the project.
279
     *
280
     * @param Project $project
281
     * @param Request $request
282
     *
283
     * @return \Symfony\Component\HttpFoundation\Response
284
     */
285
    public function postAssign(Project $project, Request $request)
286
    {
287
        $status = $project->updater($this->getLoggedUser())->assignUser((int) $request->input('user_id'));
288
289
        return response()->json(['status' => (bool) $status]);
290
    }
291
292
    /**
293
     * Ajax: remove user from the project.
294
     *
295
     * @param Project $project
296
     * @param Request $request
297
     *
298
     * @return \Symfony\Component\HttpFoundation\Response
299
     */
300
    public function postUnassign(Project $project, Request $request)
301
    {
302
        $status = $project->updater($this->getLoggedUser())->unassignUser((int) $request->input('user_id'));
303
304
        return response()->json(['status' => (bool) $status]);
305
    }
306
307
    /**
308
     * To add a new note to the project.
309
     *
310
     * @param Project          $project
311
     * @param Note             $note
312
     * @param FormRequest\Note $request
313
     *
314
     * @return \Illuminate\Http\RedirectResponse
315
     */
316
    public function postAddNote(Project $project, Note $note, FormRequest\Note $request)
317
    {
318
        $note->setRelation('project', $project);
319
        $note->setRelation('createdBy', $this->getLoggedUser());
320
        $note->updater($this->getLoggedUser())->create($request->all());
321
322
        return redirect($note->to())->with('notice', trans('tinyissue.your_note_added'));
323
    }
324
325
    /**
326
     * Ajax: To update project note.
327
     *
328
     * @param Project $project
329
     * @param Note    $note
330
     * @param Request $request
331
     *
332
     * @return \Symfony\Component\HttpFoundation\Response
333
     */
334
    public function postEditNote(Project $project, Note $note, Request $request)
335
    {
336
        $body = '';
337
        if ($request->has('body')) {
338
            $note->setRelation('project', $project);
339
            $note->updater($this->getLoggedUser())->updateBody((string)$request->input('body'));
340
341
            $body = \Html::format($note->body);
342
        }
343
344
        return response()->json(['status' => true, 'text' => $body]);
345
    }
346
347
    /**
348
     * Ajax: to delete a project note.
349
     *
350
     * @param Project $project
351
     * @param Note    $note
352
     *
353
     * @return \Symfony\Component\HttpFoundation\Response
354
     */
355
    public function getDeleteNote(Project $project, Note $note)
356
    {
357
        $note->setRelation('project', $project);
358
        $note->updater($this->getLoggedUser())->delete();
359
360
        return response()->json(['status' => true]);
361
    }
362
363
    /**
364
     * Ajax: generate the issues export file.
365
     *
366
     * @param Project  $project
367
     * @param Exporter $exporter
368
     * @param Request  $request
369
     *
370
     * @return \Symfony\Component\HttpFoundation\Response
371
     */
372
    public function postExportIssues(Project $project, Exporter $exporter, Request $request)
373
    {
374
        // Generate export file
375
        $info = $exporter->exportFile(
376
            'Project\Issue',
377
            $request->input('format', Exporter::TYPE_CSV),
0 ignored issues
show
Bug introduced by
It seems like $request->input('format'...ces\Exporter::TYPE_CSV) targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Tinyissue\Services\Exporter::exportFile() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
378
            $request->all()
379
        );
380
381
        // Download link
382
        $link = link_to(
383
            $project->to('download_export/' . $info['file']),
384
            trans('tinyissue.download_export'),
0 ignored issues
show
Bug introduced by
It seems like trans('tinyissue.download_export') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, link_to() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
385
            ['class' => 'btn btn-link']
386
        );
387
388
        return response()->json([
389
            'link'  => $link,
390
            'title' => $info['title'],
391
            'file'  => $info['file'],
392
            'ext'   => $info['ext'],
393
        ]);
394
    }
395
396
    /**
397
     * Download and then delete an export file.
398
     *
399
     * @param Project $project
400
     * @param string  $file
401
     *
402
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
403
     */
404
    public function getDownloadExport(Project $project, $file)
405
    {
406
        $this->authorize('export', $project);
407
408
        // Filter out any characters that are not in pattern
409
        $file = preg_replace('/[^a-z0-9\_\.]/mi', '', $file);
410
411
        // Download export
412
        return response()->download(storage_path('exports/' . $file), $file)->deleteFileAfterSend(true);
413
    }
414
}
415