Completed
Push — master ( a19a47...15bbc5 )
by Arjay
15:59
created

Article::getPopular()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Yajra\CMS\Entities;
4
5
use Conner\Tagging\Taggable;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Laracasts\Presenter\PresentableTrait;
9
use Spatie\EloquentSortable\SortableTrait;
10
use Spatie\Sluggable\HasSlug;
11
use Spatie\Sluggable\SlugOptions;
12
use Yajra\Acl\Models\Permission;
13
use Yajra\Acl\Traits\HasPermission;
14
use Yajra\Auditable\AuditableTrait;
15
use Yajra\CMS\Contracts\UrlGenerator;
16
use Yajra\CMS\Entities\Traits\CanRequireAuthentication;
17
use Yajra\CMS\Entities\Traits\HasParameters;
18
use Yajra\CMS\Entities\Traits\PublishableTrait;
19
use Yajra\CMS\Presenters\ArticlePresenter;
20
21
/**
22
 * Class Article
23
 *
24
 * @package App
25
 * @property int id
26
 * @property string title
27
 * @property string alias
28
 * @property boolean published
29
 * @property boolean featured
30
 * @property int category_id
31
 * @property bool authenticated
32
 * @property bool is_page
33
 * @property Collection permissions
34
 * @property int hits
35
 * @property int order
36
 * @property string parameters
37
 * @property string authorization
38
 * @property string author_alias
39
 * @property Category category
40
 */
41
class Article extends Model implements UrlGenerator
42
{
43
    use AuditableTrait, PublishableTrait, HasSlug;
44
    use CanRequireAuthentication, HasPermission, PresentableTrait;
45
    use HasParameters, SortableTrait, Taggable;
46
47
    public $sortable = [
48
        'order_column_name'  => 'order',
49
        'sort_when_creating' => true,
50
    ];
51
52
    /**
53
     * @var \Yajra\CMS\Presenters\ArticlePresenter
54
     */
55
    protected $presenter = ArticlePresenter::class;
56
57
    /**
58
     * @var string
59
     */
60
    protected $table = 'articles';
61
62
    /**
63
     * @var array
64
     */
65
    protected $fillable = [
66
        'title',
67
        'alias',
68
        'published',
69
        'category_id',
70
        'body',
71
        'order',
72
        'featured',
73
        'parameters',
74
        'blade_template',
75
        'authenticated',
76
        'authorization',
77
        'author_alias',
78
    ];
79
80
    /**
81
     * Find a published article by slug.
82
     *
83
     * @param string $slug
84
     * @return \Yajra\CMS\Entities\Article|null
85
     */
86
    public static function findBySlug(string $slug)
87
    {
88
        return static::published()->where('alias', $slug)->firstOrFail();
89
    }
90
91
    /**
92
     * Get popular articles.
93
     *
94
     * @param int $limit
95
     * @return \Illuminate\Support\Collection
96
     */
97
    public static function getPopular($limit = 5)
98
    {
99
        return static::where('hits', '>', 0)->orderBy('hits', 'desc')->limit($limit)->get();
100
    }
101
102
    /**
103
     * Get recently added articles.
104
     *
105
     * @param int $limit
106
     * @return \Illuminate\Support\Collection
107
     */
108
    public static function getRecentlyAdded($limit = 5)
109
    {
110
        return static::latest()->limit($limit)->get();
111
    }
112
113
    /**
114
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
115
     */
116
    public function category()
117
    {
118
        return $this->belongsTo(Category::class);
119
    }
120
121
    /**
122
     * Get the options for generating the slug.
123
     */
124
    public function getSlugOptions(): SlugOptions
125
    {
126
        return SlugOptions::create()
127
                          ->generateSlugsFrom('title')
128
                          ->saveSlugsTo('alias');
129
    }
130
131
    /**
132
     * Get associated permissions.
133
     *
134
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
135
     */
136
    public function permissions()
137
    {
138
        return $this->belongsToMany(Permission::class, 'article_permission');
139
    }
140
141
    /**
142
     * Get order attribute or set default value to 1.
143
     *
144
     * @return int
145
     */
146
    public function getOrderAttribute()
147
    {
148
        return $this->attributes['order'] ?? 1;
149
    }
150
151
    /**
152
     * Get url from implementing class.
153
     *
154
     * @param mixed $args
155
     * @return string
156
     */
157
    public function getUrl($args = null)
158
    {
159
        if ($this->is_page) {
160
            return url($this->alias);
161
        }
162
163
        return url($this->category->getUrl($args) . '/' . $this->alias);
164
    }
165
166
    /**
167
     * Get article's route name.
168
     *
169
     * @return string
170
     */
171
    public function getRouteName()
172
    {
173
        if ($this->is_page) {
174
            return $this->alias;
175
        }
176
177
        return $this->category->getRouteName() . '.' . $this->alias;
178
    }
179
}
180