Role::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 Gzero\EloquentTree\Model\Tree;
0 ignored issues
show
Bug introduced by
The type Gzero\EloquentTree\Model\Tree 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...
6
use Illuminate\Database\Eloquent\SoftDeletes;
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\Support\Traits\UccelloModule;
10
11
class Role extends Tree implements Searchable
12
{
13
    use SoftDeletes;
14
    use UccelloModule;
15
16
    protected $tablePrefix;
17
18
    /**
19
     * The table associated with the model.
20
     *
21
     * @var string
22
     */
23
    protected $table = 'roles';
24
25
    /**
26
     * The attributes that should be mutated to dates.
27
     *
28
     * @var array
29
     */
30
    protected $dates = [ 'deleted_at' ];
31
32
    /**
33
     * The attributes that are mass assignable.
34
     *
35
     * @var array
36
     */
37
    protected $fillable = [
38
        'name',
39
        'description',
40
        'parent_id',
41
        'domain_id'
42
    ];
43
44
    public $searchableType = 'role';
45
46
    public $searchableColumns = [
47
        'name'
48
    ];
49
50
    public static function boot()
51
    {
52
        parent::boot();
53
54
        // Linck to parent record
55
        static::created(function ($model) {
56
            static::linkToParentRecord($model);
57
        });
58
59
        // static::updatedParent(function ($model) {
60
        //     static::linkToParentRecord($model);
61
        // });
62
63
        static::updated(function ($model) {
64
            static::linkToParentRecord($model);
65
        });
66
    }
67
68
    public static function linkToParentRecord($model)
69
    {
70
        // Set parent record
71
        $parentRecord = Role::find(request('parent'));
72
        if (!is_null($parentRecord)) {
73
            with($model)->setChildOf($parentRecord);
74
        }
75
        // Remove parent domain
76
        else {
77
            with($model)->setAsRoot();
78
        }
79
    }
80
81
    /**
82
     * Check if node is root
83
     * This function check foreign key field
84
     *
85
     * @return bool
86
     */
87
    public function isRoot()
88
    {
89
        // return (empty($this->{$this->getTreeColumn('parent')})) ? true : false;
90
        return $this->{$this->getTreeColumn('path')} === $this->getKey() . '/'
91
                && $this->{$this->getTreeColumn('level')} === 0;
92
    }
93
94
    public function getSearchResult(): SearchResult
95
    {
96
        return new SearchResult(
97
            $this,
98
            $this->recordLabel
99
        );
100
    }
101
102
    public function __construct(array $attributes = [ ])
103
    {
104
        parent::__construct($attributes);
105
106
        // Init table prefix
107
        $this->initTablePrefix();
108
109
        // Init table name
110
        $this->initTableName();
111
112
        $this->addTreeEvents(); // Adding tree events
113
    }
114
115
    public function getTablePrefix()
116
    {
117
        return $this->tablePrefix;
118
    }
119
120
    protected function initTablePrefix()
121
    {
122
        $this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_');
123
    }
124
125
    protected function initTableName()
126
    {
127
        if ($this->table)
128
        {
129
            $this->table = $this->tablePrefix.$this->table;
130
        }
131
    }
132
133
    public function domain()
134
    {
135
        return $this->belongsTo(Domain::class);
136
    }
137
138
    public function privileges()
139
    {
140
        return $this->hasMany(Privilege::class);
141
    }
142
143
    public function profiles()
144
    {
145
        return $this->belongsToMany(Profile::class, $this->tablePrefix.'profiles_roles');
146
    }
147
148
    public function users()
149
    {
150
        return $this->belongsToMany(User::class, 'uccello_privileges');
151
    }
152
153
    /**
154
     * Returns record label
155
     *
156
     * @return string
157
     */
158
    public function getRecordLabelAttribute() : string
159
    {
160
        return $this->name;
161
    }
162
}
163