Completed
Pull Request — master (#34)
by Fèvre
18:57 queued 15:50
created

PostController::create()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Discuss;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Xetaio\Mentions\Parser\MentionParser;
8
use Xetaravel\Models\DiscussConversation;
9
use Xetaravel\Models\DiscussPost;
10
use Xetaravel\Models\Repositories\DiscussPostRepository;
11
use Xetaravel\Models\Repositories\DiscussUserRepository;
12
use Xetaravel\Models\User;
13
use Xetaravel\Models\Validators\DiscussPostValidator;
14
15
class PostController extends Controller
16
{
17
    /**
18
     * Create a post for a conversation.
19
     *
20
     * @param \Illuminate\Http\Request $request
21
     *
22
     * @return \Illuminate\Http\RedirectResponse
23
     */
24
    public function create(Request $request): RedirectResponse
25
    {
26
        $conversation = DiscussConversation::findOrFail($request->conversation_id);
0 ignored issues
show
Unused Code introduced by
$conversation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
28
        if (DiscussPost::isFlooding('xetaravel.flood.discuss.post')) {
29
            return back()
30
                ->withInput()
31
                ->with('danger', 'Wow, keep calm bro, and try to not flood !');
32
        }
33
34
        DiscussPostValidator::create($request->all())->validate();
35
        $post = DiscussPostRepository::create($request->all());
36
        $user = DiscussUserRepository::create($request->all());
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
38
        $parser = new MentionParser($post);
39
        $content = $parser->parse($post->content);
40
41
        $post->content = $content;
42
        $post->save();
43
44
        return redirect()
45
            ->route('discuss.post.show', ['id' => $post->getKey()])
46
            ->with('success', 'Your reply has been posted successfully !');
47
    }
48
49
    /**
50
     * Redirect an user to a conversation, page and post.
51
     *
52
     * @param \Illuminate\Http\Request $request
53
     * @param int $id The ID of the post.
54
     *
55
     * @return \Illuminate\Http\RedirectResponse
56
     */
57 View Code Duplication
    public function show(Request $request, int $id): RedirectResponse
0 ignored issues
show
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...
58
    {
59
        $post = DiscussPost::findOrFail($id);
60
61
        $postsBefore = DiscussPost::where([
62
            ['conversation_id', $post->conversation_id],
63
            ['created_at', '<', $post->created_at]
64
        ])->count();
65
66
        $postsPerPage = config('xetaravel.pagination.discuss.post_per_page');
67
68
        $page = floor($postsBefore / $postsPerPage) + 1;
69
        $page = ($page > 1) ? $page : 1;
70
71
        $request->session()->keep(['primary', 'danger', 'warning', 'success', 'info']);
0 ignored issues
show
Bug introduced by
The method keep() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

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...
72
73
        return redirect()
74
            ->route(
75
                'discuss.conversation.show',
76
                [
77
                    'slug' => $post->conversation->slug,
78
                    'id' => $post->conversation->id,
79
                    'page' => $page,
80
                    '#post-' . $post->getKey()
81
                ]
82
            );
83
    }
84
85
    /**
86
     * Mark as solved.
87
     *
88
     * @param \Illuminate\Http\Request $request
89
     * @param int $id
90
     *
91
     * @return \Illuminate\Http\RedirectResponse
92
     */
93
    public function solved(Request $request, int $id): RedirectResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
94
    {
95
        $post = DiscussPost::findOrFail($id);
96
97
        if ($post->getKey() == $post->conversation->solved_post_id) {
98
            return back()
99
                ->with('danger', 'This post is already the solved post !');
100
        }
101
102
        if (!is_null($post->conversation->solved_post_id)) {
103
            return back()
104
                ->with('danger', 'This conversation has already a solved post !');
105
        }
106
        $conversation = DiscussConversation::findOrFail($post->conversation_id);
107
108
        $conversation->solved_post_id = $post->getKey();
109
        $conversation->is_solved = true;
110
        $conversation->save();
111
112
        return redirect()
113
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
114
            ->with('success', 'This reply as been marked as solved !');
115
    }
116
}
117