Passed
Pull Request — 2.x (#727)
by Bekzat
16:53
created

Model   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 36.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 83
ccs 13
cts 36
cp 0.361
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A scopePublished() 0 3 1
A setPublishStartDateAttribute() 0 3 1
A scopeDraft() 0 3 1
A scopePublishedInListings() 0 7 2
A scopeOnlyTrashed() 0 3 1
A getTranslatedAttributes() 0 3 1
A getFillable() 0 26 6
A scopeVisible() 0 15 3
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use A17\Twill\Models\Behaviors\HasPresenter;
6
use A17\Twill\Models\Behaviors\IsTranslatable;
7
use Carbon\Carbon;
8
use Cartalyst\Tags\TaggableInterface;
9
use Cartalyst\Tags\TaggableTrait;
10
use Illuminate\Database\Eloquent\Model as BaseModel;
11
use Illuminate\Database\Eloquent\SoftDeletes;
12
use Illuminate\Support\Str;
13
14
abstract class Model extends BaseModel implements TaggableInterface
15
{
16
    use HasPresenter, SoftDeletes, TaggableTrait, IsTranslatable;
0 ignored issues
show
Bug introduced by
The trait A17\Twill\Models\Behaviors\IsTranslatable requires the property $translatedAttributes which is not provided by A17\Twill\Models\Model.
Loading history...
introduced by
The trait Cartalyst\Tags\TaggableTrait requires some properties which are not provided by A17\Twill\Models\Model: $tags, $count
Loading history...
17
18
    public $timestamps = true;
19
20 5
    public function scopePublished($query)
21
    {
22 5
        return $query->wherePublished(true);
23
    }
24
25
    public function scopePublishedInListings($query)
26
    {
27
        if ($this->isFillable('public')) {
28
            $query->wherePublic(true);
29
        }
30
31
        return $query->published()->visible();
32
    }
33
34
    public function scopeVisible($query)
35
    {
36
        if ($this->isFillable('publish_start_date')) {
37
            $query->where(function ($query) {
38
                $query->whereNull('publish_start_date')->orWhere('publish_start_date', '<=', Carbon::now());
39
            });
40
41
            if ($this->isFillable('publish_end_date')) {
42
                $query->where(function ($query) {
43
                    $query->whereNull('publish_end_date')->orWhere('publish_end_date', '>=', Carbon::now());
44
                });
45
            }
46
        }
47
48
        return $query;
49
    }
50
51 7
    public function setPublishStartDateAttribute($value)
52
    {
53 7
        $this->attributes['publish_start_date'] = $value ?? Carbon::now();
54 7
    }
55
56 31
    public function scopeDraft($query)
57
    {
58 31
        return $query->wherePublished(false);
59
    }
60
61
    public function scopeOnlyTrashed($query)
62
    {
63
        return $query->whereNotNull('deleted_at');
64
    }
65
66 51
    public function getFillable()
67
    {
68
        // If the fillable attribute is filled, just use it
69 51
        $fillable = $this->fillable;
70
71
        // If fillable is empty
72
        // and it's a translation model
73
        // and the baseModel was defined
74
        // Use the list of translatable attributes on our base model
75
        if (
76 51
            blank($fillable) &&
77 51
            Str::contains($class = get_class($this), 'Models\Translations') &&
78 51
            property_exists($class, 'baseModuleModel')
79
        ) {
80
            $fillable = (new $this->baseModuleModel)->getTranslatedAttributes();
0 ignored issues
show
Bug introduced by
The property baseModuleModel does not seem to exist on A17\Twill\Models\Model. 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...
81
82
            if (!collect($fillable)->contains('locale')) {
83
                $fillable[] = 'locale';
84
            }
85
86
            if (!collect($fillable)->contains('active')) {
87
                $fillable[] = 'active';
88
            }
89
        }
90
91 51
        return $fillable;
92
    }
93
94
    public function getTranslatedAttributes()
95
    {
96
        return $this->translatedAttributes ?? [];
0 ignored issues
show
Bug introduced by
The property translatedAttributes does not seem to exist on A17\Twill\Models\Model. 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...
97
    }
98
}
99