Completed
Push — develop-3.0 ( 360277...bd5ff0 )
by Mohamed
06:52
created

ProjectController   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 363
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 26
lcom 1
cbo 13
dl 0
loc 363
rs 10
c 2
b 1
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
B indexView() 0 16 6
A getEdit() 0 8 1
A postEdit() 0 15 2
A getInactiveUsers() 0 6 1
A postAssign() 0 6 1
A postUnassign() 0 6 1
A getDeleteNote() 0 7 1
A postExportIssues() 0 23 1
A getIssues() 0 8 2
A getDownloadExport() 0 10 1
A getIndex() 0 11 1
A getOpenIssues() 0 12 1
A getClosedIssues() 0 12 1
A getAssigned() 0 12 1
A getCreated() 0 12 1
A getNotes() 0 13 1
A postAddNote() 0 8 1
A postEditNote() 0 12 2
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 activity for a project.
33
     *
34
     * @param Project $project
35
     *
36
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
37
     */
38
    public function getIndex(Project $project)
39
    {
40
        $activities = $project->getRecentActivities($this->getLoggedUser());
41
42
        return $this->indexView([
43
            'activities'          => $activities,
44
            'notes_count'         => $project->countNotes(),
45
            'open_issues_count'   => $project->countOpenIssues($this->getLoggedUser()),
46
            'closed_issues_count' => $project->countClosedIssues($this->getLoggedUser()),
47
        ], 'activity', $project);
48
    }
49
50
    /**
51
     * Display issues for a project.
52
     *
53
     * @param FilterForm $filterForm
54
     * @param Request    $request
55
     * @param Project    $project
56
     * @param int        $status
57
     *
58
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
59
     */
60
    public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN)
61
    {
62
        if ($status === Issue::STATUS_OPEN) {
63
            return $this->getOpenIssues($filterForm, $request, $project);
64
        }
65
66
        return $this->getClosedIssues($filterForm, $request, $project);
67
    }
68
69
    /**
70
     * Display open issues.
71
     *
72
     * @param FilterForm $filterForm
73
     * @param Request    $request
74
     * @param Project    $project
75
     *
76
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
77
     */
78
    public function getOpenIssues(FilterForm $filterForm, Request $request, Project $project)
79
    {
80
        $issues = $project->getIssuesForLoggedUser(Issue::STATUS_OPEN, $request->all());
81
82
        return $this->indexView([
83
            'notes_count'         => $project->countNotes(),
84
            'issues'              => $issues,
85
            'filterForm'          => $filterForm,
86
            'open_issues_count'   => $issues->count(),
87
            'closed_issues_count' => $project->countClosedIssues($this->getLoggedUser()),
88
        ], 'open_issues', $project);
89
    }
90
91
    /**
92
     * Display closed issues.
93
     *
94
     * @param FilterForm $filterForm
95
     * @param Request    $request
96
     * @param Project    $project
97
     *
98
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
99
     */
100
    public function getClosedIssues(FilterForm $filterForm, Request $request, Project $project)
101
    {
102
        $issues = $project->getIssuesForLoggedUser(Issue::STATUS_CLOSED, $request->all());
103
104
        return $this->indexView([
105
            'notes_count'         => $project->countNotes(),
106
            'issues'              => $issues,
107
            'filterForm'          => $filterForm,
108
            'open_issues_count'   => $project->countOpenIssues($this->getLoggedUser()),
109
            'closed_issues_count' => $issues->count(),
110
        ], 'closed_issues', $project);
111
    }
112
113
    /**
114
     * Display issues assigned to current user for a project.
115
     *
116
     * @param Project $project
117
     *
118
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
119
     */
120
    public function getAssigned(Project $project)
121
    {
122
        $issues = $project->getAssignedOrCreatedIssues($this->getLoggedUser());
123
124
        return $this->indexView([
125
            'notes_count'           => $project->countNotes(),
126
            'open_issues_count'     => $project->countOpenIssues($this->getLoggedUser()),
127
            'closed_issues_count'   => $project->countClosedIssues($this->getLoggedUser()),
128
            'assigned_issues_count' => $issues->count(),
129
            'issues'                => $issues,
130
        ], 'activity', $project);
131
    }
