Completed
Push — master ( f67f4f...77dcfd )
by Curtis
40s queued 12s
created

UserGroup::scopeVisible()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Models\enso\core;
4
use Illuminate\Database\Eloquent\Model;
5
use Illuminate\Support\Facades\Auth;
6
use LaravelEnso\Core\App\Exceptions\UserGroupConflict;
7
use LaravelEnso\Rememberable\App\Traits\Rememberable;
8
use App\Models\Roles\Role;
0 ignored issues
show
Bug introduced by
The type App\Models\Roles\Role 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...
9
use LaravelEnso\Roles\App\Traits\HasRoles;
10
use LaravelEnso\Tables\App\Traits\TableCache;
11
12
/**
13
 * @property int $id
14
 * @property string $name
15
 * @property string $description
16
 * @property string $created_at
17
 * @property string $updated_at
18
 * @property RoleUserGroup[] $roleUserGroups
19
 * @property User[] $users
20
 */
21
class UserGroup extends Model
22
{
23
    /**
24
     * The table associated with the model.
25
     * 
26
     * @var string
27
     */
28
    protected $table = 'user_groups';
29
    use HasRoles, Rememberable, TableCache;
0 ignored issues
show
Bug introduced by
The trait LaravelEnso\Rememberable\App\Traits\Rememberable requires the property $cacheLifetime which is not provided by App\Models\enso\core\UserGroup.
Loading history...
30
31
    protected $fillable = ['name', 'description'];
32
33
    public function roles()
34
    {
35
        return $this->belongsToMany(Role::class);
36
    }
37
38
    public function users()
39
    {
40
        return $this->hasMany(User::class, 'group_id');
41
    }
42
43
    public function delete()
44
    {
45
        if ($this->users()->exists()) {
46
            throw UserGroupConflict::hasUsers();
47
        }
48
49
        parent::delete();
50
    }
51
52
    public function scopeVisible($query)
53
    {
54
        return $query->when(
55
            ! Auth::user()->belongsToAdminGroup(),
0 ignored issues
show
Bug introduced by
The method belongsToAdminGroup() 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

55
            ! Auth::user()->/** @scrutinizer ignore-call */ belongsToAdminGroup(),
Loading history...
56
            fn ($query) => $query->whereId(Auth::user()->group_id)
0 ignored issues
show
Bug introduced by
Accessing group_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
57
        );
58
    }
59
}
60