Completed
Push — master ( e1326f...7c8e33 )
by Abdelrahman
02:34 queued 01:22
created

Page::scopeInactive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Pages\Models;
6
7
use Spatie\Sluggable\SlugOptions;
8
use Rinvex\Support\Traits\HasSlug;
9
use Spatie\EloquentSortable\Sortable;
10
use Illuminate\Database\Eloquent\Model;
11
use Rinvex\Cacheable\CacheableEloquent;
12
use Rinvex\Support\Traits\HasTranslations;
13
use Rinvex\Support\Traits\ValidatingTrait;
14
use Spatie\EloquentSortable\SortableTrait;
15
16
/**
17
 * Rinvex\Pages\Models\Page.
18
 *
19
 * @property int                 $id
20
 * @property string              $uri
21
 * @property string              $slug
22
 * @property string              $route
23
 * @property string              $domain
24
 * @property string              $middleware
25
 * @property array               $title
26
 * @property array               $subtitle
27
 * @property array               $excerpt
28
 * @property array               $content
29
 * @property string              $view
30
 * @property bool                $is_active
31
 * @property int                 $sort_order
32
 * @property \Carbon\Carbon|null $created_at
33
 * @property \Carbon\Carbon|null $updated_at
34
 * @property \Carbon\Carbon|null $deleted_at
35
 *
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page ordered($direction = 'asc')
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereContent($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereCreatedAt($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDeletedAt($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDomain($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereExcerpt($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereId($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereIsActive($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereMiddleware($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereRoute($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSlug($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSortOrder($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSubtitle($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereTitle($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUpdatedAt($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUri($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereView($value)
53
 * @mixin \Eloquent
54
 */
55
class Page extends Model implements Sortable
56
{
57
    use HasSlug;
58
    use SortableTrait;
59
    use HasTranslations;
60
    use ValidatingTrait;
61
    use CacheableEloquent;
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected $fillable = [
67
        'uri',
68
        'slug',
69
        'title',
70
        'route',
71
        'subtitle',
72
        'domain',
73
        'middleware',
74
        'excerpt',
75
        'content',
76
        'view',
77
        'is_active',
78
        'sort_order',
79
    ];
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected $casts = [
85
        'uri' => 'string',
86
        'slug' => 'string',
87
        'route' => 'string',
88
        'domain' => 'string',
89
        'middleware' => 'string',
90
        'view' => 'string',
91
        'is_active' => 'boolean',
92
        'sort_order' => 'integer',
93
        'deleted_at' => 'datetime',
94
    ];
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    protected $observables = [
100
        'validating',
101
        'validated',
102
    ];
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public $translatable = [
108
        'title',
109
        'subtitle',
110
        'excerpt',
111
        'content',
112
    ];
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public $sortable = [
118
        'order_column_name' => 'sort_order',
119
    ];
120
121
    /**
122
     * The default rules that the model will validate against.
123
     *
124
     * @var array
125
     */
126
    protected $rules = [];
127
128
    /**
129
     * Whether the model should throw a
130
     * ValidationException if it fails validation.
131
     *
132
     * @var bool
133
     */
134
    protected $throwValidationExceptions = true;
135
136
    /**
137
     * Create a new Eloquent model instance.
138
     *
139
     * @param array $attributes
140
     */
141
    public function __construct(array $attributes = [])
142
    {
143
        parent::__construct($attributes);
144
145
        $this->setTable(config('rinvex.pages.tables.pages'));
146
        $this->setRules([
147
            'uri' => 'required|regex:/^([0-9a-z\/_-]+)$/|max:150|unique:'.config('rinvex.pages.tables.pages').',uri,NULL,id,domain,'.($this->domain ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 159 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
148
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.pages.tables.pages').',slug,NULL,id,domain,'.($this->domain ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
149
            'route' => 'required|regex:/^([0-9a-z\._-]+)$/|max:150|unique:'.config('rinvex.pages.tables.pages').',route,NULL,id,domain,'.($this->domain ?? 'null'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
150
            'domain' => 'nullable|string|max:150',
151
            'middleware' => 'nullable|string|max:150',
152
            'title' => 'required|string|max:150',
153
            'subtitle' => 'nullable|string|max:150',
154
            'excerpt' => 'nullable|string|max:10000',
155
            'content' => 'nullable|string|max:10000000',
156
            'view' => 'required|string|max:150',
157
            'is_active' => 'sometimes|boolean',
158
            'sort_order' => 'nullable|integer|max:10000000',
159
        ]);
160
    }
161
162
    /**
163
     * Get the options for generating the slug.
164
     *
165
     * @return \Spatie\Sluggable\SlugOptions
166
     */
167
    public function getSlugOptions(): SlugOptions
168
    {
169
        return SlugOptions::create()
170
                          ->doNotGenerateSlugsOnUpdate()
171
                          ->generateSlugsFrom('title')
172
                          ->saveSlugsTo('slug');
173
    }
174
175
    /**
176
     * Activate the page.
177
     *
178
     * @return $this
179
     */
180
    public function activate()
181
    {
182
        $this->update(['is_active' => true]);
183
184
        return $this;
185
    }
186
187
    /**
188
     * Deactivate the page.
189
     *
190
     * @return $this
191
     */
192
    public function deactivate()
193
    {
194
        $this->update(['is_active' => false]);
195
196
        return $this;
197
    }
198
}
199