These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 Illuminate\Contracts\Auth\Guard; |
||
19 | use Illuminate\Database\QueryException; |
||
20 | use Illuminate\Foundation\Bus\DispatchesJobs; |
||
21 | use Illuminate\Http\Request; |
||
22 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
||
23 | |||
24 | class CommentController extends AbstractApiController |
||
25 | { |
||
26 | use DispatchesJobs; |
||
27 | |||
28 | /** |
||
29 | * Get all comments. |
||
30 | * |
||
31 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
32 | * @param \Illuminate\Contracts\Auth\Guard $auth |
||
33 | * |
||
34 | * @return \Illuminate\Http\JsonResponse |
||
35 | */ |
||
36 | public function getComments(Request $request, Guard $auth) |
||
37 | { |
||
38 | $comments = Comment::paginate($request->get('per_page', 20)); |
||
39 | |||
40 | return $this->paginator($comments, $request); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get a single comment. |
||
45 | * |
||
46 | * @param \Gitamin\Models\Comment $comment |
||
47 | * |
||
48 | * @return \Illuminate\Http\JsonResponse |
||
49 | */ |
||
50 | public function getComment(Comment $comment) |
||
51 | { |
||
52 | return $this->item($comment); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Create a new comment. |
||
57 | * |
||
58 | * @param \Illuminate\Contracts\Auth\Guard $auth |
||
59 | * |
||
60 | * @return \Illuminate\Http\JsonResponse |
||
61 | */ |
||
62 | View Code Duplication | public function postComments(Request $request, Guard $auth) |
|
0 ignored issues
–
show
|
|||
63 | { |
||
64 | try { |
||
65 | $comment = $this->dispatch(new AddCommentCommand( |
||
66 | $request->get('message'), |
||
67 | $request->get('target_type'), |
||
68 | $request->get('target_id'), |
||
69 | $request->get('author_id'), |
||
70 | $request->get('project_id') |
||
71 | )); |
||
72 | } catch (QueryException $e) { |
||
73 | throw new BadRequestHttpException(); |
||
74 | } |
||
75 | |||
76 | return $this->item($comment); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Update an existing comment. |
||
81 | * |
||
82 | * @param \Gitamin\Models\Inicdent $comment |
||
83 | * |
||
84 | * @return \Illuminate\Http\JsonResponse |
||
85 | */ |
||
86 | View Code Duplication | public function putComment(Request $request, Comment $comment) |
|
87 | { |
||
88 | try { |
||
89 | $comment = $this->dispatch(new UpdateCommentCommand( |
||
90 | $comment, |
||
91 | $request->get('message') |
||
92 | )); |
||
93 | } catch (QueryException $e) { |
||
94 | throw new BadRequestHttpException(); |
||
95 | } |
||
96 | |||
97 | return $this->item($comment); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Delete an existing comment. |
||
102 | * |
||
103 | * @param \Gitamin\Models\Comment $comment |
||
104 | * |
||
105 | * @return \Illuminate\Http\JsonResponse |
||
106 | */ |
||
107 | public function deleteComment(Comment $comment) |
||
108 | { |
||
109 | $this->dispatch(new RemoveCommentCommand($comment)); |
||
110 | |||
111 | return $this->noContent(); |
||
112 | } |
||
113 | } |
||
114 |
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.