Passed
Push — master ( 7c331b...3c78de )
by Jonathan
18:06
created

Module::getDefaultRouteAttribute()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 23
rs 9.2222
cc 6
nc 6
nop 0
1
<?php
2
3
namespace Uccello\Core\Models;
4
5
use Illuminate\Support\Facades\Cache;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Cache 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 Uccello\Core\Database\Eloquent\Model;
7
8
class Module extends Model
9
{
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'modules';
16
17
    /**
18
     * The attributes that should be casted to native types.
19
     *
20
     * @var array
21
     */
22
    protected $casts = [
23
        'data' => 'object',
24
    ];
25
26
    /**
27
     * The attributes that are mass assignable.
28
     *
29
     * @var array
30
     */
31
    protected $fillable = [
32
        'name',
33
        'icon',
34
        'model_class',
35
        'data'
36
    ];
37
38
    protected function initTablePrefix()
39
    {
40
        $this->tablePrefix = env('UCCELLO_TABLE_PREFIX', 'uccello_');
0 ignored issues
show
Bug introduced by
The function env was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

40
        $this->tablePrefix = /** @scrutinizer ignore-call */ env('UCCELLO_TABLE_PREFIX', 'uccello_');
Loading history...
41
    }
42
43
    public function permissions()
44
    {
45
        return $this->hasMany(Permission::class);
46
    }
47
48
    public function domains()
49
    {
50
        return $this->belongsToMany(Domain::class, $this->tablePrefix.'domains_modules');
51
    }
52
53
    public function tabs()
54
    {
55
        return $this->hasMany(Tab::class)->orderBy('sequence');
56
    }
57
58
    public function blocks()
59
    {
60
        return $this->hasMany(Block::class)->orderBy('sequence');
61
    }
62
63
    public function fields()
64
    {
65
        return $this->hasMany(Field::class);
66
    }
67
68
    public function filters()
69
    {
70
        return $this->hasMany(Filter::class);
71
    }
72
73
    public function relatedlists()
74
    {
75
        return $this->hasMany(Relatedlist::class, 'module_id')->orderBy('sequence');
76
    }
77
78
    public function links()
79
    {
80
        return $this->hasMany(Link::class, 'module_id')->orderBy('sequence');
81
    }
82
83
    public function detailLinks()
84
    {
85
        return $this->hasMany(Link::class, 'module_id')->where('type', 'detail')->orderBy('sequence');
86
    }
87
88
    public function detailActionLinks()
89
    {
90
        return $this->hasMany(Link::class, 'module_id')->where('type', 'detail.action')->orderBy('sequence');
91
    }
92
93
    public function widgets()
94
    {
95
        return $this->belongsToMany(Widget::class, $this->tablePrefix.'modules_widgets')->withPivot('data');
96
    }
97
98
    /**
99
     * Returns module package name
100
     *
101
     * @return string|null
102
     */
103
    public function getPackageAttribute() : ?string
104
    {
105
        $package = ''; // For modules created directory in the host application
106
107
        // Get only package name if defined (Format: vendor/package)
108
        if (isset($this->data->package))
109
        {
110
            $packageData = explode('/', $this->data->package);
111
            $package = array_pop($packageData);
112
        }
113
114
        return $package;
115
    }
116
117
    /**
118
     * Return all module links to display in the menu
119
     *
120
     * @return array
121
     */
122
    public function getMenuLinksAttribute() : array
123
    {
124
        $menuLinks = [ ];
125
126
        //TODO: Adds capability needed
127
128
        if (isset($this->data->menu)) {
129
            // One route
130
            if (is_string($this->data->menu)) {
131
                $link = new \StdClass;
132
                $link->label = $this->name;
133
                $link->route = $this->data->menu;
134
                $link->icon = $this->icon;
135
                $menuLinks[ ] = $link;
136
            }
137
            // Several routes
138
            elseif (is_array($this->data->menu)) {
139
                foreach ($this->data->menu as $link) {
140
                    if (empty($link->icon)) {
141
                        $link->icon = $this->icon;
142
                    }
143
                    $menuLinks[ ] = $link;
144
                }
145
            }
146
            // No route wanted
147
            elseif ($this->data->menu === false) {
148
                // Nothing to do
149
            }
150
        }
151
        // No route defined, add it automaticaly
152
        else {
153
            $link = new \StdClass;
154
            $link->label = $this->name;
155
            $link->route = 'uccello.list';
156
            $link->icon = $this->icon;
157
            $menuLinks[ ] = $link;
158
        }
159
160
        return $menuLinks;
161
    }
162
163
    /**
164
     * Returns module's default route
165
     *
166
     * @return string
167
     */
168
    public function getDefaultRouteAttribute() : string
169
    {
170
        if (isset($this->data->menu)) {
171
            // One route
172
            if (is_string($this->data->menu)) {
173
                $defaultRoute = $this->data->menu;
174
            }
175
            // Several routes
176
            elseif (is_array($this->data->menu)) {
177
                if (!empty($this->data->menu[0]->route)) {
178
                    $defaultRoute = $this->data->menu[0]->route;
179
                }
180
            }
181
            // No route wanted
182
            elseif ($this->data->menu === false) {
183
                $defaultRoute = null;
184
            }
185
        } else {
186
            // No route defined, add it automaticaly
187
            $defaultRoute = 'uccello.list';
188
        }
189
190
        return $defaultRoute;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultRoute does not seem to be defined for all execution paths leading up to this point.
Loading history...
191
    }
192
193
    /**
194
     * Searches in the module a field by name.
195
     *
196
     * @param string $name
197
     * @return Field|null
198
     */
199
    public function getField($name) : ?Field
200
    {
201
        return $this->fields()->where('name', $name)->first();
202
    }
203
204
    /**
205
     * Checks if the module is active on a domain.
206
     *
207
     * @param Domain $domain
208
     * @return boolean
209
     */
210
    public function isActiveOnDomain(Domain $domain) : bool
211
    {
212
        // We don't use cache else it can be very huge with a lot of domains!!!
213
        return $this->domains()->where('domain_id', $domain->id)->count() > 0;
214
    }
215
216
    /**
217
     * Checks if the module is for administration.
218
     *
219
     * @return boolean
220
     */
221
    public function isAdminModule() : bool
222
    {
223
        return $this->data->admin ?? false;
224
    }
225
226
    /**
227
     * Check if the module is mandatory.
228
     *
229
     * @return boolean
230
     */
231
    public function isMandatory() : bool
232
    {
233
        return $this->data->mandatory ?? false;
234
    }
235
}
236