Completed
Push — master ( 0309f7...e870c7 )
by Arjay
14:45
created

Article::getRouteName()   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
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
 * @property Category category
37
 */
38
class Article extends Model implements UrlGenerator
39
{
40
    use AuditableTrait, PublishableTrait, HasSlug;
41
    use CanRequireAuthentication, HasPermission, PresentableTrait;
42
    use HasParameters;
43
44
    /**
45
     * @var \Yajra\CMS\Presenters\ArticlePresenter
46
     */
47
    protected $presenter = ArticlePresenter::class;
48
49
    /**
50
     * @var string
51
     */
52
    protected $table = 'articles';
53
54
    /**
55
     * @var array
56
     */
57
    protected $fillable = [
58
        'title',
59
        'alias',
60
        'published',
61
        'category_id',
62
        'body',
63
        'order',
64
        'featured',
65
        'parameters',
66
        'blade_template',
67
        'authenticated',
68
        'authorization',
69
        'author_alias',
70
    ];
71
72
    /**
73
     * Find a published article by slug.
74
     *
75
     * @param string $slug
76
     * @return \Yajra\CMS\Entities\Article|null
77
     */
78
    public static function findBySlug(string $slug)
79
    {
80
        return static::published()->where('alias', $slug)->firstOrFail();
81
    }
82
83
    /**
84
     * Get popular articles.
85
     *
86
     * @param int $limit
87
     * @return \Illuminate\Support\Collection
88
     */
89
    public static function getPopular($limit = 5)
90
    {
91
        return static::where('hits', '>', 0)->orderBy('hits', 'desc')->limit($limit)->get();
92
    }
93
94
    /**
95
     * Get recently added articles.
96
     *
97
     * @param int $limit
98
     * @return \Illuminate\Support\Collection
99
     */
100
    public static function getRecentlyAdded($limit = 5)
101
    {
102
        return static::latest()->limit($limit)->get();
103
    }
104
105
    /**
106
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
107
     */
108
    public function category()
109
    {
110
        return $this->belongsTo(Category::class);
111
    }
112
113
    /**
114
     * Get the options for generating the slug.
115
     */
116
    public function getSlugOptions() : SlugOptions
117
    {
118
        return SlugOptions::create()
119
                          ->generateSlugsFrom('title')
120
                          ->saveSlugsTo('alias');
121
    }
122
123
    /**
124
     * Get associated permissions.
125
     *
126
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
127
     */
128
    public function permissions()
129
    {
130
        return $this->belongsToMany(Permission::class, 'article_permission');
131
    }
132
133
    /**
134
     * Get order attribute or set default value to 1.
135
     *
136
     * @return int
137
     */
138
    public function getOrderAttribute()
139
    {
140
        return $this->attributes['order'] ?? 1;
141
    }
142
143
    /**
144
     * Get url from implementing class.
145
     *
146
     * @param mixed $args
147
     * @return string
148
     */
149
    public function getUrl($args = null)
150
    {
151
        return url($this->category->getUrl($args) . '/' . $this->alias);
152
    }
153
154
    /**
155
     * Get article's route name.
156
     *
157
     * @return string
158
     */
159
    public function getRouteName()
160
    {
161
        return $this->category->getRouteName() . '.' . $this->alias;
162
    }
163
}
164