GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 54c72b...e7e058 )
by Aden
03:11
created

ModelAdmin::getManagedModel()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Models;
4
5
use Illuminate\Support\Str;
6
use LaravelFlare\Flare\Admin\Admin;
7
use LaravelFlare\Flare\Exceptions\ModelAdminException;
8
use LaravelFlare\Flare\Traits\ModelAdmin\ModelWriting;
9
use LaravelFlare\Flare\Traits\ModelAdmin\ModelCloning;
10
use LaravelFlare\Flare\Traits\ModelAdmin\ModelQuerying;
11
use LaravelFlare\Flare\Traits\ModelAdmin\ModelValidating;
12
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelWriteable;
13
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelQueryable;
14
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelCloneable;
15
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelValidatable;
16
17
class ModelAdmin extends Admin implements ModelWriteable, ModelQueryable, ModelValidatable, ModelCloneable
18
{
19
    use ModelWriting;
20
    use ModelQuerying;
21
    use ModelValidating;
22
    use ModelCloning;
23
24
    /**
25
     * Class of Model to Manage.
26
     * 
27
     * @var string
28
     */
29
    protected $managedModel;
30
31
    /**
32
     * ModelAdmin Icon.
33
     *
34
     * Font Awesome Defined Icon, eg 'user' = 'fa-user'
35
     *
36
     * @var string
37
     */
38
    protected $icon = '';
39
40
    /**
41
     * Validation Rules for onCreate, onEdit actions.
42
     * 
43
     * @var array
44
     */
45
    protected $rules = [];
46
47
    /**
48
     * Columns for Model.
49
     *
50
     * Defines which fields to show in the listing tables output.
51
     * 
52
     * @var array
53
     */
54
    protected $columns = [];
55
56
    /**
57
     * Map Model Attributes to AttributeTypes with
58
     * additional parameters which will be output
59
     * as fields when viewing, editting or adding
60
     * a new model entry.
61
     * 
62
     * @var array
63
     */
64
    protected $fields = [];
65
66
    /**
67
     * Columns for Model are Sortable.
68
     *
69
     * @var bool
70
     */
71
    protected $sortable = true;
72
73
    /**
74
     * The Controller to be used by the Model Admin.
75
     *
76
     * This defaults to parent::getController()
77
     * if it has been left undefined. 
78
     * 
79
     * @var string
80
     */
81
    protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController';
82
83
    /**
84
     * The Policy used for the Model Authorization logic.
85
     *
86
     * This class should implement the ModelAdminPoliceable which
87
     * includes authorization checks for the create, view, edit and delete actions.
88
     * 
89
     * @var string
90
     */
91
    protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy';
92
93
    /**
94
     * The current model to be managed.
95
     * 
96
     * @var Model
97
     */
98
    public $model;
99
100
    /**
101
     * __construct.
102
     */
103
    public function __construct()
104
    {
105
        $this->getManagedModel();
106
107
        $this->model = $this->model();
108
    }
109
110
    /**
111
     * Returns a Model Instance.
112
     * 
113
     * @return Model
114
     */
115
    public function model()
116
    {
117
        if (!$this->model) {
118
            $class = $this->getManagedModel();
119
120
            return $this->model = new $class();
121
        }
122
123
        return $this->model;
124
    }
125
126
    /**
127
     * Returns a New Model Instance.
128
     *
129
     * @return Model
130
     */
131
    public function newModel()
132
    {
133
        $class = self::getManagedModel();
134
135
        return new $class();
136
    }
137
138
    /**
139
     * Returns the Managed Model Class.
140
     * 
141
     * @return string
142
     */
143
    public function getManagedModel()
144
    {
145
        if (!isset($this->managedModel) || $this->managedModel === null) {
146
            throw new ModelAdminException('You have a ModelAdmin which does not have a model assigned to it. ModelAdmins must include a model to manage.', 1);
147
        }
148
149
        return $this->managedModel;
150
    }
151
152
    /**
153
     * Set the Managed Model Class.
154
     * 
155
     * @param string $managedModel
156
     */
157
    public function setManagedModel($managedModel = null)
158
    {
159
        $this->managedModel = $managedModel;
160
    }
161
162
    /**
163
     * Returns the Route Paramets.
164
     * 
165
     * @return array
166
     */
167
    public function routeParameters()
168
    {
169
        return array_merge(parent::routeParameters(), [
170
                                                    'model' => $this->managedModel,
171
                                                ]);
172
    }
173
174
    /**
175
     * Formats and returns the Columns.
176
     *
177
     * This is really gross, I'm removing it soon.
178
     * 
179
     * @return
180
     */
181
    public function getColumns()
182
    {
183
        $columns = [];
184
185
        foreach ($this->columns as $field => $fieldTitle) {
186
            if (in_array($field, $this->model->getFillable())) {
187
                if (!$field) {
188
                    $field = $fieldTitle;
189
                    $fieldTitle = Str::title($fieldTitle);
190
                }
191
                $columns[$field] = $fieldTitle;
192
                continue;
193
            }
194
195
            // We can replace this with data_get() I believe.
196
            if (($methodBreaker = strpos($field, '.')) !== false) {
197
                $method = substr($field, 0, $methodBreaker);
198
                if (method_exists($this->model, $method)) {
199
                    if (method_exists($this->model->$method(), $submethod = str_replace($method.'.', '', $field))) {
200
                        $this->model->$method()->$submethod();
201
202
                        $columns[$field] = $fieldTitle;
203
                        continue;
204
                    }
205
                }
206
            }
207
208
            if (is_numeric($field)) {
209
                $field = $fieldTitle;
210
                $fieldTitle = Str::title($fieldTitle);
211
            }
212
213
            $columns[$field] = $fieldTitle;
214
        }
215
216
        if (count($columns)) {
217
            return $columns;
218
        }
219
220
        return [$this->model->getKeyName() => $this->model->getKeyName()];
221
    }
222
223
    /**
224
     * Gets an Attribute by the provided key
225
     * on either the current model or a provided model instance.
226
     * 
227
     * @param string $key
228
     * @param mixed  $model
229
     * 
230
     * @return mixed
231
     */
232
    public function getAttribute($key, $model = false)
233
    {
234
        if (!$model) {
235
            $model = $this->model;
236
        }
237
238
        if ($this->hasGetAccessor($key)) {
239
            $method = 'get'.Str::studly($key).'Attribute';
240
241
            return $this->{$method}($model);
242
        }
243
244
        if ($this->hasRelatedKey($key, $model)) {
245
            return $this->relatedKey($key, $model);
246
        }
247
248
        return $model->getAttribute($key);
249
    }
250
251
    /**
252
     * Determine if a get accessor exists for an attribute.
253
     *
254
     * @param string $key
255
     * 
256
     * @return bool
257
     */
258
    public function hasGetAccessor($key)
259
    {
260
        return method_exists($this, 'get'.Str::studly($key).'Attribute');
261
    }
262
263
    /**
264
     * Determines if a key resolved a related Model.
265
     * 
266
     * @param string $key
267
     * @param mixed  $model
268
     * 
269
     * @return bool
270
     */
271
    public function hasRelatedKey($key, $model = false)
272
    {
273
        if (!$model) {
274
            $model = $this->model;
275
        }
276
277
        if (($methodBreaker = strpos($key, '.')) !== false) {
278
            $method = substr($key, 0, $methodBreaker);
279
            if (method_exists($model, $method)) {
280
                return true;
281
            }
282
        }
283
284
        return false;
285
    }
286
287
    /**
288
     * Resolves a relation based on the key provided,
289
     * either on the current model or a provided model instance.
290
     * 
291
     * @param string $key
292
     * @param mixed  $model
293
     * 
294
     * @return mixed
295
     */
296
    public function relatedKey($key, $model = false)
297
    {
298
        if (!$model) {
299
            $model = $this->model;
300
        }
301
302
        if (($methodBreaker = strpos($key, '.')) !== false) {
303
            $method = substr($key, 0, $methodBreaker);
304
            if (method_exists($model, $method)) {
305
                if (method_exists($model->$method, $submethod = str_replace($method.'.', '', $key))) {
306
                    return $model->$method->$submethod();
307
                }
308
309
                if (isset($model->$method->$submethod)) {
310
                    return $model->$method->$submethod;
311
                }
312
313
                return $model->getRelationValue($method);
314
            }
315
        }
316
317
        return false;
318
    }
319
320
    /**
321
     * Set a given attribute on the model.
322
     *
323
     * @param string $key
324
     * @param mixed  $value
325
     */
326
    public function setAttribute($key, $value)
327
    {
328
        if ($this->hasSetMutator($key)) {
329
            $method = 'set'.Str::studly($key).'Attribute';
330
331
            return $this->{$method}($value);
332
        }
333
334
        $this->model->attributes[$key] = $value;
335
    }
336
337
    /**
338
     * Determine if a set mutator exists for an attribute.
339
     *
340
     * @param string $key
341
     * 
342
     * @return bool
343
     */
344
    public function hasSetMutator($key)
345
    {
346
        return method_exists($this, 'set'.Str::studly($key).'Attribute');
347
    }
348
349
    /**
350
     * Determine if a get mutator exists for an attribute.
351
     *
352
     * @param string $key
353
     * 
354
     * @return bool
355
     */
356
    public function hasGetMutator($key)
357
    {
358
        return method_exists($this, 'get'.Str::studly($key).'Attribute');
359
    }
360
361
    /**
362
     * Returns an array of Attribute Fields ready for output.
363
     *
364
     * @param  string $type 
365
     * 
366
     * @return array
367
     */
368
    public function outputFields($type = 'view')
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
369
    {
370
        return $this->getFields();
371
    }
372
373
    /**
374
     * Gets the Managed Model Mapping.
375
     * 
376
     * @return array
377
     */
378
    public function getFields()
379
    {
380
        return $this->fields;
381
    }
382
383
    /**
384
     * Gets the Managed Model Mapping.
385
     * 
386
     * @param array $fields
387
     */
388
    public function setFields($fields = [])
389
    {
390
        $this->fields = $fields;
391
    }
392
393
    /**
394
     * Determine if the Model Admin is sortable in it's list view.
395
     *
396
     * @param string $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
397
     * 
398
     * @return bool
399
     */
400
    public function isSortable()
401
    {
402
        return isset($this->sortable) && $this->sortable ? true : false;
403
    }
404
405
    /**
406
     * Determine if the Managed Model is using the SoftDeletes Trait.
407
     *
408
     * @return bool
409
     */
410
    public function hasSoftDeletes()
411
    {
412
        $managedModelClass = $this->getManagedModel();
413
414
        return in_array(
415
            \Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive(get_class(new $managedModelClass()))
416
        ) && in_array(
417
            \LaravelFlare\Flare\Traits\ModelAdmin\ModelSoftDeleting::class, class_uses_recursive(get_class($this))
418
        );
419
    }
420
}
421