RepliesController::indexWebViewByUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
namespace PHPHub\Http\ApiControllers;
4
5
use Dingo\Api\Exception\StoreResourceFailedException;
6
use PHPHub\Repositories\ReplyRepositoryInterface;
7
use PHPHub\Transformers\ReplyTransformer;
8
use PHPHub\User;
9
use Illuminate\Http\Request;
10
use Prettus\Validator\Exceptions\ValidatorException;
11
12
class RepliesController extends Controller
13
{
14
    /**
15
     * @var ReplyRepositoryInterface
16
     */
17
    private $replies;
18
19
    /**
20
     * TopicController constructor.
21
     *
22
     * @param ReplyRepositoryInterface $repository
23
     */
24
    public function __construct(ReplyRepositoryInterface $repository)
25
    {
26
        $this->replies = $repository;
27
    }
28
29
    /**
30
     * 获取指定帖子的回复.
31
     *
32
     * @param $topic_id
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36 View Code Duplication
    public function indexByTopicId($topic_id)
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...
37
    {
38
        $this->replies->addAvailableInclude('user', ['name', 'avatar']);
39
40
        $data = $this->replies
41
            ->byTopicId($topic_id)
42
            ->autoWith()
43
            ->autoWithRootColumns(['id', 'vote_count', 'created_at'])
44
            ->paginate(per_page());
45
46
        return $this->response()->paginator($data, new ReplyTransformer());
47
    }
48
49
    /**
50
     * 获取指定用户的回复.
51
     *
52
     * @param $user_id
53
     *
54
     * @return \Dingo\Api\Http\Response
55
     */
56 View Code Duplication
    public function indexByUserId($user_id)
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...
57
    {
58
        $this->replies->addAvailableInclude('user', ['name', 'avatar']);
59
60
        $data = $this->replies
61
            ->byUserId($user_id)
62
            ->autoWith()
63
            ->autoWithRootColumns(['id', 'vote_count'])
64
            ->paginate(per_page());
65
66
        return $this->response()->paginator($data, new ReplyTransformer());
67
    }
68
69
    /**
70
     * 发布一条新回复.
71
     *
72
     * @param \Illuminate\Http\Request $request
73
     *
74
     * @return \Illuminate\Http\Response
75
     */
76 View Code Duplication
    public function store(Request $request)
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...
77
    {
78
        try {
79
            $reply = $this->replies->store($request->all());
80
81
            return $this->response()->item($reply, new ReplyTransformer());
82
        } catch (ValidatorException $e) {
83
            throw new StoreResourceFailedException('Could not create new topic.', $e->getMessageBag()->all());
84
        }
85
    }
86
87
    /**
88
     * 更新回复.
89
     *
90
     * @param \Illuminate\Http\Request $request
91
     * @param int                      $id
92
     *
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $id 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...
96
    {
97
        //
98
    }
99
100
    /**
101
     * 删除一条回复.
102
     *
103
     * @param int $id
104
     *
105
     * @return \Illuminate\Http\Response
106
     */
107
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
108
    {
109
        //
110
    }
111
112
    /**
113
     * 指定帖子下评论列表的 Web View.
114
     *
115
     * @param $topic_id
116
     *
117
     * @return \Illuminate\View\View
118
     */
119
    public function indexWebViewByTopic($topic_id)
120
    {
121
        $replies = $this->replies
122
            ->byTopicId($topic_id)
123
            ->withOnly('user', ['id', 'name', 'avatar'])
124
            ->all(['id', 'body', 'created_at', 'user_id']);
125
126
        // 楼层计数
127
        $count = 1;
128
129
        return view('api_web_views.replies_list', compact('replies', 'count'));
0 ignored issues
show
Bug Compatibility introduced by
The expression view('api_web_views.repl...t('replies', 'count')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 129 which is incompatible with the return type documented by PHPHub\Http\ApiControlle...er::indexWebViewByTopic of type Illuminate\View\View.
Loading history...
130
    }
131
132
    /**
133
     * 指定帖子下评论列表的 Web View.
134
     *
135
     * @param $user_id
136
     *
137
     * @return \Illuminate\View\View
138
     */
139
    public function indexWebViewByUser($user_id)
140
    {
141
        $replies = $this->replies
142
            ->byUserId($user_id)
143
            ->withOnly('topic.user', ['avatar'])
144
            ->all(['id', 'body', 'created_at', 'topic_id']);
145
146
        return view('api_web_views.users_replies_list', compact('replies', 'count'));
0 ignored issues
show
Bug Compatibility introduced by
The expression view('api_web_views.user...t('replies', 'count')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 146 which is incompatible with the return type documented by PHPHub\Http\ApiControlle...ler::indexWebViewByUser of type Illuminate\View\View.
Loading history...
147
    }
148
}
149