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

PostTrait   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 48
c 1
b 0
f 1
dl 0
loc 156
rs 9.68
wmc 34

30 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeYesterday() 0 3 1
A scopeAudio() 0 3 1
A scopeWeekMostVisitedPosts() 0 3 1
A scopeLimitedPriorityPosts() 0 3 1
A scopeLimitedBreakingNews() 0 3 1
A scopeRelatedPosts() 0 6 1
A scopePending() 0 3 1
A scopeLimitedHotNews() 0 3 1
A scopeRelatedTagPosts() 0 3 1
A scopeVideo() 0 3 1
A scopePublished() 0 3 1
A scopeWeek() 0 3 1
A scopeNormalPost() 0 3 1
A scopeTrending() 0 5 1
A scopeMonthMostVisitedPosts() 0 3 1
A scopeToday() 0 3 1
A scopeDraft() 0 3 1
A scopeYesterdayMostVisitedPosts() 0 3 1
A scopeTenent() 0 10 4
A scopeFeaturedLimitedPosts() 0 3 1
A scopeBreakingNews() 0 3 1
A scopeRelatedCategoryPosts() 0 3 1
A scopeMostVisitedPostsChunked() 0 3 1
A scopeYear() 0 4 1
A scopeLatestLimitedPosts() 0 3 1
A scopeHotNews() 0 3 1
A scopeGeneral() 0 3 1
A scopePriority() 0 6 2
A scopeFeatured() 0 3 1
A scopeMonth() 0 5 1
1
<?php
2
3
namespace Adminetic\Website\Traits;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Auth;
7
8
trait PostTrait
9
{
10
    public function scopeTenent($query)
11
    {
12
        if (Auth::user()->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        if (Auth::user()->/** @scrutinizer ignore-call */ exists()) {
Loading history...
13
            if (!Auth::user()->hasRole('superadmin') || !Auth::user()->hasRole('admin')) {
0 ignored issues
show
Bug introduced by
The method hasRole() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

13
            if (!Auth::user()->/** @scrutinizer ignore-call */ hasRole('superadmin') || !Auth::user()->hasRole('admin')) {
Loading history...
14
                return $query->where('author_id', Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
15
            } else {
16
                return $query->whereNotNull('id');
17
            }
18
        } else {
19
            return $query->whereNotNull('id');
20
        }
21
    }
22
    // Drafted Posts
23
    public function scopeDraft($query)
24
    {
25
        return $query->where('status', 1);
26
    }
27
28
    // Pending Post
29
    public function scopePending($query)
30
    {
31
        return $query->where('status', 2);
32
    }
33
34
    // Published Posts
35
    public function scopePublished($query)
36
    {
37
        return $query->where('status', 3);
38
    }
39
40
    // Featured Posts
41
    public function scopeFeatured($query)
42
    {
43
        return $query->published()->where('featured', 1);
44
    }
45
46
    // Normal Posts
47
    public function scopeNormalPost($query)
48
    {
49
        return $query->published()->where('featured', '<>', 1);
50
    }
51
52
53
    public function scopeRelatedTagPosts($query, $id, $tags, $limit = 5)
54
    {
55
        return $query->withAnyTag($tags)->where('id', '<>', $id)->take($limit);
56
    }
57
58
    public function scopeRelatedCategoryPosts($query, $id, $category_id, $limit = 5)
59
    {
60
        return $query->where('category_id', $category_id)->where('id', '<>', $id)->take($limit);
61
    }
62
63
    public function scopeRelatedPosts($query, $id, $category_id, $tags, $limit = 5)
0 ignored issues
show
Unused Code introduced by
The parameter $limit is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function scopeRelatedPosts($query, $id, $category_id, $tags, /** @scrutinizer ignore-unused */ $limit = 5)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        return $query->withAnyTag($tags)
66
            ->orWhere('category_id', $category_id)
67
            ->where('id', '<>', $id)
68
            ->orderByViews();
69
    }
70
    public function scopeGeneral($query)
71
    {
72
        return $query->where('type', 1);
73
    }
74
    public function scopeVideo($query)
75
    {
76
        return $query->where('type', 2)->whereNotNull('video');
77
    }
78
    public function scopeAudio($query)
79
    {
80
        return $query->where('type', 1)->whereNotNull('audio');
81
    }
82
    public function scopeBreakingNews($query)
83
    {
84
        return $query->where('breaking_news', 1);
85
    }
86
    public function scopeHotNews($query)
87
    {
88
        return $query->where('hot_news', 1);
89
    }
90
    public function scopePriority($query, $desc = true)
91
    {
92
        if ($desc) {
93
            return $query->orderBy('priority', 'desc');
94
        } else {
95
            return $query->orderBy('priority', 'asc');
96
        }
97
    }
98
    public function scopeTrending($query, $limit = 12)
99
    {
100
        return $query->with('author', 'tagged')->published()->get()->sortByDesc(function ($p) {
101
            return $p->weight;
102
        })->take($limit);
103
    }
104
    // Filter
105
    public function scopeToday($query)
106
    {
107
        return $query->whereDate('updated_at', Carbon::now());
108
    }
109
    public function scopeYesterday($query)
110
    {
111
        return $query->whereDate('updated_at', Carbon::yesterday());
112
    }
113
    public function scopeWeek($query)
114
    {
115
        return $query->whereBetween('updated_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]);
116
    }
117
    public function scopeMonth($query)
118
    {
119
        $year = Carbon::now()->year;
120
        $month = Carbon::now()->month;
121
        return $query->whereYear('updated_at', $year)->whereMonth('updated_at', $month);
122
    }
123
    public function scopeYear($query)
124
    {
125
        $year = Carbon::now()->year;
126
        return $query->whereYear('updated_at', $year);
127
    }
128
    // Custom 
129
    public function scopeLatestLimitedPosts($query, $limit = 10)
130
    {
131
        return $query->with('author', 'tagged')->published()->latest()->take($limit);
132
    }
133
    public function scopeFeaturedLimitedPosts($query, $limit = 10)
134
    {
135
        return $query->published()->featured()->with('author', 'tagged')->latest()->take($limit);
136
    }
137
    public function scopeLimitedHotNews($query, $limit = 10)
138
    {
139
        return $query->published()->hotNews()->with('author', 'tagged')->latest()->take($limit);
140
    }
141
    public function scopeLimitedBreakingNews($query, $limit = 5)
142
    {
143
        return $query->published()->breakingNews()->with('author', 'tagged')->latest()->take($limit);
144
    }
145
    public function scopeLimitedPriorityPosts($query, $limit = 10)
146
    {
147
        return $query->published()->priority()->with('author', 'tagged')->take($limit);
148
    }
149
    public function scopeYesterdayMostVisitedPosts($query, $limit = 5)
150
    {
151
        return $query->published()->with('author', 'tagged')->yesterday()->orderByViews()->take($limit);
152
    }
153
    public function scopeWeekMostVisitedPosts($query, $limit = 5)
154
    {
155
        return $query->published()->with('author', 'tagged')->week()->orderByViews()->take($limit);
156
    }
157
    public function scopeMonthMostVisitedPosts($query, $limit = 5)
158
    {
159
        return $query->published()->with('author', 'tagged')->month()->orderByViews()->take($limit);
160
    }
161
    public function scopeMostVisitedPostsChunked($query, $limit = 12)
162
    {
163
        return $query->published()->with('author', 'tagged')->orderByViews()->take($limit)->get()->chunk((int) ($limit / 2));
164
    }
165
}
166