Passed
Push — 5.0.0 ( b29d18...60ee56 )
by Fèvre
05:16
created

ConversationController::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
nc 2
nop 1
dl 0
loc 28
rs 9.7
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ConversationController::update() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\Discuss;
6
7
use Illuminate\Contracts\View\Factory;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Http\RedirectResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Auth;
13
use Xetaio\Mentions\Parser\MentionParser;
14
use Xetaravel\Events\Discuss\ConversationWasCreatedEvent;
15
use Xetaravel\Models\DiscussCategory;
16
use Xetaravel\Models\DiscussConversation;
17
use Xetaravel\Models\Repositories\DiscussConversationRepository;
18
use Xetaravel\Models\Validators\DiscussConversationValidator;
19
20
class ConversationController extends Controller
21
{
22
    /**
23
     * Get the current page for the conversation.
24
     *
25
     * @param Request $request
26
     *
27
     * @return int
28
     */
29
    protected function getCurrentPage(Request $request): int
30
    {
31
        return !is_null($request->get('page')) ? (int) $request->get('page') : 1;
32
    }
33
    /**
34
     * Show the conversation by its id.
35
     *
36
     * @return Factory|View|Application|\Illuminate\View\View|object
37
     */
38
    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

38
    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...
39
    {
40
        $conversation = DiscussConversation::findOrFail($id);
41
        $categories = DiscussCategory::pluckLocked('title', 'id');
42
43
        $posts = $conversation->posts()
44
            ->with('user')
45
            ->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...
46
            ->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...
47
            ->paginate(config('xetaravel.pagination.discuss.post_per_page'));
48
49
        $postsWithLogs = $conversation->getPostsWithLogs(
50
            collect($posts->items()),
51
            $this->getCurrentPage($request)
52
        );
53
54
        $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...
55
56
        return view(
57
            'Discuss::conversation.show',
58
            compact('conversation', 'posts', 'postsWithLogs', 'breadcrumbs', 'categories')
59
        );
60
    }
61
62
63
    /**
64
     * Handle a conversation update request for the application.
65
     *
66
     * @param Request $request
67
     * @param string $slug The slug of the conversation to update.
68
     * @param int $id The id of the conversation to update.
69
     *
70
     * @return RedirectResponse
71
     */
72
    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

72
    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...
73
    {
74
        $conversation = DiscussConversation::findOrFail($id);
75
76
        $this->authorize('update', $conversation);
77
78
        DiscussConversationValidator::update($request->all(), $id)->validate();
79
        $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

79
        $conversation = DiscussConversationRepository::update($request->all(), /** @scrutinizer ignore-type */ $conversation);
Loading history...
80
81
        return redirect()
82
            ->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

82
            ->/** @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...
83
            ->with('success', 'Your discussion has been updated successfully !');
84
    }
85
86
    /**
87
     * Handle the delete request for a conversation.
88
     *
89
     * @param string $slug The slug of the conversation to delete.
90
     * @param int $id The id of the conversation to delete.
91
     *
92
     * @return RedirectResponse
93
     */
94
    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

94
    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...
95
    {
96
        $conversation = DiscussConversation::findOrFail($id);
97
98
        $this->authorize('delete', $conversation);
99
100
        if ($conversation->delete()) {
101
            return redirect()
102
                ->route('discuss.index')
103
                ->with('success', 'This discussion has been deleted successfully !');
104
        }
105
106
        return back()
107
            ->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

107
            ->/** @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...
108
    }
109
}
110