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

PostController::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
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 28
rs 9.7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\Discuss;
6
7
use Carbon\Carbon;
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Auth;
11
use Xetaio\Mentions\Parser\MentionParser;
12
use Xetaravel\Events\Discuss\PostWasDeletedEvent;
13
use Xetaravel\Events\Discuss\PostWasSolvedEvent;
14
use Xetaravel\Models\DiscussConversation;
15
use Xetaravel\Models\DiscussPost;
16
use Xetaravel\Models\Validators\DiscussPostValidator;
17
18
class PostController extends Controller
19
{
20
21
    /**
22
     * Redirect an user to a conversation, page and post.
23
     *
24
     * @param Request $request
25
     * @param int $id The ID of the post.
26
     *
27
     * @return RedirectResponse
28
     */
29
    public function show(Request $request, int $id): RedirectResponse
30
    {
31
        $post = DiscussPost::findOrFail($id);
32
33
        $postsBefore = DiscussPost::where([
34
            ['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...
35
            ['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...
36
        ])->count();
37
38
        $postsPerPage = config('xetaravel.pagination.discuss.post_per_page');
39
40
        $page = floor($postsBefore / $postsPerPage) + 1;
41
        $page = ($page > 1) ? $page : 1;
42
43
        $request->session()->keep(['primary', 'danger', 'warning', 'success', 'info']);
44
45
        return redirect()
46
            ->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

46
            ->/** @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...
47
                'discuss.conversation.show',
48
                [
49
                    '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...
50
                    'id' => $post->conversation->id,
51
                    'page' => $page,
52
                    '#post-' . $post->getKey()
53
                ]
54
            );
55
    }
56
57
    /**
58
     * Handle a delete action for the post.
59
     *
60
     * @param Request $request
61
     * @param int $id
62
     *
63
     * @return RedirectResponse
64
     */
65
    public function delete(int $id): RedirectResponse
66
    {
67
        $post = DiscussPost::findOrFail($id);
68
69
        $this->authorize('delete', $post);
70
71
        if ($post->conversation->first_post_id === $post->getKey()) {
0 ignored issues
show
Bug introduced by
The property conversation does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
72
            return redirect()
73
                ->route('discuss.post.show', ['id' => $post->getKey()])
74
                ->with('danger', 'You can not delete the first post of a discussion !');
75
        }
76
77
        if ($post->delete()) {
78
            event(new PostWasDeletedEvent($post->conversation, $post->user));
0 ignored issues
show
Bug introduced by
The property user does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
79
80
            return redirect()
81
                ->route(
82
                    'discuss.conversation.show',
83
                    ['id' => $post->conversation->getKey(), 'slug' => $post->conversation->slug]
84
                )
85
                ->with('success', 'This post has been deleted successfully !');
86
        }
87
88
        return redirect()
89
            ->route('discuss.post.show', ['id' => $post->getKey()])
90
            ->with('danger', 'An error occurred while deleting this post !');
91
    }
92
93
    /**
94
     * Mark as solved.
95
     *
96
     * @param int $id
97
     *
98
     * @return RedirectResponse
99
     */
100
    public function solved(int $id): RedirectResponse
101
    {
102
        $post = DiscussPost::findOrFail($id);
103
104
        $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...
105
106
        if ($post->getKey() === $post->conversation->solved_post_id) {
107
            return back()
108
                ->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

108
                ->/** @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...
109
        }
110
111
        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...
112
            return back()
113
                ->with('danger', 'This conversation has already a solved post !');
114
        }
115
        $conversation = DiscussConversation::findOrFail($post->conversation_id);
116
117
        $conversation->solved_post_id = $post->getKey();
118
        $conversation->is_solved = true;
119
        $conversation->save();
120
121
        $post->is_solved = true;
122
        $post->save();
123
124
        event(new PostWasSolvedEvent(Auth::user(), $post));
125
126
        return redirect()
127
            ->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
128
            ->with('success', 'This reply as been marked as solved !');
129
    }
130
131
    /**
132
     * Handle an edit action for the post.
133
     *
134
     * @param Request $request
135
     * @param int $id The id of the post to edit.
136
     *
137
     * @return RedirectResponse
138
     */
139
    public function edit(Request $request, int $id): RedirectResponse
140
    {
141
        $post = DiscussPost::findOrFail($id);
142
143
        if (!Auth::user()->can('update', $post)) {
144
            return back()
145
                ->with('danger', 'You\'re not authorized to edit this message.');
146
        }
147
148
        DiscussPostValidator::edit($request->all())->validate();
149
150
        $parser = new MentionParser($post, [
0 ignored issues
show
Bug introduced by
It seems like $post can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $model of Xetaio\Mentions\Parser\M...onParser::__construct() does only seem to accept Illuminate\Database\Eloquent\Model, 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

150
        $parser = new MentionParser(/** @scrutinizer ignore-type */ $post, [
Loading history...
151
            'regex' => config('mentions.regex')
152
        ]);
153
        $content = $parser->parse($request->input('content'));
154
155
        $post->content = $content;
0 ignored issues
show
Bug introduced by
The property content does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
156
        $post->is_edited = true;
0 ignored issues
show
Bug introduced by
The property is_edited does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
157
        $post->edit_count++;
0 ignored issues
show
Bug introduced by
The property edit_count does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
158
        $post->edited_user_id = Auth::id();
0 ignored issues
show
Bug introduced by
The property edited_user_id does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
159
        $post->edited_at = Carbon::now();
0 ignored issues
show
Bug introduced by
The property edited_at does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
160
        $post->save();
161
162
        return redirect()
163
            ->route('discuss.post.show', ['id' => $id])
164
            ->with('success', 'Your post has been edited successfully !');
165
    }
166
167
    /**
168
     * Get the edit json template.
169
     *
170
     * @param int $id
171
     *
172
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
173
     */
174
    public function editTemplate(int $id)
175
    {
176
        $post = DiscussPost::find($id);
177
178
        if (!Auth::user()->can('update', $post) || !$post) {
179
            return response()->json([
180
                'error' => true,
181
                'message' => 'You\'re not authorized to edit this message or this message has been deleted.'
182
            ]);
183
        }
184
185
        return response(
186
            view('Discuss::post.editTemplate', ['post' => $post]),
187
            200,
188
            ['Content-Type' => 'application/json']
189
        );
190
    }
191
}
192