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

CreatePost::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.9332
1
<?php
2
3
namespace Xetaravel\Livewire\Discuss;
4
5
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6
use Illuminate\Support\Facades\Auth;
7
use Livewire\Component;
8
use Masmerise\Toaster\Toastable;
9
use Xetaravel\Events\Discuss\PostWasCreatedEvent;
10
use Xetaravel\Livewire\Forms\DiscussPostForm;
11
use Xetaravel\Models\DiscussConversation;
12
use Xetaravel\Models\DiscussPost;
13
14
class CreatePost extends Component
15
{
16
    use AuthorizesRequests;
17
    use Toastable;
18
19
    /**
20
     * The form used to create/update a model.
21
     *
22
     * @var DiscussPostForm
23
     */
24
    public DiscussPostForm $form;
25
26
    public function mount(DiscussConversation $discussConversation): void
27
    {
28
        $this->form->conversation_id = $discussConversation->getKey();
29
    }
30
31
    /**
32
     * Create a blank model and assign it to the model. (Used in create modal)
33
     *
34
     * @return void
35
     */
36
    public function create(): void
37
    {
38
        $this->authorize('create', DiscussPost::class);
39
40
        $this->validate();
41
42
        // Users that have the permission "manage.discuss" can bypass this rule. (Default to Administrator)
43
        if (DiscussPost::isFlooding('xetaravel.flood.discuss.post') && !Auth::user()->hasPermissionTo('manage discuss conversation')) {
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        if (DiscussPost::isFlooding('xetaravel.flood.discuss.post') && !Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('manage discuss conversation')) {
Loading history...
44
            $this->error('Wow, keep calm bro, and try to not flood !');
45
46
            return;
47
        }
48
        $discussPost = $this->form->store();
49
50
        event(new PostWasCreatedEvent(Auth::user(), $discussPost));
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...tedEvent::__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

50
        event(new PostWasCreatedEvent(/** @scrutinizer ignore-type */ Auth::user(), $discussPost));
Loading history...
51
52
        redirect()
53
            ->route('discuss.post.show', ['id' => $discussPost->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

53
            ->/** @scrutinizer ignore-call */ route('discuss.post.show', ['id' => $discussPost->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...
54
            ->success('Your reply has been posted successfully !');
55
    }
56
57
58
    public function render()
59
    {
60
        return view('livewire.discuss.create-post');
61
    }
62
}
63