forked from eatvio/eatvio-infra
reduce complexity ListViewService
This commit is contained in:
@@ -4,11 +4,13 @@ namespace App\Services\BlogPost;
|
||||
|
||||
use App\Livewire\BlogPost\ListView;
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\Tag;
|
||||
use App\Models\User;
|
||||
use App\Services\Strategy\Criteried\AuthorCriteriaStrategy;
|
||||
use App\Services\Strategy\Criteried\CriteriaStrategy;
|
||||
use App\Services\Strategy\Criteried\DefaultCriteriaStrategy;
|
||||
use App\Services\Strategy\Criteried\SearchCriteriaStrategy;
|
||||
use App\Services\Strategy\Criteried\TagCriteriaStrategy;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ListViewService
|
||||
{
|
||||
@@ -28,49 +30,34 @@ class ListViewService
|
||||
|
||||
private function getPrimaryCriteriedBlogPosts(): Builder|BlogPost|MorphToMany|array
|
||||
{
|
||||
if ($this->parentState->author) {
|
||||
$posts = $this->getAuthorBlogPosts();
|
||||
} elseif ($this->parentState->tag) {
|
||||
$posts = $this->getTagBlogPosts();
|
||||
} elseif ($this->parentState->search && mb_strlen($this->parentState->search) > 2) {
|
||||
$posts = $this->getSearchBlogPosts();
|
||||
} else {
|
||||
$posts = (new BlogPost());
|
||||
$criteria = $this->getCriteriaStrategy();
|
||||
return $criteria->getBlogPosts();
|
||||
}
|
||||
|
||||
private function getCriteriaStrategy(): CriteriaStrategy
|
||||
{
|
||||
$strategies = [
|
||||
'author' => AuthorCriteriaStrategy::class,
|
||||
'tag' => TagCriteriaStrategy::class,
|
||||
'search' => SearchCriteriaStrategy::class,
|
||||
];
|
||||
|
||||
foreach ($strategies as $condition => $strategyClass) {
|
||||
if ($this->parentState->{$condition}) {
|
||||
return new $strategyClass($this->parentState->{$condition});
|
||||
}
|
||||
}
|
||||
|
||||
return $posts;
|
||||
return new DefaultCriteriaStrategy();
|
||||
}
|
||||
|
||||
private function getSecondaryCriteriedBlogPosts(Builder|BlogPost|MorphToMany|array $posts): BlogPost|Builder
|
||||
{
|
||||
if ($this->parentState->typeFilter === 'user') {
|
||||
$posts = $posts->whereRelation('author', 'is_consultant', false);
|
||||
}
|
||||
|
||||
if ($this->parentState->typeFilter === 'consultant') {
|
||||
$posts = $posts->whereRelation('author', 'is_consultant', true);
|
||||
}
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
private function getAuthorBlogPosts(): array
|
||||
{
|
||||
$author = $this->parentState->author;
|
||||
$user = User::find($author);
|
||||
|
||||
return $user ? $user->author_of_blog_post()->distinct() : [];
|
||||
}
|
||||
|
||||
private function getTagBlogPosts(): MorphToMany
|
||||
{
|
||||
$tagId = Str::of($this->parentState->tag)->split('/-/')->first();
|
||||
return Tag::find($tagId)->blogPosts();
|
||||
}
|
||||
|
||||
private function getSearchBlogPosts(): Builder
|
||||
{
|
||||
return BlogPost::where('title', 'rlike', $this->parentState->search);
|
||||
return match ($this->parentState->typeFilter) {
|
||||
'user' => $posts->whereRelation('author', 'is_consultant', false),
|
||||
'consultant' => $posts->whereRelation('author', 'is_consultant', true),
|
||||
default => $posts,
|
||||
};
|
||||
}
|
||||
|
||||
// private function getTagsCollection(): array
|
||||
|
||||
Reference in New Issue
Block a user