Completed
Pull Request — master (#45)
by Fèvre
08:12 queued 03:05
created

ConversationController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
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 View Code Duplication
    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...
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...
121
    {
122
        $conversation = DiscussConversation::findOrFail($id);
123
124
        if ($conversation->delete()) {
125
            return redirect()
126
                ->route('discuss.index')
127
                ->with('success', 'This discussion has been deleted successfully !');
128
        }
129
130
        return back()
131
            ->with('danger', 'An error occurred while deleting this discussion !');
132
    }
133
134
    /**
135
     * Get the current page for the conversation.
136
     *
137
     * @param \Illuminate\Http\Request $request
138
     *
139
     * @return int
140
     */
141
    protected function getCurrentPage(Request $request): int
142
    {
143
        return !is_null($request->get('page')) ? (int)$request->get('page') : 1;
144
    }
145
}
146