Passed
Push — 5.0.0 ( c0ce95...10c169 )
by Fèvre
06:34
created

CreatePost::updatePostContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Discuss;
6
7
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
8
use Illuminate\Support\Facades\Auth;
9
use Livewire\Attributes\On;
10
use Livewire\Component;
11
use Masmerise\Toaster\Toastable;
12
use Xetaravel\Events\Discuss\PostWasCreatedEvent;
13
use Xetaravel\Livewire\Forms\DiscussPostForm;
14
use Xetaravel\Models\DiscussConversation;
15
use Xetaravel\Models\DiscussPost;
16
17
class CreatePost extends Component
18
{
19
    use AuthorizesRequests;
20
    use Toastable;
21
22
    /**
23
     * The form used to create/update a model.
24
     *
25
     * @var DiscussPostForm
26
     */
27
    public DiscussPostForm $form;
28
29
    public function mount(DiscussConversation $discussConversation): void
30
    {
31
        $this->form->conversation_id = $discussConversation->getKey();
32
        $this->form->is_pinned = $discussConversation->is_pinned;
33
    }
34
35
    /**
36
     * Create a blank model and assign it to the model. (Used in create modal)
37
     *
38
     * @return void
39
     */
40
    public function create(): void
41
    {
42
        $this->authorize('create', DiscussPost::class);
43
44
        $this->validate();
45
46
        // Users that have the permission "manage discuss conversation" can bypass this rule. (Default to Developer)
47
        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

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

54
        event(new PostWasCreatedEvent(/** @scrutinizer ignore-type */ Auth::user(), $discussPost));
Loading history...
55
56
        redirect()
57
            ->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

57
            ->/** @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...
58
            ->success('Your reply has been posted successfully !');
59
    }
60
61
62
    public function render()
63
    {
64
        return view('livewire.discuss.create-post');
65
    }
66
67
    #[On('post-reply')]
68
    public function updatePostContent($content): void
69
    {
70
        $this->form->content = $content . $this->form->content;
71
    }
72
}
73