Passed
Push — master ( a68981...55ae94 )
by Dimitri
02:53
created

SimplePageNoAutoSlug   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeVisibleInMenu() 0 3 1
A __construct() 0 5 1
A getIsPublicAttribute() 0 11 6
A scopePublished() 0 11 1
1
<?php
2
3
namespace Kurious7\SimplePages\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use Kurious7\SimplePages\Contracts\SimplePage as SimplePageContract;
10
11
class SimplePageNoAutoSlug extends Model implements SimplePageContract
12
{
13
    use SoftDeletes;
14
15
    protected $fillable = [
16
        'title',
17
        'slug',
18
        'content',
19
        'show_in_menu',
20
        'public',
21
        'public_from',
22
        'public_until',
23
    ];
24
25
    protected $casts = [
26
        'public' => 'boolean',
27
        'show_in_menu' => 'boolean',
28
    ];
29
30
    protected $dates = [
31
        'public_from',
32
        'public_until',
33
        'created_at',
34
        'updated_at',
35
        'deleted_at',
36
    ];
37
38
    public function __construct(array $attributes = [])
39
    {
40
        parent::__construct($attributes);
41
42
        $this->table = config('simple-pages.table', 'pages');
43
    }
44
45
    /**
46
     * Attributes.
47
     */
48
    protected function getIsPublicAttribute()
49
    {
50
        if (
51
            $this->public == 1
0 ignored issues
show
Bug introduced by
The property public does not seem to exist on Kurious7\SimplePages\Models\SimplePageNoAutoSlug. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
52
            || ($this->public_from && $this->public_from < new Carbon())
0 ignored issues
show
Bug introduced by
The property public_from does not seem to exist on Kurious7\SimplePages\Models\SimplePageNoAutoSlug. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
53
            || ($this->public_until && $this->public_until > new Carbon())
0 ignored issues
show
Bug introduced by
The property public_until does not seem to exist on Kurious7\SimplePages\Models\SimplePageNoAutoSlug. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
54
        ) {
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * Scopes.
63
     */
64
    public function scopeVisibleInMenu($query): Builder
65
    {
66
        return $query->where('show_in_menu', true);
67
    }
68
69
    public function scopePublished($query): Builder
70
    {
71
        return $query->where('public', true)
72
            ->where(function ($query) {
73
                $today = (new Carbon)->toDateString();
74
75
                $query->where(function ($query) use ($today) {
76
                    $query->whereDate('public_from', '<=', $today)->orWhereNull('public_from');
77
                })
78
                    ->where(function ($query) use ($today) {
79
                        $query->whereDate('public_until', '>=', $today)->orWhereNull('public_until');
80
                    });
81
            });
82
    }
83
}
84