Passed
Push — 5.0.0 ( 746f48...fc989a )
by Fèvre
06:29
created

ConversationController::showCreateForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\Discuss;
6
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\View\View;
11
use Xetaio\Mentions\Parser\MentionParser;
12
use Xetaravel\Events\Discuss\ConversationWasCreatedEvent;
13
use Xetaravel\Models\DiscussCategory;
14
use Xetaravel\Models\DiscussConversation;
15
use Xetaravel\Models\Repositories\DiscussConversationRepository;
16
use Xetaravel\Models\Validators\DiscussConversationValidator;
17
18
class ConversationController extends Controller
19
{
20
    /**
21
     * Get the current page for the conversation.
22
     *
23
     * @param Request $request
24
     *
25
     * @return int
26
     */
27
    protected function getCurrentPage(Request $request): int
28
    {
29
        return !is_null($request->get('page')) ? (int) $request->get('page') : 1;
30
    }
31
    /**
32
     * Show the conversation by its id.
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36
    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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    public function show(Request $request, /** @scrutinizer ignore-unused */ string $slug, int $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        $conversation = DiscussConversation::findOrFail($id);
39
        $categories = DiscussCategory::pluckLocked('title', 'id');
40
41
        $posts = $conversation->posts()
42
            ->where('id', '!=', $conversation->solved_post_id)
0 ignored issues
show
Bug introduced by
The property solved_post_id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
43
            ->where('id', '!=', $conversation->first_post_id)
0 ignored issues
show
Bug introduced by
The property first_post_id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
44
            ->paginate(config('xetaravel.pagination.discuss.post_per_page'));
45
46
        $postsWithLogs = $conversation->getPostsWithLogs(
47
            collect($posts->items()),
48
            $this->getCurrentPage($request)
49
        );
50
51
        $this->breadcrumbs->setListElementClasses('breadcrumbs');
52
        $breadcrumbs = $this->breadcrumbs->addCrumb(e($conversation->title), $conversation->conversation_url);
0 ignored issues
show
Bug introduced by
The property conversation_url does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug introduced by
The property title does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
53
54
        return view(
55
            'Discuss::conversation.show',
56
            compact('conversation', 'posts', 'postsWithLogs', 'breadcrumbs', 'categories')
57
        );
58
    }
59
60
    /**
61
     * Handle a conversation create request for the application.
62
     *
63
     * @param Request $request
64
     *
65
     * @return RedirectResponse
66
     */
67
    public function create(Request $request)
68
    {
69
        DiscussConversationValidator::create($request->all())->validate();
70
71
        // Users that have the permission "manage.discuss" can bypass this rule. (Default to Administrator)
72
        if (DiscussConversation::isFlooding('xetaravel.flood.discuss.conversation') &&
73
            !Auth::user()->hasPermission('manage.discuss')
0 ignored issues
show
Bug introduced by
The method hasPermission() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            !Auth::user()->/** @scrutinizer ignore-call */ hasPermission('manage.discuss')
Loading history...
74
        ) {
75
            return back()
76
                ->withInput()
0 ignored issues
show
Bug introduced by
The method withInput() does not exist on Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
                ->/** @scrutinizer ignore-call */ withInput()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
                ->with('danger', 'Wow, keep calm bro, and try to not flood !');
78
        }
79
        $conversation = DiscussConversationRepository::create($request->all());
80
        $post = $conversation->firstPost;
81
82
        $parser = new MentionParser($post, [
83
            'regex' => config('mentions.regex')
84
        ]);
85
        $content = $parser->parse($post->content);
86
87
        $post->content = $content;
88
        $post->save();
89
90
        event(new ConversationWasCreatedEvent($conversation, Auth::user()));
0 ignored issues
show
Bug introduced by
Illuminate\Support\Facades\Auth::user() of type Illuminate\Contracts\Auth\Authenticatable|null is incompatible with the type Xetaravel\Models\DiscussConversation expected by parameter $discussConversation of Xetaravel\Events\Discuss...tedEvent::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        event(new ConversationWasCreatedEvent($conversation, /** @scrutinizer ignore-type */ Auth::user()));
Loading history...
91
92
        return redirect()
93
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
0 ignored issues
show
Bug introduced by
The method route() does not exist on Illuminate\Routing\Redirector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            ->/** @scrutinizer ignore-call */ route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
            ->with('success', 'Your discussion has been created successfully !');
95
    }
96
97
    /**
98
     * Handle a conversation update request for the application.
99
     *
100
     * @param Request $request
101
     * @param string $slug The slug of the conversation to update.
102
     * @param int $id The id of the conversation to update.
103
     *
104
     * @return RedirectResponse
105
     */
106
    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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
    public function update(Request $request, /** @scrutinizer ignore-unused */ string $slug, int $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        $conversation = DiscussConversation::findOrFail($id);
109
110
        $this->authorize('update', $conversation);
111
112
        DiscussConversationValidator::update($request->all(), $id)->validate();
113
        $conversation = DiscussConversationRepository::update($request->all(), $conversation);
0 ignored issues
show
Bug introduced by
It seems like $conversation can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $conversation of Xetaravel\Models\Reposit...ionRepository::update() does only seem to accept Xetaravel\Models\DiscussConversation, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        $conversation = DiscussConversationRepository::update($request->all(), /** @scrutinizer ignore-type */ $conversation);
Loading history...
114
115
        return redirect()
116
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
117
            ->with('success', 'Your discussion has been updated successfully !');
118
    }
119
120
    /**
121
     * Handle the delete request for a conversation.
122
     *
123
     * @param string $slug The slug of the conversation to delete.
124
     * @param int $id The id of the conversation to delete.
125
     *
126
     * @return RedirectResponse
127
     */
128
    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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

128
    public function delete(/** @scrutinizer ignore-unused */ string $slug, int $id): RedirectResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
    {
130
        $conversation = DiscussConversation::findOrFail($id);
131
132
        $this->authorize('delete', $conversation);
133
134
        if ($conversation->delete()) {
135
            return redirect()
136
                ->route('discuss.index')
137
                ->with('success', 'This discussion has been deleted successfully !');
138
        }
139
140
        return back()
141
            ->with('danger', 'An error occurred while deleting this discussion !');
0 ignored issues
show
Bug introduced by
The method with() does not exist on Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
            ->/** @scrutinizer ignore-call */ with('danger', 'An error occurred while deleting this discussion !');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
    }
143
}
144