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

Role   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 113
rs 10
c 2
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeAccessible() 0 11 2
A getUsersCountAttribute() 0 3 1
A getCreatedAtAttribute() 0 3 1
A users() 0 3 1
A getAccessibleRolesAttribute() 0 3 1
A scopeDraft() 0 3 1
A scopeOnlyTrashed() 0 3 1
A scopePublished() 0 3 1
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use A17\Twill\Models\User;
6
use A17\Twill\Models\Behaviors\HasMedias;
7
use A17\Twill\Models\Behaviors\HasPermissions;
8
use A17\Twill\Models\Behaviors\IsTranslatable;
9
use A17\Twill\Models\Behaviors\HasPosition;
10
use A17\Twill\Models\Behaviors\Sortable;
11
use Illuminate\Database\Eloquent\Builder;
12
use Illuminate\Database\Eloquent\Collection;
13
use Illuminate\Database\Eloquent\SoftDeletes;
14
use Illuminate\Database\Eloquent\Model as BaseModel;
15
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
16
17
/**
18
 * Role model
19
 *
20
 * @property-read string $createdAt Date of creation
21
 * @property-read string $usersCount Formatted number of users in this role (ie. '123 users')
22
 * @method static Builder published() Get published roles
23
 * @method static Builder draft() Get draft roles
24
 * @method static Builder onlyTrashed() Get trashed roles
25
 */
26
class Role extends BaseModel implements Sortable
27
{
28
    use HasMedias, SoftDeletes, HasPermissions, IsTranslatable, HasPosition;
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\Role.
Loading history...
Bug introduced by
The trait A17\Twill\Models\Behaviors\HasPermissions requires the property $permissions which is not provided by A17\Twill\Models\Role.
Loading history...
introduced by
The trait A17\Twill\Models\Behaviors\HasMedias requires some properties which are not provided by A17\Twill\Models\Role: $medias, $role, $pivot, $metadatas, $uuid, $height, $lqip_data, $crop_h, $locale, $video, $crop, $crop_w, $width
Loading history...
29
30
    public $timestamps = true;
31
32
    protected $fillable = [
33
        'name',
34
        'published',
35
        'in_everyone_group',
36
        'position',
37
    ];
38
39
    protected $dates = [
40
        'deleted_at',
41
    ];
42
43
    public $checkboxes = ['published'];
44
45
    protected $casts = [
46
        'in_everyone_group' => 'boolean',
47
    ];
48
49
    /**
50
     * Scope accessible roles for the current user.
51
     *
52
     * @param Builder $query
53
     * @return Builder
54
     */
55
    public function scopeAccessible($query)
56
    {
57
        $currentUser = auth('twill_users')->user();
58
59
        if ($currentUser->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

59
        if ($currentUser->/** @scrutinizer ignore-call */ isSuperAdmin()) {
Loading history...
60
            return $query;
61
        }
62
63
        $accessibleRoleIds = $currentUser->role->accessibleRoles->pluck('id')->toArray();
0 ignored issues
show
Bug introduced by
Accessing role on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
64
65
        return $query->whereIn('id', $accessibleRoleIds);
66
    }
67
68
    /**
69
     * Scope published roles.
70
     *
71
     * @param Builder $query
72
     * @return Builder
73
     */
74
    public function scopePublished($query)
75
    {
76
        return $query->wherePublished(true);
77
    }
78
79
    /**
80
     * Scope unpublished (draft) roles.
81
     *
82
     * @param Builder $query
83
     * @return Builder
84
     */
85
    public function scopeDraft($query)
86
    {
87
        return $query->wherePublished(false);
88
    }
89
90
    /**
91
     * Scope trashed roles.
92
     *
93
     * @param Builder $query
94
     * @return Builder
95
     */
96
    public function scopeOnlyTrashed($query)
97
    {
98
        return $query->whereNotNull('deleted_at');
99
    }
100
101
    /**
102
     * User model relationship
103
     *
104
     * @return BelongsToMany|Collection|User[]
105
     */
106
    public function users()
107
    {
108
        return $this->hasMany(User::class);
109
    }
110
111
    /**
112
     * Return the formatted created date
113
     *
114
     * @return string
115
     */
116
    public function getCreatedAtAttribute($value)
117
    {
118
        return \Carbon\Carbon::parse($value)->format('d M Y');
119
    }
120
121
    /**
122
     * Return the formatted number of users in this group
123
     *
124
     * @return string
125
     */
126
    public function getUsersCountAttribute($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

126
    public function getUsersCountAttribute(/** @scrutinizer ignore-unused */ $value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        return $this->users->count() . ' users';
129
    }
130
131
    /**
132
     * Return the roles that are considered accessible for this role
133
     *
134
     * @return \Illuminate\Database\Eloquent\Collection
135
     */
136
    public function getAccessibleRolesAttribute()
137
    {
138
        return static::where('position', '>=', $this->position)->get();
139
    }
140
}
141