Issues (264)

app/Livewire/Discuss/DeletePost.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Discuss;
6
7
use Illuminate\Contracts\View\Factory;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
11
use Illuminate\Http\RedirectResponse;
12
use Illuminate\Support\Facades\DB;
13
use Livewire\Attributes\On;
14
use Livewire\Component;
15
use Livewire\Features\SupportRedirects\Redirector;
16
use Masmerise\Toaster\Toastable;
17
use Throwable;
18
use Xetaravel\Events\Discuss\PostWasDeletedEvent;
19
use Xetaravel\Models\DiscussPost;
20
21
class DeletePost extends Component
22
{
23
    use AuthorizesRequests;
24
    use Toastable;
25
26
    /**
27
     * The form used to create/update a model.
28
     *
29
     * @var DiscussPost
30
     */
31
    public DiscussPost $discussPost;
32
33
    /**
34
     * Used to show the Edit/Create modal.
35
     *
36
     * @var bool
37
     */
38
    public bool $showModal = false;
39
40
    public function render(): Factory|Application|View|\Illuminate\View\View
41
    {
42
        return view('livewire.discuss.delete-post');
43
    }
44
45
    /**
46
     * Show the confirmation modal to delete a post.
47
     *
48
     * @param DiscussPost $discussPost
49
     *
50
     * @return void
51
     */
52
    #[On('delete-post')]
53
    public function deletePost(DiscussPost $discussPost): void
54
    {
55
        $this->authorize('delete', $discussPost);
56
57
        $this->discussPost = $discussPost;
58
59
        $this->showModal = true;
60
    }
61
62
    /**
63
     * Delete a conversation.
64
     *
65
     * @return RedirectResponse|Redirector|null
66
     *
67
     * @throws Throwable
68
     */
69
    public function delete(): RedirectResponse|Redirector|null
70
    {
71
        $this->authorize('delete', $this->discussPost);
72
73
        if ($this->discussPost->conversation->first_post_id === $this->discussPost->getKey()) {
74
            $this->showModal = false;
75
            $this->error('You can not delete the first post of a discussion !');
76
77
            return null;
78
        }
79
80
        $result = DB::transaction(function () {
81
            return $this->discussPost->delete();
82
        });
83
84
        if ($result) {
85
            event(new PostWasDeletedEvent($this->discussPost->conversation, $this->discussPost->user));
86
87
            return redirect()
88
                ->route(
0 ignored issues
show
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

88
                ->/** @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...
89
                    'discuss.conversation.show',
90
                    ['id' => $this->discussPost->conversation->getKey(), 'slug' => $this->discussPost->conversation->slug]
91
                )
92
                ->success('This post has been deleted successfully !');
93
        }
94
95
        return redirect()
96
            ->route('discuss.post.show', ['id' => $this->discussPost->getKey()])
97
            ->error('An error occurred while deleting this post !');
98
    }
99
}
100