Completed
Pull Request — master (#45)
by Fèvre
07:37
created

ConversationController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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