Passed
Push — 5.0.0 ( 9d1037...fbd7bb )
by Fèvre
05:16
created

DeletePost::deletePost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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