Completed
Push — master ( 7a1e6c...8794c3 )
by Arjay
11:28
created

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