Passed
Push — tailwind ( b7f4de...80ea7d )
by Fèvre
04:55
created

Articles::fireFlash()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Xetaravel\Http\Livewire\Admin\Blog;
4
5
use Illuminate\Contracts\Database\Query\Builder;
6
use Illuminate\Contracts\View\View;
7
use Illuminate\Pagination\LengthAwarePaginator;
8
use Illuminate\Support\Str;
9
use Livewire\Component;
10
use Livewire\WithPagination;
11
use Xetaravel\Http\Livewire\Traits\WithCachedRows;
12
use Xetaravel\Http\Livewire\Traits\WithSorting;
13
use Xetaravel\Http\Livewire\Traits\WithBulkActions;
14
use Xetaravel\Http\Livewire\Traits\WithPerPagePagination;
15
use Xetaravel\Models\Article;
16
17
class Articles extends Component
18
{
19
    use WithPagination;
0 ignored issues
show
Bug introduced by
The trait Livewire\WithPagination requires the property $paginationTheme which is not provided by Xetaravel\Http\Livewire\Admin\Blog\Articles.
Loading history...
20
    use WithSorting;
21
    use WithCachedRows;
22
    use WithBulkActions;
0 ignored issues
show
introduced by
The trait Xetaravel\Http\Livewire\Traits\WithBulkActions requires some properties which are not provided by Xetaravel\Http\Livewire\Admin\Blog\Articles: $selectedRowsQuery, $rows, $rowsQuery
Loading history...
23
    use WithPerPagePagination;
0 ignored issues
show
Bug introduced by
The trait Xetaravel\Http\Livewire\...s\WithPerPagePagination requires the property $paginationTheme which is not provided by Xetaravel\Http\Livewire\Admin\Blog\Articles.
Loading history...
24
25
    /**
26
     * The string to search.
27
     *
28
     * @var string
29
     */
30
    public string $search = '';
31
32
    /**
33
     * Used to update in URL the query string.
34
     *
35
     * @var string[]
36
     */
37
    protected $queryString = [
38
        'sortField' => ['as' => 'f'],
39
        'sortDirection' => ['as' => 'd'],
40
        'search' => ['except' => '', 'as' => 's']
41
    ];
42
43
    /**
44
     * Used to show the delete modal.
45
     *
46
     * @var bool
47
     */
48
    public bool $showDeleteModal = false;
49
50
    /**
51
     * Number of rows displayed on a page.
52
     * @var int
53
     */
54
    public int $perPage = 10;
55
56
    /**
57
     * The Livewire Component constructor.
58
     *
59
     * @return void
60
     */
61
    public function mount(): void
62
    {
63
        $this->perPage = config('xetaravel.pagination.blog.article_per_page');
64
    }
65
66
    /**
67
     * Function to render the component.
68
     *
69
     * @return View
70
     */
71
    public function render()
72
    {
73
        return view('livewire.admin.blog.articles', [
74
            'articles' => $this->rows
0 ignored issues
show
Bug Best Practice introduced by
The property rows does not exist on Xetaravel\Http\Livewire\Admin\Blog\Articles. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
        ]);
76
    }
77
78
    /**
79
     * Create and return the query for the items.
80
     *
81
     * @return Builder
82
     */
83
    public function getRowsQueryProperty(): Builder
84
    {
85
        $query = Article::query()
86
            ->search('title', $this->search);
87
88
        return $this->applySorting($query);
89
    }
90
91
    /**
92
     * Build the query or get it from the cache and paginate it.
93
     *
94
     * @return LengthAwarePaginator
95
     */
96
    public function getRowsProperty(): LengthAwarePaginator
97
    {
98
        return $this->cache(function () {
99
            return $this->applyPagination($this->rowsQuery);
0 ignored issues
show
Bug Best Practice introduced by
The property rowsQuery does not exist on Xetaravel\Http\Livewire\Admin\Blog\Articles. Since you implemented __get, consider adding a @property annotation.
Loading history...
100
        });
101
    }
102
103
    /**
104
     * Display a flash message regarding the action that fire it and the type of the message, then emit an
105
     * `alert ` event.
106
     *
107
     * @param string $action The action that fire the flash message.
108
     * @param string $type The type of the message, success or danger.
109
     * @param int $deleteCount If set, the number of categories that has been deleted.
110
     *
111
     * @return void
112
     */
113
    public function fireFlash(string $action, string $type, int $deleteCount = 0)
114
    {
115
        switch ($action) {
116
            case 'delete':
117
                if ($type == 'success') {
118
                    session()->flash('success', "<b>{$deleteCount}</b> articles has been deleted successfully !");
119
                } else {
120
                    session()->flash('danger', "An error occurred while deleting the articles !");
121
                }
122
                break;
123
        }
124
125
        // Emit the alert event to the front so the DIsmiss can trigger the flash message.
126
        $this->emit('alert');
127
    }
128
}
129