PostController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 74
rs 10
c 2
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 24 2
A solved() 0 29 3
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\PostWasSolvedEvent;
11
use Xetaravel\Models\DiscussConversation;
12
use Xetaravel\Models\DiscussPost;
13
14
class PostController extends Controller
15
{
16
    /**
17
     * Redirect an user to a conversation, page and post.
18
     *
19
     * @param Request $request
20
     * @param int $id The ID of the post.
21
     *
22
     * @return RedirectResponse
23
     */
24
    public function show(Request $request, int $id): RedirectResponse
25
    {
26
        $post = DiscussPost::findOrFail($id);
27
28
        $postsBefore = DiscussPost::where([
29
            ['conversation_id', $post->conversation_id],
0 ignored issues
show
Bug introduced by
The property conversation_id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
30
            ['created_at', '<', $post->created_at]
0 ignored issues
show
Bug introduced by
The property created_at does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
31
        ])->count();
32
33
        $postsPerPage = config('xetaravel.pagination.discuss.post_per_page');
34
35
        $page = floor($postsBefore / $postsPerPage) + 1;
36
        $page = ($page > 1) ? $page : 1;
37
38
        $request->session()->keep(['primary', 'error', 'warning', 'success', 'info']);
39
40
        return redirect()
41
            ->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

41
            ->/** @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...
42
                'discuss.conversation.show',
43
                [
44
                    'slug' => $post->conversation->slug,
0 ignored issues
show
Bug introduced by
The property conversation does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
45
                    'id' => $post->conversation->id,
46
                    'page' => $page,
47
                    '#post-' . $post->getKey()
48
                ]
49
            );
50
    }
51
52
    /**
53
     * Mark as solved.
54
     *
55
     * @param int $id
56
     *
57
     * @return RedirectResponse
58
     */
59
    public function solved(int $id): RedirectResponse
60
    {
61
        $post = DiscussPost::findOrFail($id);
62
63
        $this->authorize('solved', $post->conversation);
0 ignored issues
show
Bug introduced by
The property conversation does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
64
65
        if ($post->getKey() === $post->conversation->solved_post_id) {
66
            return back()
67
                ->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

67
                ->/** @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...
68
        }
69
70
        if (!is_null($post->conversation->solved_post_id)) {
0 ignored issues
show
introduced by
The condition is_null($post->conversation->solved_post_id) is always false.
Loading history...
71
            return back()
72
                ->with('danger', 'This conversation has already a solved post !');
73
        }
74
        $conversation = DiscussConversation::findOrFail($post->conversation_id);
75
76
        $conversation->solved_post_id = $post->getKey();
77
        $conversation->is_solved = true;
78
        $conversation->save();
79
80
        $post->is_solved = true;
81
        $post->save();
82
83
        event(new PostWasSolvedEvent(Auth::user(), $post));
84
85
        return redirect()
86
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
87
            ->with('success', 'This reply as been marked as solved !');
88
    }
89
}
90