Completed
Pull Request — master (#34)
by Fèvre
19:42 queued 16:30
created

ThreadController::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Discuss;
3
4
use Illuminate\Http\Request;
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\View\View;
7
use Xetaio\Mentions\Parser\MentionParser;
8
use Xetaravel\Models\DiscussCategory;
9
use Xetaravel\Models\DiscussThread;
10
use Xetaravel\Models\DiscussLog;
11
use Xetaravel\Models\Repositories\DiscussThreadRepository;
12
use Xetaravel\Models\Validators\DiscussThreadValidator;
13
14
class ThreadController extends Controller
15
{
16
    /**
17
     * Show the thread by its id.
18
     *
19
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be View|\Illuminate\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
20
     */
21
    public function show(Request $request, string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $slug 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...
22
    {
23
        $thread = DiscussThread::findOrFail($id);
24
25
        $comments = $thread->comments()
26
            ->where('id', '!=', $thread->solved_comment_id)
27
            ->paginate(config('xetaravel.pagination.discuss.comment_per_page'));
28
        $comments->load('user');
29
30
        $commentsWithLogs = $thread->getCommentWithLogs(
31
            collect($comments->items()),
32
            $this->getCurrentPage($request)
33
        );
34
35
        $this->breadcrumbs->setListElementClasses('breadcrumbs');
36
        $breadcrumbs = $this->breadcrumbs->addCrumb(e($thread->title), $thread->thread_url);
37
38
        return view('Discuss::thread.show', compact('thread', 'comments', 'commentsWithLogs', 'breadcrumbs'));
39
    }
40
41
    /**
42
     * Show the create form.
43
     *
44
     * @return \Illuminate\View\View
45
     */
46 View Code Duplication
    public function showCreateForm(): View
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...
47
    {
48
        $categories = DiscussCategory::pluck('title', 'id');
49
50
        $breadcrumbs = $this->breadcrumbs->addCrumb('Start a discussion', route('discuss.thread.create'));
51
52
        return view('Discuss::thread.create', compact('breadcrumbs', 'categories'));
53
    }
54
55
    /**
56
     * Handle a thread create request for the application.
57
     *
58
     * @param \Illuminate\Http\Request $request
59
     *
60
     * @return \Illuminate\Http\RedirectResponse
61
     */
62
    public function create(Request $request)
63
    {
64
        DiscussThreadValidator::create($request->all())->validate();
65
66
        if (DiscussThread::isFlooding(Auth::user(), 'xetaravel.flood.discuss.thread')) {
67
            return back()
68
                ->withInput()
69
                ->with('danger', 'Wow, keep calm bro, and try to not flood !');
70
        }
71
        $thread = DiscussThreadRepository::create($request->all());
72
73
        $parser = new MentionParser($thread);
74
        $content = $parser->parse($thread->content);
75
76
        $thread->content = $content;
77
        $thread->save();
78
79
        return redirect()
80
            ->route('discuss.thread.show', ['slug' => $thread->slug, 'id' => $thread->getKey()])
81
            ->with('success', 'Your discussion has been created successfully !');
82
    }
83
84
    /**
85
     * Get the current page for the thread.
86
     *
87
     * @param \Illuminate\Http\Request $request
88
     *
89
     * @return int
90
     */
91
    protected function getCurrentPage(Request $request)
92
    {
93
        return !is_null($request->get('page')) ? (int)$request->get('page') : 1;
94
    }
95
}
96