forked from eatvio/eatvio-infra
make static-analyse code
This commit is contained in:
@@ -7,52 +7,56 @@ use App\Models\Recipe;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class FeedService
|
||||
{
|
||||
public static function showActualFeed()
|
||||
public static function showActualFeed(): Collection
|
||||
{
|
||||
$dateMax = Recipe::max('created_at');
|
||||
$dateMin = Carbon::parse($dateMax)->subDays(10);
|
||||
|
||||
$sql = <<<'EOF'
|
||||
SELECT
|
||||
p.id,
|
||||
p.title,
|
||||
(
|
||||
(p.likes_count + p.comments_count) /
|
||||
((UNIX_TIMESTAMP(GREATEST(p.created_at,
|
||||
COALESCE((SELECT MAX(l.created_at) FROM blog_post_likes l WHERE l.blog_post_id = p.id), p.created_at),
|
||||
COALESCE((SELECT MAX(c.created_at) FROM blog_post_comments c WHERE c.blog_post_id = p.id), p.created_at)
|
||||
)) - UNIX_TIMESTAMP(p.created_at))/60 + 1
|
||||
) ^ 1.8
|
||||
) AS popularity
|
||||
FROM
|
||||
(SELECT
|
||||
`blog_posts`.id,
|
||||
blog_posts.title,
|
||||
blog_posts.created_at,
|
||||
COUNT(blog_post_comments.id) AS comments_count,
|
||||
COUNT(blog_post_likes.id) AS likes_count
|
||||
FROM
|
||||
`blog_posts`
|
||||
LEFT JOIN blog_post_comments ON blog_post_comments.blog_post_id = blog_posts.id
|
||||
LEFT JOIN blog_post_likes ON blog_post_likes.blog_post_id = blog_posts.id
|
||||
GROUP BY blog_posts.id) p
|
||||
ORDER BY popularity DESC;
|
||||
EOF;
|
||||
SELECT
|
||||
p.id,
|
||||
p.title,
|
||||
(
|
||||
(p.likes_count + p.comments_count) /
|
||||
((UNIX_TIMESTAMP(GREATEST(p.created_at,
|
||||
COALESCE((SELECT MAX(l.created_at) FROM blog_post_likes l WHERE l.blog_post_id = p.id), p.created_at),
|
||||
COALESCE((SELECT MAX(c.created_at) FROM blog_post_comments c WHERE c.blog_post_id = p.id), p.created_at)
|
||||
)) - UNIX_TIMESTAMP(p.created_at))/60 + 1
|
||||
) ^ 1.8
|
||||
) AS popularity
|
||||
FROM
|
||||
(SELECT
|
||||
`blog_posts`.id,
|
||||
blog_posts.title,
|
||||
blog_posts.created_at,
|
||||
COUNT(blog_post_comments.id) AS comments_count,
|
||||
COUNT(blog_post_likes.id) AS likes_count
|
||||
FROM
|
||||
`blog_posts`
|
||||
LEFT JOIN blog_post_comments ON blog_post_comments.blog_post_id = blog_posts.id
|
||||
LEFT JOIN blog_post_likes ON blog_post_likes.blog_post_id = blog_posts.id
|
||||
GROUP BY blog_posts.id) p
|
||||
ORDER BY popularity DESC;
|
||||
EOF;
|
||||
|
||||
$hotBlogPosts = collect(
|
||||
DB::select(DB::raw($sql))
|
||||
)->map(fn ($post) => BlogPost::find($post->id))
|
||||
->take(16)->chunk(4);
|
||||
DB::select($sql)
|
||||
)
|
||||
->map(fn($post) => BlogPost::find($post->id))
|
||||
->take(16)
|
||||
->chunk(4);
|
||||
|
||||
$newbiesBlogPosts = User::orderBy('created_at', 'desc')
|
||||
->withCount('author_of_blog_post')
|
||||
->get()
|
||||
->filter(fn ($u) => $u->author_of_blog_post_count > 0)->take(4)
|
||||
->filter(fn($u) => $u->author_of_blog_post_count > 0)
|
||||
->take(4)
|
||||
->pluck('id')
|
||||
->map(fn ($id) => User::find($id)->author_of_blog_post()->limit(2)->get())
|
||||
->map(fn($id) => User::find($id)->author_of_blog_post()->limit(2)->get())
|
||||
->flatten()
|
||||
->random();
|
||||
|
||||
@@ -62,9 +66,14 @@ EOF;
|
||||
->limit(2)
|
||||
->get();
|
||||
|
||||
$firstCollection = $hotBlogPosts->first()->push($newbiesBlogPosts->first())->push($randomRecipe->first());
|
||||
$secondCollection = $hotBlogPosts->skip(1)->first()->push($newbiesBlogPosts->skip(1)->first())->push($randomRecipe->skip(1)->first());
|
||||
$firstCollection = collect([$hotBlogPosts->first(), $newbiesBlogPosts->first()]);
|
||||
$secondCollection = collect([$hotBlogPosts->skip(1)->first(), $newbiesBlogPosts->skip(1)->first()]);
|
||||
|
||||
return $firstCollection->concat($secondCollection)->filter(fn ($el) => ! is_null($el) && $el->id);
|
||||
return $firstCollection->concat($secondCollection)->filter(fn($el) => !is_null($el) && $el->id);
|
||||
|
||||
// $firstCollection = $hotBlogPosts->first()->push($newbiesBlogPosts->first())->push($randomRecipe->first());
|
||||
// $secondCollection = $hotBlogPosts->skip(1)->first()->push($newbiesBlogPosts->skip(1)->first())->push($randomRecipe->skip(1)->first());
|
||||
|
||||
// return $firstCollection->concat($secondCollection)->filter(fn ($el) => ! is_null($el) && $el->id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user