Completed
Pull Request — master (#34)
by Fèvre
14:51 queued 12:29
created

ConversationController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 11
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 23 1
A create() 0 22 2
A update() 0 13 1
A getCurrentPage() 0 4 2
A showCreateForm() 0 8 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Discuss;
3
4
use Illuminate\Http\Request;
5
use Illuminate\View\View;
6
use Xetaio\Mentions\Parser\MentionParser;
7
use Xetaravel\Models\DiscussCategory;
8
use Xetaravel\Models\DiscussConversation;
9
use Xetaravel\Models\DiscussLog;
10
use Xetaravel\Models\Repositories\DiscussConversationRepository;
11
use Xetaravel\Models\Validators\DiscussConversationValidator;
12
13
class ConversationController extends Controller
14
{
15
    /**
16
     * Show the conversation by its id.
17
     *
18
     * @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...
19
     */
20
    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...
21
    {
22
        $conversation = DiscussConversation::findOrFail($id);
23
        $categories = DiscussCategory::pluckLocked('title', 'id');
24
25
        $posts = $conversation->posts()
26
            ->where('id', '!=', $conversation->solved_post_id)
27
            ->where('id', '!=', $conversation->first_post_id)
28
            ->paginate(config('xetaravel.pagination.discuss.post_per_page'));
29
30
        $postsWithLogs = $conversation->getPostsWithLogs(
31
            collect($posts->items()),
32
            $this->getCurrentPage($request)
33
        );
34
35
        $this->breadcrumbs->setListElementClasses('breadcrumbs');
36
        $breadcrumbs = $this->breadcrumbs->addCrumb(e($conversation->title), $conversation->conversation_url);
37
38
        return view(
39
            'Discuss::conversation.show',
40
            compact('conversation', 'posts', 'postsWithLogs', 'breadcrumbs', 'categories')
41
        );
42
    }
43
44
    /**
45
     * Show the create form.
46
     *
47
     * @return \Illuminate\View\View
48
     */
49
    public function showCreateForm(): View
50
    {
51
        $categories = DiscussCategory::pluckLocked('title', 'id');
52
53
        $breadcrumbs = $this->breadcrumbs->addCrumb('Start a discussion', route('discuss.conversation.create'));
54
55
        return view('Discuss::conversation.create', compact('breadcrumbs', 'categories'));
56
    }
57
58
    /**
59
     * Handle a conversation create request for the application.
60
     *
61
     * @param \Illuminate\Http\Request $request
62
     *
63
     * @return \Illuminate\Http\RedirectResponse
64
     */
65
    public function create(Request $request)
66
    {
67
        DiscussConversationValidator::create($request->all())->validate();
68
69
        if (DiscussConversation::isFlooding('xetaravel.flood.discuss.conversation')) {
70
            return back()
71
                ->withInput()
72
                ->with('danger', 'Wow, keep calm bro, and try to not flood !');
73
        }
74
        $conversation = DiscussConversationRepository::create($request->all());
75
        $post = $conversation->firstPost;
76
77
        $parser = new MentionParser($post);
78
        $content = $parser->parse($post->content);
79
80
        $post->content = $content;
81
        $post->save();
82
83
        return redirect()
84
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
85
            ->with('success', 'Your discussion has been created successfully !');
86
    }
87
88
    /**
89
     * Handle a conversation update request for the application.
90
     *
91
     * @param \Illuminate\Http\Request $request
92
     * @param string $slug
93
     * @param int $id
94
     *
95
     * @return \Illuminate\Http\RedirectResponse
96
     */
97
    public function update(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...
98
    {
99
        $conversation = DiscussConversation::findOrFail($id);
100
101
        $this->authorize('update', $conversation);
102
103
        DiscussConversationValidator::update($request->all(), $id)->validate();
104
        $conversation = DiscussConversationRepository::update($request->all(), $conversation);
105
106
        return redirect()
107
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
108
            ->with('success', 'Your conversation has been updated successfully !');
109
    }
110
111
    /**
112
     * Get the current page for the conversation.
113
     *
114
     * @param \Illuminate\Http\Request $request
115
     *
116
     * @return int
117
     */
118
    protected function getCurrentPage(Request $request): int
119
    {
120
        return !is_null($request->get('page')) ? (int)$request->get('page') : 1;
121
    }
122
}
123