Completed
Push — master ( 5c55d7...79e99a )
by Fèvre
28s queued 15s
created

Articles   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 110
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRowsQueryProperty() 0 6 1
A mount() 0 3 1
A getRowsProperty() 0 4 1
A render() 0 4 1
A fireFlash() 0 14 3
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 Livewire\Component;
9
use Livewire\WithPagination;
10
use Xetaravel\Http\Livewire\Traits\WithCachedRows;
11
use Xetaravel\Http\Livewire\Traits\WithSorting;
12
use Xetaravel\Http\Livewire\Traits\WithBulkActions;
13
use Xetaravel\Http\Livewire\Traits\WithPerPagePagination;
14
use Xetaravel\Models\Article;
15
16
class Articles extends Component
17
{
18
    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...
19
    use WithSorting;
20
    use WithCachedRows;
21
    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...
22
    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...
23
24
    /**
25
     * The string to search.
26
     *
27
     * @var string
28
     */
29
    public string $search = '';
30
31
    /**
32
     * Used to update in URL the query string.
33
     *
34
     * @var string[]
35
     */
36
    protected $queryString = [
37
        'sortField' => ['as' => 'f'],
38
        'sortDirection' => ['as' => 'd'],
39
        'search' => ['except' => '', 'as' => 's']
40
    ];
41
42
    /**
43
     * Used to show the delete modal.
44
     *
45
     * @var bool
46
     */
47
    public bool $showDeleteModal = false;
48
49
    /**
50
     * Number of rows displayed on a page.
51
     * @var int
52
     */
53
    public int $perPage = 10;
54
55
    /**
56
     * The Livewire Component constructor.
57
     *
58
     * @return void
59
     */
60
    public function mount(): void
61
    {
62
        $this->perPage = config('xetaravel.pagination.blog.article_per_page');
63
    }
64
65
    /**
66
     * Function to render the component.
67
     *
68
     * @return View
69
     */
70
    public function render()
71
    {
72
        return view('livewire.admin.blog.articles', [
73
            '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...
74
        ]);
75
    }
76
77
    /**
78
     * Create and return the query for the items.
79
     *
80
     * @return Builder
81
     */
82
    public function getRowsQueryProperty(): Builder
83
    {
84
        $query = Article::query()
85
            ->search('title', $this->search);
86
87
        return $this->applySorting($query);
88
    }
89
90
    /**
91
     * Build the query or get it from the cache and paginate it.
92
     *
93
     * @return LengthAwarePaginator
94
     */
95
    public function getRowsProperty(): LengthAwarePaginator
96
    {
97
        return $this->cache(function () {
98
            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...
99
        });
100
    }
101
102
    /**
103
     * Display a flash message regarding the action that fire it and the type of the message, then emit an
104
     * `alert ` event.
105
     *
106
     * @param string $action The action that fire the flash message.
107
     * @param string $type The type of the message, success or danger.
108
     * @param int $deleteCount If set, the number of categories that has been deleted.
109
     *
110
     * @return void
111
     */
112
    public function fireFlash(string $action, string $type, int $deleteCount = 0)
113
    {
114
        switch ($action) {
115
            case 'delete':
116
                if ($type == 'success') {
117
                    session()->flash('success', "<b>{$deleteCount}</b> articles has been deleted successfully !");
118
                } else {
119
                    session()->flash('danger', "An error occurred while deleting the articles !");
120
                }
121
                break;
122
        }
123
124
        // Emit the alert event to the front so the DIsmiss can trigger the flash message.
125
        $this->emit('alert');
126
    }
127
}
128