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

PostRecommendation::recommendedPosts()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Adminetic\Website\Services;
4
5
use Adminetic\Website\Models\Admin\Post;
6
use Illuminate\Support\Facades\Cache;
7
8
class PostRecommendation
9
{
10
11
    public static function recommendedPosts(Post $post, $limit = 6)
12
    {
13
        $posts = Cache::get('posts', Post::with('category', 'author', 'tagged')->latest()->get());
14
15
        return $posts->sortByDesc(function ($p) use ($post) {
16
            $post_tags = $post->tags->pluck('name')->toArray();
17
            $tag_weight = $p->withAnyTag($post_tags)->exists() ? (5 / 100) * $p->weight : 0;
18
            $category_weight = $p->category->id == $post->category_id ? (10 / 100) * $p->weight : 0;
19
            $weight = $p->weight + $tag_weight + $category_weight;
20
            return $weight;
21
        })->take($limit);
22
    }
23
24
    public static function postWeight(Post $post)
25
    {
26
        $post_views_weight = config('coderz.post_views_weight', 5);
27
        $post_priority_weight = config('coderz.post_priority_weight', 2);
28
        $category_view_weight = config('coderz.category_view_weight', 2);
29
        $featured_post_weight = config('coderz.featured_post_weight', 15);
30
        $hot_news_weight = config('coderz.hot_news_weight', 10);
31
32
        $post_views = views($post)->count();
33
        $post_views_total_weight = ($post_views / 5) * $post_views_weight;
34
35
        $post_priority_total_weight = $post->priority * $post_priority_weight;
36
37
        $post_category_views = views($post->category)->count();
38
        $category_view_total_weight = ($post_category_views / 5) * $category_view_weight;
39
40
        $post_total_weight = $post_views_total_weight + $post_priority_total_weight + $category_view_total_weight + $featured_post_weight + $hot_news_weight;
41
42
        return $post_total_weight;
43
    }
44
}
45