Passed
Pull Request — master (#532)
by Antonio Carlos
10:20
created

Model::getTranslatedAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use Illuminate\Support\Str;
6
use A17\Twill\Models\Behaviors\HasPresenter;
7
use Carbon\Carbon;
8
use Cartalyst\Tags\TaggableInterface;
9
use Cartalyst\Tags\TaggableTrait;
10
use A17\Twill\Models\Behaviors\IsTranslatable;
11
use Illuminate\Database\Eloquent\Model as BaseModel;
12
use Illuminate\Database\Eloquent\SoftDeletes;
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 6
    public function setPublishStartDateAttribute($value)
52
    {
53 6
        $this->attributes['publish_start_date'] = $value ?? Carbon::now();
54 6
    }
55
56 5
    public function scopeDraft($query)
57
    {
58 5
        return $query->wherePublished(false);
59
    }
60
61
    public function scopeOnlyTrashed($query)
62
    {
63
        return $query->whereNotNull('deleted_at');
64
    }
65
66 47
    public function getFillable()
67
    {
68
        // If the fillable attribute is filled, just use it
69 47
        $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 47
            blank($fillable) &&
77 47
            Str::contains($class= get_class($this), 'Models\Translations') &&
78 47
            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
87 47
        return $fillable;
88
    }
89
90
    public function getTranslatedAttributes()
91
    {
92
        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...
93
    }
94
}
95