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

Permission   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 7 3
A tutorials() 0 3 1
A getTypeAttribute() 0 3 1
A delete() 0 11 3
A method() 0 5 1
A menu() 0 3 1
A roles() 0 3 1
A scopeImplicit() 0 3 1
1
<?php
2
3
namespace App\Models\enso\Permissions;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Route;
7
use App\Models\enso\Menus\Menu;
8
use LaravelEnso\Permissions\App\Enums\Types;
9
use LaravelEnso\Permissions\App\Enums\Verbs;
10
use LaravelEnso\Permissions\App\Exceptions\Permission as Exception;
11
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...
12
use LaravelEnso\Roles\App\Traits\HasRoles;
13
use LaravelEnso\Tables\App\Traits\TableCache;
14
use App\Models\Tutorials\Tutorial;
0 ignored issues
show
Bug introduced by
The type App\Models\Tutorials\Tutorial 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...
15
16
/**
17
 * @property int $id
18
 * @property string $name
19
 * @property string $description
20
 * @property boolean $is_default
21
 * @property string $created_at
22
 * @property string $updated_at
23
 * @property Menu[] $menuses
24
 * @property PermissionRole[] $permissionRoles
25
 * @property Tutorial[] $tutorials
26
 */
27
class Permission extends Model
28
{
29
    /**
30
     * The table associated with the model.
31
     * 
32
     * @var string
33
     */
34
    protected $table = 'permissions';
35
36
    use HasRoles, TableCache;
37
38
    protected $fillable = ['name', 'description', 'is_default'];
39
40
    protected $casts = ['is_default' => 'boolean'];
41
42
    public function menu()
43
    {
44
        return $this->hasOne(Menu::class);
45
    }
46
47
    public function roles()
48
    {
49
        return $this->belongsToMany(Role::class);
50
    }
51
52
    public function tutorials()
53
    {
54
        return $this->hasMany(Tutorial::class);
55
    }
56
57
    public function getTypeAttribute()
58
    {
59
        return $this->type();
60
    }
61
62
    public function type()
63
    {
64
        if ($this->relationLoaded('menu') && $this->menu !== null) {
65
            return Types::Menu;
66
        }
67
68
        return Verbs::get($this->method()) ?? Types::Link;
69
    }
70
71
    public function method()
72
    {
73
        $methods = optional(Route::getRoutes()->getByName($this->name))->methods();
74
75
        return $methods[0] ?? null;
76
    }
77
78
    public function scopeImplicit($query)
79
    {
80
        return $query->whereIsDefault(true);
81
    }
82
83
    public function delete()
84
    {
85
        if ($this->roles()->exists()) {
86
            throw Exception::roles();
87
        }
88
89
        if ($this->menu()->exists()) {
90
            throw Exception::menu();
91
        }
92
93
        parent::delete();
94
    }
95
}
96