Passed
Pull Request — 2.x (#729)
by Antonio Carlos
06:06
created

Model   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 55.26%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 88
ccs 21
cts 38
cp 0.5526
rs 10
c 1
b 0
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setPublishStartDateAttribute() 0 3 1
A isTranslationModel() 0 3 1
A scopePublished() 0 3 1
A scopeDraft() 0 3 1
A scopePublishedInListings() 0 7 2
A getTranslatedAttributes() 0 3 1
A scopeOnlyTrashed() 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 25
    protected function isTranslationModel()
21
    {
22 25
        return Str::endsWith(get_class($this), 'Translation');
23
    }
24
25 7
    public function scopePublished($query)
26
    {
27 7
        return $query->wherePublished(true);
28
    }
29
30
    public function scopePublishedInListings($query)
31
    {
32
        if ($this->isFillable('public')) {
33
            $query->wherePublic(true);
34
        }
35
36
        return $query->published()->visible();
37
    }
38
39
    public function scopeVisible($query)
40
    {
41
        if ($this->isFillable('publish_start_date')) {
42
            $query->where(function ($query) {
43
                $query->whereNull('publish_start_date')->orWhere('publish_start_date', '<=', Carbon::now());
44
            });
45
46
            if ($this->isFillable('publish_end_date')) {
47
                $query->where(function ($query) {
48
                    $query->whereNull('publish_end_date')->orWhere('publish_end_date', '>=', Carbon::now());
49
                });
50
            }
51
        }
52
53
        return $query;
54
    }
55
56 7
    public function setPublishStartDateAttribute($value)
57
    {
58 7
        $this->attributes['publish_start_date'] = $value ?? Carbon::now();
59 7
    }
60
61 33
    public function scopeDraft($query)
62
    {
63 33
        return $query->wherePublished(false);
64
    }
65
66
    public function scopeOnlyTrashed($query)
67
    {
68
        return $query->whereNotNull('deleted_at');
69
    }
70
71 55
    public function getFillable()
72
    {
73
        // If the fillable attribute is filled, just use it
74 55
        $fillable = $this->fillable;
75
76
        // If fillable is empty
77
        // and it's a translation model
78
        // and the baseModel was defined
79
        // Use the list of translatable attributes on our base model
80
        if (
81 55
            blank($fillable) &&
82 55
            $this->isTranslationModel() &&
83 55
            property_exists($this, 'baseModuleModel')
84
        ) {
85 4
            $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...
86
87 4
            if (!collect($fillable)->contains('locale')) {
88 4
                $fillable[] = 'locale';
89
            }
90
91 4
            if (!collect($fillable)->contains('active')) {
92
                $fillable[] = 'active';
93
            }
94
        }
95
96 55
        return $fillable;
97
    }
98
99 4
    public function getTranslatedAttributes()
100
    {
101 4
        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...
102
    }
103
}
104