forked from eatvio/eatvio-infra
Merge pull request 'уменьшил сложность классов' (#68) from alex_chevski/eatvio:reduce-complexity into main
Reviewed-on: eatvio/eatvio#68
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
|
||||
|
||||
@@ -4,11 +4,13 @@ namespace App\Services\Recipe;
|
||||
|
||||
use App\Livewire\Recipe\ListView;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\Tag;
|
||||
use App\Models\User;
|
||||
use App\Services\Strategy\Criteried\AuthorCriteriaStrategy;
|
||||
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;
|
||||
use App\Services\Strategy\Criteried\CriteriaStrategy;
|
||||
|
||||
class ListViewService
|
||||
{
|
||||
@@ -35,7 +37,6 @@ class ListViewService
|
||||
]
|
||||
)->reduce(function ($acc, $item) {
|
||||
[$type, $tagNames] = $item;
|
||||
|
||||
foreach ($tagNames as $name) {
|
||||
if (isset($acc[$type][$name])) {
|
||||
$acc[$type][$name] += 1;
|
||||
@@ -43,54 +44,39 @@ class ListViewService
|
||||
$acc[$type][$name] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $acc;
|
||||
});
|
||||
}
|
||||
|
||||
private function getPrimaryCriteriedRecipes(): Builder|Recipe|MorphToMany|array
|
||||
{
|
||||
if ($this->parentState->author) {
|
||||
return $this->getAuthorRecipes();
|
||||
}
|
||||
if ($this->parentState->tag) {
|
||||
return $this->getTagRecipes();
|
||||
}
|
||||
if ($this->parentState->search && mb_strlen($this->parentState->search) > 2) {
|
||||
return $this->getSearchRecipes();
|
||||
$criteria = $this->getCriteriaStrategy();
|
||||
return $criteria->getRecipes();
|
||||
}
|
||||
|
||||
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 new Recipe();
|
||||
return new DefaultCriteriaStrategy();
|
||||
}
|
||||
|
||||
private function getSecondaryCriteriedRecipes(Builder|Recipe|MorphToMany|array $recipes): Recipe
|
||||
{
|
||||
if ($this->parentState->typeFilter === 'user') {
|
||||
$recipes = $recipes->whereRelation('author', 'is_consultant', false);
|
||||
}
|
||||
|
||||
if ($this->parentState->typeFilter === 'consultant') {
|
||||
$recipes = $recipes->whereRelation('author', 'is_consultant', true);
|
||||
}
|
||||
|
||||
return $recipes;
|
||||
}
|
||||
|
||||
private function getAuthorRecipes(): array
|
||||
{
|
||||
$author = $this->parentState->author;
|
||||
$user = User::find($author);
|
||||
return $user ? $user->author_of_recipe() : [];
|
||||
}
|
||||
|
||||
private function getTagRecipes(): MorphToMany
|
||||
{
|
||||
$tagId = Str::of($this->parentState->tag)->split('/-/')->first();
|
||||
return Tag::find($tagId)->recipes();
|
||||
}
|
||||
|
||||
private function getSearchRecipes(): Builder
|
||||
{
|
||||
return Recipe::where('title', 'rlike', $this->parentState->search);
|
||||
return match ($this->parentState->typeFilter) {
|
||||
'user' => $recipes->whereRelation('author', 'is_consultant', false),
|
||||
'consultant' => $recipes->whereRelation('author', 'is_consultant', true),
|
||||
default => $recipes,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
30
app/Services/Strategy/Criteried/AuthorCriteriaStrategy.php
Normal file
30
app/Services/Strategy/Criteried/AuthorCriteriaStrategy.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Strategy\Criteried;
|
||||
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
class AuthorCriteriaStrategy implements CriteriaStrategy
|
||||
{
|
||||
public function __construct(private User $author)
|
||||
{
|
||||
}
|
||||
|
||||
public function getBlogPosts(): Builder|BlogPost|MorphToMany|array
|
||||
{
|
||||
$user = User::find($this->author);
|
||||
|
||||
return $user ? $user->author_of_blog_post()->distinct() : [];
|
||||
}
|
||||
|
||||
public function getRecipes(): Builder|Recipe|MorphToMany|array
|
||||
{
|
||||
$user = User::find($this->author);
|
||||
|
||||
return $user ? $user->author_of_recipe()->distinct() : [];
|
||||
}
|
||||
}
|
||||
15
app/Services/Strategy/Criteried/CriteriaStrategy.php
Normal file
15
app/Services/Strategy/Criteried/CriteriaStrategy.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Strategy\Criteried;
|
||||
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\Recipe;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
interface CriteriaStrategy
|
||||
{
|
||||
public function getBlogPosts(): Builder|BlogPost|MorphToMany|array;
|
||||
|
||||
public function getRecipes(): Builder|Recipe|MorphToMany|array;
|
||||
}
|
||||
21
app/Services/Strategy/Criteried/DefaultCriteriaStrategy.php
Normal file
21
app/Services/Strategy/Criteried/DefaultCriteriaStrategy.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Strategy\Criteried;
|
||||
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\Recipe;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
class DefaultCriteriaStrategy implements CriteriaStrategy
|
||||
{
|
||||
public function getBlogPosts(): Builder|BlogPost|MorphToMany|array
|
||||
{
|
||||
return new BlogPost();
|
||||
}
|
||||
|
||||
public function getRecipes(): Builder|Recipe|MorphToMany|array
|
||||
{
|
||||
return new Recipe();
|
||||
}
|
||||
}
|
||||
25
app/Services/Strategy/Criteried/SearchCriteriaStrategy.php
Normal file
25
app/Services/Strategy/Criteried/SearchCriteriaStrategy.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Strategy\Criteried;
|
||||
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\Recipe;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
class SearchCriteriaStrategy implements CriteriaStrategy
|
||||
{
|
||||
public function __construct(private string $search)
|
||||
{
|
||||
}
|
||||
|
||||
public function getBlogPosts(): Builder|BlogPost|MorphToMany|array
|
||||
{
|
||||
return BlogPost::where('title', 'rlike', $this->search);
|
||||
}
|
||||
|
||||
public function getRecipes(): Builder|Recipe|MorphToMany|array
|
||||
{
|
||||
return Recipe::where('title', 'rlike', $this->search);
|
||||
}
|
||||
}
|
||||
29
app/Services/Strategy/Criteried/TagCriteriaStrategy.php
Normal file
29
app/Services/Strategy/Criteried/TagCriteriaStrategy.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Strategy\Criteried;
|
||||
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class TagCriteriaStrategy implements CriteriaStrategy
|
||||
{
|
||||
public function __construct(private string $tag)
|
||||
{
|
||||
}
|
||||
|
||||
public function getBlogPosts(): Builder|BlogPost|MorphToMany|array
|
||||
{
|
||||
$tagId = Str::of($this->tag)->split('/-/')->first();
|
||||
return Tag::find($tagId)->blogPosts();
|
||||
}
|
||||
|
||||
public function getRecipes(): Builder|Recipe|MorphToMany|array
|
||||
{
|
||||
$tagId = Str::of($this->tag)->split('/-/')->first();
|
||||
return Tag::find($tagId)->recipes();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user