Passed
Push — main ( 3cfdb8...a45320 )
by PRATIK
10:34
created

PostsTable   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 107
c 1
b 0
f 1
dl 0
loc 182
rs 9.76
wmc 33

18 Methods

Rating   Name   Duplication   Size   Complexity  
A publishedPosts() 0 5 1
A yearPosts() 0 5 1
A featuredPosts() 0 5 1
A dateRangeFilter() 0 7 1
A weekPosts() 0 5 1
A updatedSearch() 0 4 1
A todayPosts() 0 5 1
A monthPosts() 0 5 1
A render() 0 7 1
A mount() 0 5 1
A authorPosts() 0 6 1
C initializePosts() 0 35 15
A tagPost() 0 6 1
A updatedCategoryid() 0 6 1
A orderByPriority() 0 5 1
A allPosts() 0 5 1
A searchQuery() 0 12 2
A pendingPosts() 0 5 1
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Post;
4
5
use Carbon\Carbon;
6
use Livewire\Component;
0 ignored issues
show
Bug introduced by
The type Livewire\Component was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Livewire\WithPagination;
0 ignored issues
show
Bug introduced by
The type Livewire\WithPagination was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Conner\Tagging\Model\Tag;
9
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Adminetic\Website\Models\Admin\Post;
11
use Illuminate\Support\Facades\Cache;
12
use Adminetic\Category\Models\Admin\Category;
13
14
class PostsTable extends Component
15
{
16
    use WithPagination;
17
18
    public $search;
19
20
    public $filter_type = null;
21
22
    public $startDate;
23
24
    public $endDate;
25
26
    public $tag_name;
27
28
    public $user_id;
29
30
    public $categoryid;
31
32
33
    protected $listeners = ['date_range_filter' => 'dateRangeFilter'];
34
35
    public function mount()
36
    {
37
        $this->resetPage();
38
        $this->filter_type = 1;
39
        $this->emit('initialize_posts_table');
40
    }
41
42
    protected $updatesQueryString = ['posts'];
43
44
    public function allPosts()
45
    {
46
        $this->resetPage();
47
        $this->filter_type = 1;
48
        $this->emit('initialize_posts_table');
49
    }
50
51
    // Filter
52
    public function todayPosts()
53
    {
54
        $this->resetPage();
55
        $this->filter_type = 2;
56
        $this->emit('initialize_posts_table');
57
    }
58
    public function weekPosts()
59
    {
60
        $this->resetPage();
61
        $this->filter_type = 3;
62
        $this->emit('initialize_posts_table');
63
    }
64
    public function monthPosts()
65
    {
66
        $this->resetPage();
67
        $this->filter_type = 4;
68
        $this->emit('initialize_posts_table');
69
    }
70
    public function yearPosts()
71
    {
72
        $this->resetPage();
73
        $this->filter_type = 5;
74
        $this->emit('initialize_posts_table');
75
    }
76
    public function dateRangeFilter($startDate, $endDate)
77
    {
78
        $this->resetPage();
79
        $this->filter_type = 6;
80
        $this->startDate = $startDate;
81
        $this->endDate = $endDate;
82
        $this->emit('initialize_posts_table');
83
    }
84
    public function tagPost(Tag $tag)
85
    {
86
        $this->resetPage();
87
        $this->filter_type = 7;
88
        $this->tag_name = $tag->name;
89
        $this->emit('initialize_posts_table');
90
    }
91
    public function publishedPosts()
92
    {
93
        $this->resetPage();
94
        $this->filter_type = 8;
95
        $this->emit('initialize_posts_table');
96
    }
97
    public function pendingPosts()
98
    {
99
        $this->resetPage();
100
        $this->filter_type = 9;
101
        $this->emit('initialize_posts_table');
102
    }
103
    public function featuredPosts()
104
    {
105
        $this->resetPage();
106
        $this->filter_type = 10;
107
        $this->emit('initialize_posts_table');
108
    }
109
    public function authorPosts(User $user)
110
    {
111
        $this->resetPage();
112
        $this->filter_type = 11;
113
        $this->user_id = $user->id;
114
        $this->emit('initialize_posts_table');
115
    }
116
    public function orderByPriority()
117
    {
118
        $this->resetPage();
119
        $this->filter_type = 12;
120
        $this->emit('initialize_posts_table');
121
    }
122
    public function updatedCategoryid($categoryid)
123
    {
124
        $this->resetPage();
125
        $this->filter_type = 13;
126
        $this->categoryid = $categoryid;
127
        $this->emit('initialize_posts_table');
128
    }
129
130
    public function updatedSearch()
131
    {
132
        $this->filter_type = 14;
133
        $this->emit('initialize_posts_table');
134
    }
135
136
    public function render()
137
    {
138
        $posts = $this->initializePosts();
139
        $tags = Tag::where('count', '>', 0)->orderBy('count', 'desc')->get();
140
        $authors = User::has('posts', '>', 0)->with('posts')->get();
141
        $parentcategories = Category::whereNull('category_id')->with('childrenCategories')->get();
142
        return view('website::livewire.admin.post.posts-table', compact('tags', 'authors', 'parentcategories', 'posts'));
143
    }
144
145
146
    protected function initializePosts()
147
    {
148
        $filter = $this->filter_type;
149
        if ($filter == 1) {
150
            return Post::tenent()->with('author', 'tagged')->latest()->paginate(10);
151
        } elseif ($filter == 2) {
152
            return Post::tenent()->with('author', 'tagged')->today()->paginate(10);
153
        } elseif ($filter == 3) {
154
            return  Post::tenent()->with('author', 'tagged')->week()->paginate(10);
155
        } elseif ($filter == 4) {
156
            return Post::tenent()->with('author', 'tagged')->month()->paginate(10);
157
        } elseif ($filter == 5) {
158
            return Post::tenent()->with('author', 'tagged')->year()->paginate(10);
159
        } elseif ($filter == 6) {
160
            $start = Carbon::create($this->startDate);
161
            $end = Carbon::create($this->endDate);
162
            return Post::tenent()->with('author', 'tagged')->whereBetween('updated_at', [$start->toDateString(), $end->toDateString()])->paginate(10);
163
        } elseif ($filter == 7) {
164
            return Post::tenent()->withAnyTag($this->tag_name)->with('author', 'tagged')->latest()->paginate(10);
165
        } elseif ($filter == 8) {
166
            return Post::tenent()->published()->with('author', 'tagged')->latest()->paginate(10);
167
        } elseif ($filter == 9) {
168
            return Post::tenent()->pending()->with('author', 'tagged')->latest()->paginate(10);
169
        } elseif ($filter == 10) {
170
            return Post::tenent()->featured()->with('author', 'tagged')->latest()->paginate(10);
171
        } elseif ($filter == 11) {
172
            return Post::tenent()->where('author_id', $this->user_id)->paginate(10);
173
        } elseif ($filter == 12) {
174
            return  Post::tenent()->with('author', 'tagged')->orderBy('priority', 'desc')->paginate(10);
175
        } elseif ($filter == 13) {
176
            return Post::tenent()->with('author', 'tagged')->where('category_id', $this->categoryid)->latest()->paginate(10);
177
        } elseif ($filter == 14) {
178
            return $this->searchQuery()->tenent()->with('author', 'tagged')->latest()->paginate(10);
179
        } else {
180
            return Post::tenent()->with('author', 'tagged')->latest()->paginate(10)->paginate(10);
181
        }
182
    }
183
184
    protected function searchQuery()
185
    {
186
        $search = $this->search ?? null;
187
        if ($search != '') {
188
            return Post::where(function ($query) use ($search) {
189
                $query->where('name', 'LIKE', '%' . $search . '%')
190
                    ->orWhere('excerpt', 'LIKE', '%' . $search . '%')
191
                    ->orWhere('seo_name', 'LIKE', '%' . $search . '%')
192
                    ->orWhere('meta_description', 'LIKE', '%' . $search . '%');
193
            });
194
        } else {
195
            return Post::latest();
196
        }
197
    }
198
}
199