Passed
Pull Request — 2.x (#1049)
by
unknown
08:35
created

Model   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 36.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 133
ccs 13
cts 36
cp 0.361
rs 10
wmc 25

12 Methods

Rating   Name   Duplication   Size   Complexity  
A scopePublished() 0 3 1
A isTranslationModel() 0 3 1
A scopeAccessible() 0 25 6
A scopeDraft() 0 3 1
A setPublishStartDateAttribute() 0 3 1
A getTranslatedAttributes() 0 3 1
A scopePublishedInListings() 0 7 2
A tags() 0 8 1
A bootTaggableTrait() 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\Services\Capsules\HasCapsules;
7
use A17\Twill\Models\Behaviors\IsTranslatable;
8
use A17\Twill\Models\Permission;
9
use Auth;
10
use Carbon\Carbon;
11
use Cartalyst\Tags\TaggableInterface;
12
use Cartalyst\Tags\TaggableTrait;
13
use Illuminate\Database\Eloquent\Model as BaseModel;
14
use Illuminate\Database\Eloquent\Relations\MorphToMany;
15
use Illuminate\Database\Eloquent\SoftDeletes;
16
use Illuminate\Support\Str;
17
18
abstract class Model extends BaseModel implements TaggableInterface
19
{
20 5
    use HasPresenter, SoftDeletes, TaggableTrait, IsTranslatable, HasCapsules;
0 ignored issues
show
Bug introduced by
The trait A17\Twill\Services\Capsules\HasCapsules requires the property $command which is not provided by A17\Twill\Models\Model.
Loading history...
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...
21
22 5
    public $timestamps = true;
23
24
    protected function isTranslationModel()
25
    {
26
        return Str::endsWith(get_class($this), 'Translation');
27
    }
28
29
    public function scopePublished($query)
30
    {
31
        return $query->wherePublished(true);
32
    }
33
34
    public function scopeAccessible($query)
35
    {
36
        $model = get_class($query->getModel());
37
        $moduleName = isPermissionableModule(getModuleNameByModel($model));
38
39
        if ($moduleName && !Auth::user()->isSuperAdmin()) {
0 ignored issues
show
Bug introduced by
The method isSuperAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        if ($moduleName && !Auth::user()->/** @scrutinizer ignore-call */ isSuperAdmin()) {
Loading history...
40
            // Get all permissions the logged in user has regards to the model.
41
            $allPermissions = Auth::user()->allPermissions();
0 ignored issues
show
Bug introduced by
The method allPermissions() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $allPermissions = Auth::user()->/** @scrutinizer ignore-call */ allPermissions();
Loading history...
42
            $allModelPermissions = (clone $allPermissions)->ofModel($model);
43
44
            // If the user has any module permissions, or global manage all modules permissions, all items will be return
45
            if ((clone $allModelPermissions)->module()->whereIn('name', Permission::available(Permission::SCOPE_MODULE))->exists()
46
                || (clone $allPermissions)->global()->where('name', 'manage-modules')->exists()) {
47
                return $query;
48
            }
49
50
            // If the module is submodule, skip the scope.
51 7
            if (strpos($moduleName, '.')) {
0 ignored issues
show
Bug introduced by
It seems like $moduleName can also be of type true; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            if (strpos(/** @scrutinizer ignore-type */ $moduleName, '.')) {
Loading history...
52
                return $query;
53 7
            };
54 7
55
            $authorizedItemsIds = $allModelPermissions->moduleItem()->pluck('permissionable_id');
56 31
            return $query->whereIn($this->getTable() . '.id', $authorizedItemsIds);
57
        }
58 31
        return $query;
59
    }
60
61
    public function scopePublishedInListings($query)
62
    {
63
        if ($this->isFillable('public')) {
64
            $query->wherePublic(true);
65
        }
66 51
67
        return $query->published()->visible();
68
    }
69 51
70
    public function scopeVisible($query)
71
    {
72
        if ($this->isFillable('publish_start_date')) {
73
            $query->where(function ($query) {
74
                $query->whereNull('publish_start_date')->orWhere('publish_start_date', '<=', Carbon::now());
75
            });
76 51
77 51
            if ($this->isFillable('publish_end_date')) {
78 51
                $query->where(function ($query) {
79
                    $query->whereNull('publish_end_date')->orWhere('publish_end_date', '>=', Carbon::now());
80
                });
81
            }
82
        }
83
84
        return $query;
85
    }
86
87
    public function setPublishStartDateAttribute($value)
88
    {
89
        $this->attributes['publish_start_date'] = $value ?? Carbon::now();
90
    }
91 51
92
    public function scopeDraft($query)
93
    {
94
        return $query->wherePublished(false);
95
    }
96
97
    public function scopeOnlyTrashed($query)
98
    {
99
        return $query->whereNotNull('deleted_at');
100
    }
101
102
    public function getFillable()
103
    {
104
        // If the fillable attribute is filled, just use it
105
        $fillable = $this->fillable;
106
107
        // If fillable is empty
108
        // and it's a translation model
109
        // and the baseModel was defined
110
        // Use the list of translatable attributes on our base model
111
        if (
112
            blank($fillable) &&
113
            $this->isTranslationModel() &&
114
            property_exists($this, 'baseModuleModel')
115
        ) {
116
            $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...
117
118
            if (!collect($fillable)->contains('locale')) {
119
                $fillable[] = 'locale';
120
            }
121
122
            if (!collect($fillable)->contains('active')) {
123
                $fillable[] = 'active';
124
            }
125
        }
126
127
        return $fillable;
128
    }
129
130
    public function getTranslatedAttributes()
131
    {
132
        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...
133
    }
134
135
    protected static function bootTaggableTrait()
136
    {
137
        static::$tagsModel = Tag::class;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function tags(): MorphToMany
144
    {
145
        return $this->morphToMany(
146
            static::$tagsModel,
147
            'taggable',
148
            config('twill.tagged_table', 'tagged'),
149
            'taggable_id',
150
            'tag_id'
151
        );
152
    }
153
}
154