Completed
Push — develop ( 094427...2fd97c )
by Abdelrahman
01:06
created

Page::deactivate()   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 ordered($direction = 'asc')
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereContent($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereCreatedAt($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDeletedAt($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDomain($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereExcerpt($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereId($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereIsActive($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereMiddleware($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereRoute($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSlug($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSortOrder($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSubtitle($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereTitle($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUpdatedAt($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUri($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereView($value)
55
 * @mixin \Eloquent
56
 */
57
class Page extends Model implements PageContract, Sortable
58
{
59
    use HasSlug;
60
    use SortableTrait;
61
    use HasTranslations;
62
    use ValidatingTrait;
63
    use CacheableEloquent;
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected $fillable = [
69
        'uri',
70
        'slug',
71
        'title',
72
        'route',
73
        'subtitle',
74
        'domain',
75
        'middleware',
76
        'excerpt',
77
        'content',
78
        'view',
79
        'is_active',
80
        'sort_order',
81
    ];
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected $casts = [
87
        'uri' => 'string',
88
        'slug' => 'string',
89
        'route' => 'string',
90
        'domain' => 'string',
91
        'middleware' => 'string',
92
        'view' => 'string',
93
        'is_active' => 'boolean',
94
        'sort_order' => 'integer',
95
        'deleted_at' => 'datetime',
96
    ];
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    protected $observables = [
102
        'validating',
103
        'validated',
104
    ];
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public $translatable = [
110
        'title',
111
        'subtitle',
112
        'excerpt',
113
        'content',
114
    ];
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public $sortable = [
120
        'order_column_name' => 'sort_order',
121
    ];
122
123
    /**
124
     * The default rules that the model will validate against.
125
     *
126
     * @var array
127
     */
128
    protected $rules = [];
129
130
    /**
131
     * Whether the model should throw a
132
     * ValidationException if it fails validation.
133
     *
134
     * @var bool
135
     */
136
    protected $throwValidationExceptions = true;
137
138
    /**
139
     * Create a new Eloquent model instance.
140
     *
141
     * @param array $attributes
142
     */
143
    public function __construct(array $attributes = [])
144
    {
145
        parent::__construct($attributes);
146
147
        $this->setTable(config('rinvex.pages.tables.pages'));
148
        $this->setRules([
149
            '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...
150
            '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...
151
            '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...
152
            'domain' => 'nullable|string|max:150',
153
            'middleware' => 'nullable|string|max:150',
154
            'title' => 'required|string|max:150',
155
            'subtitle' => 'nullable|string|max:150',
156
            'excerpt' => 'nullable|string|max:10000',
157
            'content' => 'nullable|string|max:10000000',
158
            'view' => 'required|string|max:150',
159
            'is_active' => 'sometimes|boolean',
160
            'sort_order' => 'nullable|integer|max:10000000',
161
        ]);
162
    }
163
164
    /**
165
     * Get the options for generating the slug.
166
     *
167
     * @return \Spatie\Sluggable\SlugOptions
168
     */
169
    public function getSlugOptions(): SlugOptions
170
    {
171
        return SlugOptions::create()
172
                          ->doNotGenerateSlugsOnUpdate()
173
                          ->generateSlugsFrom('title')
174
                          ->saveSlugsTo('slug');
175
    }
176
177
    /**
178
     * Activate the page.
179
     *
180
     * @return $this
181
     */
182
    public function activate()
183
    {
184
        $this->update(['is_active' => true]);
185
186
        return $this;
187
    }
188
189
    /**
190
     * Deactivate the page.
191
     *
192
     * @return $this
193
     */
194
    public function deactivate()
195
    {
196
        $this->update(['is_active' => false]);
197
198
        return $this;
199
    }
200
}
201