reduce complexity php insight

This commit is contained in:
2024-01-23 10:45:11 +03:00
parent c34b67bef6
commit 9aa724279b
4 changed files with 206 additions and 64 deletions

View File

@@ -9,6 +9,7 @@ use Illuminate\Support\Str;
use Illuminate\View\View;
use Livewire\Component;
use Livewire\WithPagination;
use App\Services\BlogPost\ListViewService;
class ListView extends Component
{
@@ -29,28 +30,11 @@ class ListView extends Component
'author' => ['except' => ''],
];
public function render(): View
public function render(ListViewService $service): View
{
if ($this->author) {
$author = $this->author;
$user = User::find($author);
$posts = $user ? $user->author_of_blog_post()->distinct() : [];
} elseif ($this->tag) {
$tagId = Str::of($this->tag)->split('/-/')->first();
$posts = Tag::find($tagId)->blogPosts();
} elseif ($this->search && mb_strlen($this->search) > 2) {
$posts = BlogPost::where('title', 'rlike', $this->search);
} else {
$posts = (new BlogPost());
}
if ($this->typeFilter === 'user') {
$posts = $posts->whereRelation('author', 'is_consultant', false);
}
if ($this->typeFilter === 'consultant') {
$posts = $posts->whereRelation('author', 'is_consultant', true);
}
$posts = $service->buildBlogPosts();
return view('livewire.blog-post.list-view', ['blogPosts' => $posts->orderBy('created_at', 'desc')->paginate(10)]);
}
}