132
133
    /**
134
     * Display issues created to current user for a project.
135
     *
136
     * @param Project $project
137
     *
138
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
139
     */
140
    public function getCreated(Project $project)
141
    {
142
        $issues = $project->getAssignedOrCreatedIssues($this->getLoggedUser());
143
144
        return $this->indexView([
145
            'notes_count'           => $project->countNotes(),
146
            'open_issues_count'     => $project->countOpenIssues($this->getLoggedUser()),
147
            'closed_issues_count'   => $project->countClosedIssues($this->getLoggedUser()),
148
            'assigned_issues_count' => $issues->count(),
149
            'issues'                => $issues,
150
        ], 'issue_created_by_you', $project);
151
    }
152
153
    /**
154
     * Display notes for a project.
155
     *
156
     * @param Project  $project
157
     * @param NoteForm $form
158
     *
159
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
160
     */
161
    public function getNotes(Project $project, NoteForm $form)
162
    {
163
        $notes = $project->getNotes();
164
165
        return $this->indexView([
166
            'notes_count'         => $project->countNotes(),
167
            'open_issues_count'   => $project->countOpenIssues($this->getLoggedUser()),
168
            'closed_issues_count' => $project->countClosedIssues($this->getLoggedUser()),
169
            'notes'               => $notes,
170
            'notes_count'         => $notes->count(),
171
            'noteForm'            => $form,
172
        ], 'notes', $project);
173
    }
174
175
    /**
176
     * @param mixed   $data
177
     * @param string  $active
178
     * @param Project $project
179
     *
180
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
181
     */
182
    protected function indexView($data, $active, Project $project)
183
    {
184
        // For logged users that is not a normal user
185
        if (!array_key_exists('assigned_issues_count', $data) && $this->isLoggedIn() && !$this->isLoggedNormalUser()) {
186
            $data['assigned_issues_count'] = $project->countAssignedIssues($this->getLoggedUser());
187
        } elseif ($this->isLoggedNormalUser() && $project->isPrivateInternal()) {
188
            $data['created_issues_count'] = $project->countCreatedIssues($this->getLoggedUser());
189
        }
190
191
        $data['sidebar']           = 'project';
192
        $data['active']            = $active;
193
        $data['project']           = $project;
194
        $data['usersCanFixIssues'] = $project->getUsersCanFixIssue();
195
196
        return view('project.index', $data);
197
    }
198
199
    /**
200
     * Edit the project.
201
     *
202
     * @param Project $project
203
     * @param Form    $form
204
     *
205
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
206
     */
207
    public function getEdit(Project $project, Form $form)
208
    {
209
        return view('project.edit', [
210
            'form'    => $form,
211
            'project' => $project,
212
            'sidebar' => 'project',
213
        ]);
214
    }
215
216
    /**
217
     * To update project details.
218
     *
219
     * @param Project             $project
220
     * @param FormRequest\Project $request
221
     *
222
     * @return \Illuminate\Http\RedirectResponse
223
     */
224
    public function postEdit(Project $project, FormRequest\Project $request)
225
    {
226
        // Delete the project
227
        if ($request->has('delete-project')) {
228
            $project->updater()->delete();
229
230
            return redirect('projects')
231
                ->with('notice', trans('tinyissue.project_has_been_deleted'));
232
        }
233
234
        $project->updater()->update($request->all());
235
236
        return redirect($project->to())
237
            ->with('notice', trans('tinyissue.project_has_been_updated'));
238
    }
239
240
    /**
241
     * Ajax: returns list of users that are not in the project.
242
     *
243
     * @param Project $project
244
     *
245
     * @return \Symfony\Component\HttpFoundation\Response
246
     */
247
    public function getInactiveUsers(Project $project)
248
    {
249
        $users = $project->getNotMembers()->dropdown('fullname');
250
251
        return response()->json($users);
252
    }
253
254
    /**
255
     * Ajax: add user to the project.
256
     *
257
     * @param Project $project
258
     * @param Request $request
259
     *
260
     * @return \Symfony\Component\HttpFoundation\Response
261
     */
