Passed
Push — feature/permission-manager ( 41e93d )
by
unknown
09:00
created

Group   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 168
rs 10
c 1
b 0
f 0
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A scopePublished() 0 3 1
A getTitleInBrowserAttribute() 0 3 1
A getEveryoneGroup() 0 3 1
A permissionableIds() 0 3 1
A scopeDraft() 0 3 1
A getUsersCountAttribute() 0 3 1
A viewableItems() 0 5 1
A getCanPublishAttribute() 0 3 1
A permissionableItems() 0 5 1
A getCreatedAtAttribute() 0 3 1
A isEveryoneGroup() 0 3 1
A users() 0 3 1
A scopeOnlyTrashed() 0 3 1
A getCanEditAttribute() 0 3 1
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use A17\Twill\Models\Behaviors\HasPermissions;
8
use A17\Twill\Models\Behaviors\IsTranslatable;
9
use Illuminate\Database\Eloquent\Model as BaseModel;
10
11
/**
12
 * Group model
13
 *
14
 * @property-read string $titleInBrowser Title
15
 * @property-read string $createdAt Date of creation
16
 * @property-read boolean $canEdit Check if the group is editable (ie. not the Everyone group)
17
 * @property-read boolean $canPublish Check if the group is publishable (ie. not the Everyone group)
18
 * @property-read string $usersCount Formatted number of users in this group (ie. '123 users')
19
 * @method static Builder published() Get published groups
20
 * @method static Builder draft() Get draft groups
21
 * @method static Builder onlyTrashed() Get trashed groups
22
 */
23
class Group extends BaseModel
24
{
25
    use HasPermissions, SoftDeletes, IsTranslatable;
0 ignored issues
show
Bug introduced by
The trait A17\Twill\Models\Behaviors\HasPermissions requires the property $permissions which is not provided by A17\Twill\Models\Group.
Loading history...
Bug introduced by
The trait A17\Twill\Models\Behaviors\IsTranslatable requires the property $translatedAttributes which is not provided by A17\Twill\Models\Group.
Loading history...
26
27
    public $timestamps = true;
28
29
    protected $fillable = [
30
        'name',
31
        'description',
32
        'published',
33
        'subdomains_access',
34
    ];
35
36
    protected $dates = [
37
        'deleted_at',
38
    ];
39
40
    public $checkboxes = ['published'];
41
42
    protected $casts = [
43
        'subdomains_access' => 'array',
44
    ];
45
46
    /**
47
     * Return the Everyone group
48
     *
49
     * @return BaseModel
50
     */
51
    public static function getEveryoneGroup()
52
    {
53
        return Group::where([['is_everyone_group', true], ['name', 'Everyone']])->firstOrFail();
54
    }
55
56
    /**
57
     * Return the group title
58
     *
59
     * @return string
60
     */
61
    public function getTitleInBrowserAttribute()
62
    {
63
        return $this->name;
64
    }
65
66
    /**
67
     * Scope published groups.
68
     *
69
     * @param Builder $query
70
     * @return Builder
71
     */
72
    public function scopePublished($query)
73
    {
74
        return $query->wherePublished(true);
75
    }
76
77
    /**
78
     * Scope unpublished (draft) groups.
79
     *
80
     * @param Builder $query
81
     * @return Builder
82
     */
83
    public function scopeDraft($query)
84
    {
85
        return $query->wherePublished(false);
86
    }
87
88
    /**
89
     * Scope trashed groups.
90
     *
91
     * @param Builder $query
92
     * @return Builder
93
     */
94
    public function scopeOnlyTrashed($query)
95
    {
96
        return $query->whereNotNull('deleted_at');
97
    }
98
99
    /**
100
     * User model relationship
101
     *
102
     * @return BelongsToMany|Collection|User[]
0 ignored issues
show
Bug introduced by
The type A17\Twill\Models\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type A17\Twill\Models\BelongsToMany was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
     */
104
    public function users()
105
    {
106
        return $this->belongsToMany(twillModel('user'), 'group_twill_user', 'group_id', 'twill_user_id');
107
    }
108
109
    /**
110
     * Check if current group is the Everyone group
111
     *
112
     * @return boolean
113
     */
114
    public function isEveryoneGroup()
115
    {
116
        return $this->id === $this->getEveryoneGroup()->id;
117
    }
118
119
    /**
120
     * Return the formatted created date
121
     *
122
     * @return string
123
     */
124
    public function getCreatedAtAttribute($value)
125
    {
126
        return \Carbon\Carbon::parse($value)->format('d M Y');
127
    }
128
129
    /**
130
     * Check if the group can be edited (not a system group, ie. Everyone)
131
     *
132
     * @return boolean
133
     */
134
    public function getCanEditAttribute()
135
    {
136
        return !$this->isEveryoneGroup();
137
    }
138
139
    /**
140
     * Check if the group can be published (not a system group, ie. Everyone)
141
     *
142
     * @return boolean
143
     */
144
    public function getCanPublishAttribute()
145
    {
146
        return !$this->isEveryoneGroup();
147
    }
148
149
    /**
150
     * Return the formatted number of users in this group
151
     *
152
     * @return string
153
     */
154
    public function getUsersCountAttribute()
155
    {
156
        return $this->users->count() . ' users';
0 ignored issues
show
Bug introduced by
The property users does not exist on A17\Twill\Models\Group. Did you mean usersCount?
Loading history...
157
    }
158
159
    /**
160
     * Return viewable items
161
     *
162
     * @return Collection
163
     */
164
    public function viewableItems()
165
    {
166
        return Permission::where('name', 'view-item')->whereHas('groups', function ($query) {
167
            $query->where('id', $this->id);
168
        })->with('permissionable')->get()->pluck('permissionable');
169
    }
170
171
    /**
172
     * Return ids of permissionable items
173
     *
174
     * @return int[]
175
     */
176
    public function permissionableIds()
177
    {
178
        return $this->permissions->pluck('id')->toArray();
0 ignored issues
show
Bug introduced by
The property permissions does not seem to exist on A17\Twill\Models\Group. 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...
179
    }
180
181
    /**
182
     * Return permissionable items
183
     *
184
     * @return Collection
185
     */
186
    public function permissionableItems()
187
    {
188
        return Permission::whereHas('groups', function ($query) {
189
            $query->where('id', $this->id);
190
        })->with('permissionable')->get()->pluck('permissionable');
191
    }
192
}
193