Completed
Push — develop ( 99e42f...c7bc75 )
by Abdelrahman
01:02
created

Page::activate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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\Pages\Contracts\PageContract;
13
use Illuminate\Database\Eloquent\Builder;
14
use Rinvex\Support\Traits\HasTranslations;
15
use Rinvex\Support\Traits\ValidatingTrait;
16
use Spatie\EloquentSortable\SortableTrait;
17
18
/**
19
 * Rinvex\Pages\Models\Page.
20
 *
21
 * @property int                 $id
22
 * @property string              $uri
23
 * @property string              $slug
24
 * @property string              $route
25
 * @property string              $domain
26
 * @property string              $middleware
27
 * @property array               $title
28
 * @property array               $subtitle
29
 * @property array               $excerpt
30
 * @property array               $content
31
 * @property string              $view
32
 * @property bool                $is_active
33
 * @property int                 $sort_order
34
 * @property \Carbon\Carbon|null $created_at
35
 * @property \Carbon\Carbon|null $updated_at
36
 * @property \Carbon\Carbon|null $deleted_at
37
 *
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page active()
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page inactive()
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page ordered($direction = 'asc')
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereContent($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereCreatedAt($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDeletedAt($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDomain($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereExcerpt($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereId($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereIsActive($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereMiddleware($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereRoute($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSlug($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSortOrder($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSubtitle($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereTitle($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUpdatedAt($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUri($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereView($value)
57
 * @mixin \Eloquent
58
 */
59
class Page extends Model implements PageContract, Sortable
60
{
61
    use HasSlug;
62
    use SortableTrait;
63
    use HasTranslations;
64
    use ValidatingTrait;
65
    use CacheableEloquent;
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected $fillable = [
71
        'uri',
72
        'slug',
73
        'title',
74
        'route',
75
        'subtitle',
76
        'domain',
77
        'middleware',
78
        'excerpt',
79
        'content',
80
        'view',
81
        'is_active',
82
        'sort_order',
83
    ];
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected $casts = [
89
        'uri' => 'string',
90
        'slug' => 'string',
91
        'route' => 'string',
92
        'domain' => 'string',
93
        'middleware' => 'string',
94
        'view' => 'string',
95
        'is_active' => 'boolean',
96
        'sort_order' => 'integer',
97
        'deleted_at' => 'datetime',
98
    ];
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected $observables = [
104
        'validating',
105
        'validated',
106
    ];
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public $translatable = [
112
        'title',
113
        'subtitle',
114
        'excerpt',
115
        'content',
116
    ];
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public $sortable = [
122
        'order_column_name' => 'sort_order',
123
    ];
124
125
    /**
126
     * The default rules that the model will validate against.
127
     *
128
     * @var array
129
     */
130
    protected $rules = [];
131
132
    /**
133
     * Whether the model should throw a
134
     * ValidationException if it fails validation.
135
     *
136
     * @var bool
137
     */
138
    protected $throwValidationExceptions = true;
139
140
    /**
141
     * Create a new Eloquent model instance.
142
     *
143
     * @param array $attributes
144
     */
145
    public function __construct(array $attributes = [])
146
    {
147
        parent::__construct($attributes);
148
149
        $this->setTable(config('rinvex.pages.tables.pages'));
150
        $this->setRules([
151
            '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...
152
            '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...
153
            '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...
154
            'domain' => 'nullable|string|max:150',
155
            'middleware' => 'nullable|string|max:150',
156
            'title' => 'required|string|max:150',
157
            'subtitle' => 'nullable|string|max:150',
158
            'excerpt' => 'nullable|string|max:10000',
159
            'content' => 'nullable|string|max:10000000',
160
            'view' => 'required|string|max:150',
161
            'is_active' => 'sometimes|boolean',
162
            'sort_order' => 'nullable|integer|max:10000000',
163
        ]);
164
    }
165
166
    /**
167
     * Get the active pages.
168
     *
169
     * @param \Illuminate\Database\Eloquent\Builder $builder
170
     *
171
     * @return \Illuminate\Database\Eloquent\Builder
172
     */
173
    public function scopeActive(Builder $builder): Builder
174
    {
175
        return $builder->where('is_active', true);
176
    }
177
178
    /**
179
     * Get the inactive pages.
180
     *
181
     * @param \Illuminate\Database\Eloquent\Builder $builder
182
     *
183
     * @return \Illuminate\Database\Eloquent\Builder
184
     */
185
    public function scopeInactive(Builder $builder): Builder
186
    {
187
        return $builder->where('is_active', false);
188
    }
189
190
    /**
191
     * Get the options for generating the slug.
192
     *
193
     * @return \Spatie\Sluggable\SlugOptions
194
     */
195
    public function getSlugOptions(): SlugOptions
196
    {
197
        return SlugOptions::create()
198
                          ->doNotGenerateSlugsOnUpdate()
199
                          ->generateSlugsFrom('title')
200
                          ->saveSlugsTo('slug');
201
    }
202
203
    /**
204
     * Activate the page.
205
     *
206
     * @return $this
207
     */
208
    public function activate()
209
    {
210
        $this->update(['is_active' => true]);
211
212
        return $this;
213
    }
214
215
    /**
216
     * Deactivate the page.
217
     *
218
     * @return $this
219
     */
220
    public function deactivate()
221
    {
222
        $this->update(['is_active' => false]);
223
224
        return $this;
225
    }
226
}
227