Profile::initTablePrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Models;
4
5
use Illuminate\Database\Eloquent\SoftDeletes;
6
use Illuminate\Support\Collection;
7
use Spatie\Searchable\Searchable;
0 ignored issues
show
Bug introduced by
The type Spatie\Searchable\Searchable 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...
8
use Spatie\Searchable\SearchResult;
0 ignored issues
show
Bug introduced by
The type Spatie\Searchable\SearchResult 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 Uccello\Core\Database\Eloquent\Model;
10
use Uccello\Core\Support\Traits\UccelloModule;
11
12
class Profile extends Model implements Searchable
13
{
14
    use SoftDeletes;
15
    use UccelloModule;
16
17
    /**
18
     * The table associated with the model.
19
     *
20
     * @var string
21
     */
22
    protected $table = 'profiles';
23
24
    /**
25
     * The attributes that should be mutated to dates.
26
     *
27
     * @var array
28
     */
29
    protected $dates = [ 'deleted_at' ];
30
31
    /**
32
     * The attributes that are mass assignable.
33
     *
34
     * @var array
35
     */
36
    protected $fillable = [
37
        'name',
38
        'description',
39
        'domain_id'
40
    ];
41
42
    public $searchableType = 'profile';
43
44
    public $searchableColumns = [
45
        'name'
46
    ];
47
48
    public function getSearchResult(): SearchResult
49
    {
50
        return new SearchResult(
51
            $this,
52
            $this->recordLabel
53
        );
54
    }
55
56
    protected function initTablePrefix()
57
    {
58
        $this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_');
59
    }
60
61
    public function domain()
62
    {
63
        return $this->belongsTo(Domain::class);
64
    }
65
66
    public function roles()
67
    {
68
        return $this->belongsToMany(Role::class, $this->tablePrefix.'profiles_roles');
69
    }
70
71
    public function permissions()
72
    {
73
        return $this->hasMany(Permission::class);
74
    }
75
76
    /**
77
     * Returns record label
78
     *
79
     * @return string
80
     */
81
    public function getRecordLabelAttribute() : string
82
    {
83
        return $this->name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Profile. 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...
84
    }
85
86
    /**
87
     * Returns profile capabilities on a module
88
     *
89
     * @param Module $module
90
     * @return \Illuminate\Support\Collection
91
     */
92
    public function capabilitiesOnModule(Module $module) : Collection
93
    {
94
        $capabilities = new Collection();
95
96
        // Get profile permissions on module
97
        $permissions = $this->permissions->where('module_id', $module->id);
98
99
        foreach ($permissions as $permission) {
100
            $capabilities[ ] = $permission->capability;
101
        }
102
103
        return $capabilities->unique();
104
    }
105
106
    /**
107
     * Checks if the profil has a capability on a module
108
     *
109
     * @param Capability $capability
110
     * @param Module $module
111
     * @return boolean
112
     */
113
    public function hasCapabilityOnModule(Capability $capability, Module $module) : bool
114
    {
115
        $hasCapability = false;
116
117
        foreach ($this->capabilitiesOnModule($module) as $capabilityOnModule) {
118
            if ($capabilityOnModule->id === $capability->id) {
119
                $hasCapability = true;
120
                break;
121
            }
122
        }
123
124
        return $hasCapability;
125
    }
126
}
127