Completed
Push — master ( 28f331...347733 )
by Phecho
03:40
created

CommentController::putComment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4286
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 * 
6
 * Copyright (C) 2015-2016 The Gitamin Team
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 Gitamin\Http\Controllers\Api;
13
14
use Gitamin\Commands\Comment\AddCommentCommand;
15
use Gitamin\Commands\Comment\RemoveCommentCommand;
16
use Gitamin\Commands\Comment\UpdateCommentCommand;
17
use Gitamin\Models\Comment;
18
use GrahamCampbell\Binput\Facades\Binput;
19
use Illuminate\Contracts\Auth\Guard;
20
use Illuminate\Database\QueryException;
21
use Illuminate\Foundation\Bus\DispatchesJobs;
22
use Illuminate\Http\Request;
23
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
24
25
class CommentController extends AbstractApiController
26
{
27
    use DispatchesJobs;
28
29
    /**
30
     * Get all comments.
31
     *
32
     * @param \Symfony\Component\HttpFoundation\Request $request
33
     * @param \Illuminate\Contracts\Auth\Guard          $auth
34
     *
35
     * @return \Illuminate\Http\JsonResponse
36
     */
37
    public function getComments(Request $request, Guard $auth)
0 ignored issues
show
Unused Code introduced by
The parameter $auth is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        $comments = Comment::paginate(Binput::get('per_page', 20));
40
41
        return $this->paginator($comments, $request);
42
    }
43
44
    /**
45
     * Get a single comment.
46
     *
47
     * @param \Gitamin\Models\Comment $comment
48
     *
49
     * @return \Illuminate\Http\JsonResponse
50
     */
51
    public function getComment(Comment $comment)
52
    {
53
        return $this->item($comment);
54
    }
55
56
    /**
57
     * Create a new comment.
58
     *
59
     * @param \Illuminate\Contracts\Auth\Guard $auth
60
     *
61
     * @return \Illuminate\Http\JsonResponse
62
     */
63
    public function postComments(Guard $auth)
0 ignored issues
show
Unused Code introduced by
The parameter $auth is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        try {
66
            $comment = $this->dispatch(new AddCommentCommand(
67
                Binput::get('message'),
68
                Binput::get('target_type'),
69
                Binput::get('target_id'),
70
                Binput::get('author_id'),
71
                Binput::get('project_id')
72
            ));
73
        } catch (QueryException $e) {
74
            throw new BadRequestHttpException();
75
        }
76
77
        return $this->item($comment);
78
    }
79
80
    /**
81
     * Update an existing comment.
82
     *
83
     * @param \Gitamin\Models\Inicdent $comment
84
     *
85
     * @return \Illuminate\Http\JsonResponse
86
     */
87 View Code Duplication
    public function putComment(Comment $comment)
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...
88
    {
89
        try {
90
            $comment = $this->dispatch(new UpdateCommentCommand(
91
                $comment,
92
                Binput::get('message')
93
            ));
94
        } catch (QueryException $e) {
95
            throw new BadRequestHttpException();
96
        }
97
98
        return $this->item($comment);
99
    }
100
101
    /**
102
     * Delete an existing comment.
103
     *
104
     * @param \Gitamin\Models\Comment $comment
105
     *
106
     * @return \Illuminate\Http\JsonResponse
107
     */
108
    public function deleteComment(Comment $comment)
109
    {
110
        $this->dispatch(new RemoveCommentCommand($comment));
111
112
        return $this->noContent();
113
    }
114
}
115