Passed
Push — 5.0.0 ( 9d1037...fbd7bb )
by Fèvre
05:16
created

PostController::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 26
rs 9.7333
c 1
b 0
f 0
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 Xetaravel\Events\Discuss\PostWasDeletedEvent;
11
use Xetaravel\Events\Discuss\PostWasSolvedEvent;
12
use Xetaravel\Models\DiscussConversation;
13
use Xetaravel\Models\DiscussPost;
14
15
class PostController extends Controller
16
{
17
    /**
18
     * Redirect an user to a conversation, page and post.
19
     *
20
     * @param Request $request
21
     * @param int $id The ID of the post.
22
     *
23
     * @return RedirectResponse
24
     */
25
    public function show(Request $request, int $id): RedirectResponse
26
    {
27
        $post = DiscussPost::findOrFail($id);
28
29
        $postsBefore = DiscussPost::where([
30
            ['conversation_id', $post->conversation_id],
31
            ['created_at', '<', $post->created_at]
32
        ])->count();
33
34
        $postsPerPage = config('xetaravel.pagination.discuss.post_per_page');
35
36
        $page = floor($postsBefore / $postsPerPage) + 1;
37
        $page = ($page > 1) ? $page : 1;
38
39
        $request->session()->keep(['primary', 'error', 'warning', 'success', 'info']);
40
41
        return redirect()
42
            ->route(
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

42
            ->/** @scrutinizer ignore-call */ route(

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...
43
                'discuss.conversation.show',
44
                [
45
                    'slug' => $post->conversation->slug,
46
                    'id' => $post->conversation->id,
47
                    'page' => $page,
48
                    '#post-' . $post->getKey()
49
                ]
50
            );
51
    }
52
53
    /**
54
     * Mark as solved.
55
     *
56
     * @param int $id
57
     *
58
     * @return RedirectResponse
59
     */
60
    public function solved(int $id): RedirectResponse
61
    {
62
        $post = DiscussPost::findOrFail($id);
63
64
        $this->authorize('solved', $post->conversation);
65
66
        if ($post->getKey() === $post->conversation->solved_post_id) {
67
            return back()
68
                ->with('danger', 'This post is already the solved post !');
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

68
                ->/** @scrutinizer ignore-call */ with('danger', 'This post is already the solved post !');

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...
69
        }
70
71
        if (!is_null($post->conversation->solved_post_id)) {
72
            return back()
73
                ->with('danger', 'This conversation has already a solved post !');
74
        }
75
        $conversation = DiscussConversation::findOrFail($post->conversation_id);
76
77
        $conversation->solved_post_id = $post->getKey();
78
        $conversation->is_solved = true;
79
        $conversation->save();
80
81
        $post->is_solved = true;
82
        $post->save();
83
84
        event(new PostWasSolvedEvent(Auth::user(), $post));
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $user of Xetaravel\Events\Discuss...vedEvent::__construct() does only seem to accept Xetaravel\Models\User, 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

84
        event(new PostWasSolvedEvent(/** @scrutinizer ignore-type */ Auth::user(), $post));
Loading history...
85
86
        return redirect()
87
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
88
            ->with('success', 'This reply as been marked as solved !');
89
    }
90
}
91