262
    public function postAssign(Project $project, Request $request)
263
    {
264
        $status = $project->updater($this->getLoggedUser())->assignUser((int) $request->input('user_id'));
265
266
        return response()->json(['status' => (bool) $status]);
267
    }
268
269
    /**
270
     * Ajax: remove user from the project.
271
     *
272
     * @param Project $project
273
     * @param Request $request
274
     *
275
     * @return \Symfony\Component\HttpFoundation\Response
276
     */
277
    public function postUnassign(Project $project, Request $request)
278
    {
279
        $status = $project->updater($this->getLoggedUser())->unassignUser((int) $request->input('user_id'));
280
281
        return response()->json(['status' => (bool) $status]);
282
    }
283
284
    /**
285
     * To add a new note to the project.
286
     *
287
     * @param Project          $project
288
     * @param Note             $note
289
     * @param FormRequest\Note $request
290
     *
291
     * @return \Illuminate\Http\RedirectResponse
292
     */
293
    public function postAddNote(Project $project, Note $note, FormRequest\Note $request)
294
    {
295
        $note->setRelation('project', $project);
296
        $note->setRelation('createdBy', $this->getLoggedUser());
297
        $note->updater($this->getLoggedUser())->create($request->all());
298
299
        return redirect($note->to())->with('notice', trans('tinyissue.your_note_added'));
300
    }
301
302
    /**
303
     * Ajax: To update project note.
304
     *
305
     * @param Project $project
306
     * @param Note    $note
307
     * @param Request $request
308
     *
309
     * @return \Symfony\Component\HttpFoundation\Response
310
     */
311
    public function postEditNote(Project $project, Note $note, Request $request)
312
    {
313
        $body = '';
314
        if ($request->has('body')) {
315
            $note->setRelation('project', $project);
316
            $note->updater($this->getLoggedUser())->updateBody((string)$request->input('body'));
317
318
            $body = \Html::format($note->body);
319
        }
320
321
        return response()->json(['status' => true, 'text' => $body]);
322
    }
323
324
    /**
325
     * Ajax: to delete a project note.
326
     *
327
     * @param Project $project
328
     * @param Note    $note
329
     *
330
     * @return \Symfony\Component\HttpFoundation\Response
331
     */
332
    public function getDeleteNote(Project $project, Note $note)
333
    {
334
        $note->setRelation('project', $project);
335
        $note->updater($this->getLoggedUser())->delete();
336
337
        return response()->json(['status' => true]);
338
    }
339
340
    /**
341
     * Ajax: generate the issues export file.
342
     *
343
     * @param Project  $project
344
     * @param Exporter $exporter
345
     * @param Request  $request
346
     *
347
     * @return \Symfony\Component\HttpFoundation\Response
348
     */
349
    public function postExportIssues(Project $project, Exporter $exporter, Request $request)
350
    {
351
        // Generate export file
352
        $info = $exporter->exportFile(
353
            'Project\Issue',
354
            $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\Request::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...
355
            $request->all()
356
        );
357
358
        // Download link
359
        $link = link_to(
360
            $project->to('download_export/' . $info['file']),
361
            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<Symfony\Component...on\TranslatorInterface>; 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...
362
            ['class' => 'btn btn-link']
363
        );
364
365
        return response()->json([
366
            'link'  => $link,
367
            'title' => $info['title'],
368
            'file'  => $info['file'],
369
            'ext'   => $info['ext'],
370
        ]);
371
    }
372
373
    /**
374
     * Download and then delete an export file.
375
     *
376
     * @param Project $project
377
     * @param string  $file
378
     *
379
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
380
     */
381
    public function getDownloadExport(Project $project, $file)
382
    {
383
        $this->authorize('export', $project);
384
385
        // Filter out any characters that are not in pattern
386
        $file = preg_replace('/[^a-z0-9\_\.]/mi', '', $file);
387
388
        // Download export
389
        return response()->download(storage_path('exports/' . $file), $file)->deleteFileAfterSend(true);
390
    }
391
}
392