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
Branch master (9e3162)
by Dave
63:34
created

ModelConfiguration::getCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Model;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Model;
7
use SleepingOwl\Admin\Contracts\Initializable;
8
use SleepingOwl\Admin\Contracts\Form\FormInterface;
9
use SleepingOwl\Admin\Display\DisplayDatatablesAsync;
10
use SleepingOwl\Admin\Contracts\Display\DisplayInterface;
11
12
class ModelConfiguration extends ModelConfigurationManager
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $createTitle;
18
19
    /**
20
     * @var string
21
     */
22
    protected $editTitle;
23
24
    /**
25
     * @var Closure|null
26
     */
27
    protected $display;
28
29
    /**
30
     * @var Closure|null
31
     */
32
    protected $create;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $displayable = true;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $creatable = true;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $editable = true;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $restorable = true;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $deletable = true;
58
59
    /**
60
     * @var bool
61
     */
62
    protected $destroyable = true;
63
64
    /**
65
     * @var Closure|null
66
     */
67
    protected $edit;
68
69
    /**
70
     * @var Closure|null
71
     */
72
    protected $delete = true;
73
74
    /**
75
     * @var Closure|null
76
     */
77
    protected $destroy = true;
78
79
    /**
80
     * @var Closure|null
81
     */
82
    protected $restore = true;
83
84
    /**
85
     * @var string
86
     */
87
    protected $messageOnCreate;
88
89
    /**
90
     * @var string
91
     */
92
    protected $messageOnUpdate;
93
94
    /**
95
     * @var string
96
     */
97
    protected $messageOnDelete;
98
99
    /**
100
     * @var string
101
     */
102
    protected $messageOnDestroy;
103
104
    /**
105
     * @var string
106
     */
107
    protected $messageOnRestore;
108
109
    protected $breadcrumbs = null;
110
111 27
    public function __construct(\Illuminate\Contracts\Foundation\Application $app, $class)
112
    {
113 27
        parent::__construct($app, $class);
114 27
        $this->breadcrumbs = collect();
115 27
    }
116
117
    /**
118
     * @return \Illuminate\Support\Collection|null
119
     */
120
    public function getBreadCrumbs()
121
    {
122
        return $this->breadcrumbs->toArray();
123
    }
124
125
    /**
126
     * @param $breadcrumb
127
     * @return mixed|void
128
     */
129
    public function addBreadCrumb($breadcrumb)
130
    {
131
        $this->breadcrumbs->push($breadcrumb);
132
    }
133
134
    /**
135
     * @param string $alias
136
     *
137
     * @return $this
138
     */
139 1
    public function setAlias($alias)
140
    {
141 1
        $this->alias = $alias;
142
143 1
        return $this;
144
    }
145
146
    /**
147
     * @param string $title
148
     *
149
     * @return $this
150
     */
151 1
    public function setTitle($title)
152
    {
153 1
        $this->title = $title;
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * @return string|\Symfony\Component\Translation\TranslatorInterface
160
     */
161 1
    public function getCreateTitle()
162
    {
163 1
        if (is_null($this->createTitle)) {
0 ignored issues
show
introduced by
The condition is_null($this->createTitle) is always false.
Loading history...
164 1
            return parent::getCreateTitle();
165
        }
166
167 1
        return $this->createTitle;
168
    }
169
170
    /**
171
     * @param string $title
172
     *
173
     * @return $this
174
     */
175 1
    public function setCreateTitle($title)
176
    {
177 1
        $this->createTitle = $title;
178
179 1
        return $this;
180
    }
181
182
    /**
183
     * @param Model $model
184
     * @return string|\Symfony\Component\Translation\TranslatorInterface
185 1
     */
186
    public function getEditTitle()
187 1
    {
188 1
        if (is_null($this->editTitle)) {
0 ignored issues
show
introduced by
The condition is_null($this->editTitle) is always false.
Loading history...
189
            return parent::getEditTitle();
190
        }
191 1
192
        return $this->editTitle;
193
    }
194
195
    /**
196
     * @param string $title
197
     *
198
     * @return $this
199 1
     */
200
    public function setEditTitle($title)
201 1
    {
202
        $this->editTitle = $title;
203 1
204
        return $this;
205
    }
206
207
    /**
208
     * @return bool
209 1
     */
210
    public function isDisplayable()
211 1
    {
212
        return $this->displayable && parent::isDisplayable();
213
    }
214
215
    /**
216
     * @return $this
217 1
     */
218
    public function disableDisplay()
219 1
    {
220
        $this->displayable = false;
221 1
222
        return $this;
223
    }
224
225
    /**
226
     * @param string $action
227
     * @param \Illuminate\Database\Eloquent\Model $model
228
     *
229
     * @return bool
230 6
     */
231
    public function can($action, Model $model)
232 6
    {
233 6
        if (! $this->checkAccess) {
234
            return true;
235
        }
236
237
        return \Gate::allows($action, $model);
0 ignored issues
show
Bug introduced by
$model of type Illuminate\Database\Eloquent\Model is incompatible with the type array expected by parameter $| of Illuminate\Support\Facades\Gate::allows(). ( Ignorable by Annotation )

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

237
        return \Gate::allows($action, /** @scrutinizer ignore-type */ $model);
Loading history...
238
    }
239
240
    /**
241
     * @return bool
242 1
     */
243
    public function isCreatable()
244 1
    {
245 1
        if (! is_callable($this->getCreate())) {
246
            return false;
247
        }
248 1
249
        return $this->creatable && parent::isCreatable($this->getModel());
0 ignored issues
show
Unused Code introduced by
The call to SleepingOwl\Admin\Model\...nManager::isCreatable() has too many arguments starting with $this->getModel(). ( Ignorable by Annotation )

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

249
        return $this->creatable && parent::/** @scrutinizer ignore-call */ isCreatable($this->getModel());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
250
    }
251
252
    /**
253
     * @return $this
254 1
     */
255
    public function disableCreating()
256 1
    {
257
        $this->creatable = false;
258 1
259
        return $this;
260
    }
261
262
    /**
263
     * @param Model $model
264
     *
265
     * @return bool
266 1
     */
267
    public function isEditable(Model $model)
268 1
    {
269 1
        if (! is_callable($this->getEdit())) {
270
            return false;
271
        }
272 1
273
        return $this->editable && parent::isEditable($model);
274
    }
275
276
    /**
277
     * @return $this
278 1
     */
279
    public function disableEditing()
280 1
    {
281
        $this->editable = false;
282 1
283
        return $this;
284
    }
285
286
    /**
287
     * @param bool $deletable
288
     *
289
     * @return $this
290 1
     */
291
    public function setDeletable($deletable)
292 1
    {
293
        $this->deletable = (bool) $deletable;
294 1
295
        return $this;
296
    }
297
298
    /**
299
     * @param Model $model
300
     *
301
     * @return bool
302 1
     */
303
    public function isDeletable(Model $model)
304 1
    {
305
        return $this->deletable && parent::isDeletable($model);
306
    }
307
308
    /**
309
     * @return $this
310 1
     */
311
    public function disableDeleting()
312 1
    {
313
        $this->setDeletable(false);
314 1
315
        return $this;
316
    }
317
318
    /**
319
     * @param Model $model
320
     *
321
     * @return bool
322 3
     */
323
    public function isDestroyable(Model $model)
324 3
    {
325
        return $this->destroyable && parent::isDestroyable($model);
326
    }
327
328
    /**
329
     * @return $this
330 1
     */
331
    public function disableDestroying()
332 1
    {
333
        $this->destroyable = false;
334 1
335
        return $this;
336
    }
337
338
    /**
339
     * @param Model $model
340
     *
341
     * @return bool
342 1
     */
343
    public function isRestorable(Model $model)
344 1
    {
345
        return $this->restorable && parent::isRestorable($model);
346
    }
347
348
    /**
349
     * @return bool
350 6
     */
351
    public function isRestorableModel()
352 6
    {
353
        return $this->restorable && parent::isRestorableModel();
354
    }
355
356
    /**
357
     * @return $this
358 1
     */
359
    public function disableRestoring()
360 1
    {
361
        $this->restorable = false;
362 1
363
        return $this;
364
    }
365
366
    /**
367
     * @return Closure|null
368 1
     */
369
    public function getDisplay()
370 1
    {
371
        return $this->display;
372
    }
373
374
    /**
375
     * @param Closure $callback
376
     *
377
     * @return $this
378 1
     */
379
    public function onDisplay(Closure $callback)
380 1
    {
381
        $this->display = $callback;
382 1
383
        return $this;
384
    }
385
386
    /**
387
     * @param array|null $payload
388
     * @return DisplayInterface|mixed
389 1
     */
390
    public function fireDisplay(array $payload = [])
391 1
    {
392
        if (! is_callable($this->getDisplay())) {
393
            return;
394
        }
395 1
396
        $display = $this->app->call($this->getDisplay(), $payload);
397 1
398
        if ($display instanceof DisplayDatatablesAsync) {
399
            $display->setPayload($payload);
400
        }
401 1
402 1
        if ($display instanceof DisplayInterface) {
403 1
            $display->setModelClass($this->getClass());
404
        }
405 1
406 1
        if ($display instanceof Initializable) {
407 1
            $display->initialize();
408
        }
409 1
410
        return $display;
411
    }
412
413
    /**
414
     * @return Closure|null
415 3
     */
416
    public function getCreate()
417 3
    {
418
        return $this->create;
419
    }
420
421
    /**
422
     * @param Closure|null $callback
423
     *
424
     * @return $this
425 3
     */
426
    public function onCreate(Closure $callback = null)
427 3
    {
428
        $this->create = $callback;
429 3
430
        return $this;
431
    }
432
433
    /**
434
     * @return mixed|void
435 1
     */
436
    public function fireCreate()
437 1
    {
438
        if (! is_callable($this->getCreate())) {
439
            return;
440
        }
441 1
442 1
        $form = $this->app->call($this->getCreate());
443 1
        if ($form instanceof DisplayInterface) {
444 1
            $form->setModelClass($this->getClass());
445
        }
446 1
447 1
        if ($form instanceof FormInterface) {
448 1
            $form->setAction($this->getStoreUrl());
449
        }
450 1
451 1
        if ($form instanceof Initializable) {
452 1
            $form->initialize();
453
        }
454 1
455
        return $form;
456
    }
457
458
    /**
459
     * @return Closure|null
460 3
     */
461
    public function getEdit()
462 3
    {
463
        return $this->edit;
464
    }
465
466
    /**
467
     * @param Closure|null $callback
468
     *
469
     * @return $this
470 3
     */
471
    public function onEdit(Closure $callback = null)
472 3
    {
473
        $this->edit = $callback;
474 3
475
        return $this;
476
    }
477
478
    /**
479
     * @param int|string $id
480
     *
481
     * @return mixed|void
482 1
     */
483
    public function fireEdit($id)
484 1
    {
485
        if (! is_callable($this->getEdit())) {
486
            return;
487
        }
488 1
489 1
        $form = $this->app->call($this->getEdit(), ['id' => $id]);
490 1
        if ($form instanceof DisplayInterface) {
491 1
            $form->setModelClass($this->getClass());
492
        }
493 1
494 1
        if ($form instanceof FormInterface) {
495 1
            $form->setAction($this->getUpdateUrl($id));
496
        }
497 1
498 1
        if ($form instanceof Initializable) {
499 1
            $form->initialize();
500
        }
501 1
502 1
        if ($form instanceof FormInterface) {
503 1
            $form->setId($id);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type string; however, parameter $id of SleepingOwl\Admin\Contra...\FormInterface::setId() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

503
            $form->setId(/** @scrutinizer ignore-type */ $id);
Loading history...
504
        }
505 1
506
        return $form;
507
    }
508
509
    /**
510
     * @param Closure|null $callback
511
     *
512
     * @return $this
513 1
     */
514
    public function onCreateAndEdit(Closure $callback = null)
515 1
    {
516 1
        $this->onCreate($callback);
517
        $this->onEdit($callback);
518 1
519
        return $this;
520
    }
521
522
    /**
523
     * @return Closure|null
524 1
     */
525
    public function getDelete()
526 1
    {
527
        return $this->delete;
528
    }
529
530
    /**
531
     * @param Closure|null $callback
532
     *
533
     * @return $this
534 1
     */
535
    public function onDelete(Closure $callback = null)
536 1
    {
537
        $this->delete = $callback;
538 1
539
        return $this;
540
    }
541
542
    /**
543
     * @param int|string $id
544
     *
545
     * @return mixed
546 1
     */
547
    public function fireDelete($id)
548 1
    {
549 1
        if (is_callable($this->getDelete())) {
550
            return $this->app->call($this->getDelete(), [$id]);
551
        }
552
    }
553
554
    /**
555
     * @return Closure|null
556 1
     */
557
    public function getDestroy()
558 1
    {
559
        return $this->destroy;
560
    }
561
562
    /**
563
     * @param Closure|null $callback
564
     *
565
     * @return $this
566 1
     */
567
    public function onDestroy(Closure $callback = null)
568 1
    {
569
        $this->destroy = $callback;
570 1
571
        return $this;
572
    }
573
574
    /**
575
     * @param int|string $id
576
     *
577
     * @return mixed
578 1
     */
579
    public function fireDestroy($id)
580 1
    {
581 1
        if (is_callable($this->getDestroy())) {
582
            return $this->app->call($this->getDestroy(), [$id]);
583
        }
584
    }
585
586
    /**
587
     * @return Closure|null
588 1
     */
589
    public function getRestore()
590 1
    {
591
        return $this->restore;
592
    }
593
594
    /**
595
     * @param Closure|null $callback
596
     *
597
     * @return $this
598 1
     */
599
    public function onRestore(Closure $callback = null)
600 1
    {
601
        $this->restore = $callback;
602 1
603
        return $this;
604
    }
605
606
    /**
607
     * @param int|string $id
608
     *
609
     * @return bool|mixed
610 1
     */
611
    public function fireRestore($id)
612 1
    {
613 1
        if (is_callable($this->getRestore())) {
614
            return $this->app->call($this->getRestore(), [$id]);
615
        }
616
    }
617
618
    /**
619
     * @return string
620 1
     */
621
    public function getMessageOnCreate()
622 1
    {
623 1
        if (is_null($this->messageOnCreate)) {
0 ignored issues
show
introduced by
The condition is_null($this->messageOnCreate) is always false.
Loading history...
624 1
            $this->messageOnCreate = parent::getMessageOnCreate();
625
        }
626 1
627
        return $this->messageOnCreate;
628
    }
629
630
    /**
631
     * @param string $messageOnCreate
632
     *
633
     * @return $this
634 1
     */
635
    public function setMessageOnCreate($messageOnCreate)
636 1
    {
637
        $this->messageOnCreate = $messageOnCreate;
638 1
639
        return $this;
640
    }
641
642
    /**
643
     * @return string
644 1
     */
645
    public function getMessageOnUpdate()
646 1
    {
647 1
        if (is_null($this->messageOnUpdate)) {
0 ignored issues
show
introduced by
The condition is_null($this->messageOnUpdate) is always false.
Loading history...
648 1
            $this->messageOnUpdate = parent::getMessageOnUpdate();
649
        }
650 1
651
        return $this->messageOnUpdate;
652
    }
653
654
    /**
655
     * @param string $messageOnUpdate
656
     *
657
     * @return $this
658 1
     */
659
    public function setMessageOnUpdate($messageOnUpdate)
660 1
    {
661
        $this->messageOnUpdate = $messageOnUpdate;
662 1
663
        return $this;
664
    }
665
666
    /**
667
     * @return string
668 1
     */
669
    public function getMessageOnDelete()
670 1
    {
671 1
        if (is_null($this->messageOnDelete)) {
0 ignored issues
show
introduced by
The condition is_null($this->messageOnDelete) is always false.
Loading history...
672 1
            $this->messageOnDelete = parent::getMessageOnDelete();
673
        }
674 1
675
        return $this->messageOnDelete;
676
    }
677
678
    /**
679
     * @param string $messageOnDelete
680
     *
681
     * @return $this
682 1
     */
683
    public function setMessageOnDelete($messageOnDelete)
684 1
    {
685
        $this->messageOnDelete = $messageOnDelete;
686 1
687
        return $this;
688
    }
689
690
    /**
691
     * @return string
692 1
     */
693
    public function getMessageOnDestroy()
694 1
    {
695 1
        if (is_null($this->messageOnDestroy)) {
0 ignored issues
show
introduced by
The condition is_null($this->messageOnDestroy) is always false.
Loading history...
696 1
            $this->messageOnDestroy = parent::getMessageOnDestroy();
697
        }
698 1
699
        return $this->messageOnDestroy;
700
    }
701
702
    /**
703
     * @param string $messageOnDestroy
704
     *
705
     * @return $this
706 1
     */
707
    public function setMessageOnDestroy($messageOnDestroy)
708 1
    {
709
        $this->messageOnDestroy = $messageOnDestroy;
710 1
711
        return $this;
712
    }
713
714
    /**
715
     * @return string
716 1
     */
717
    public function getMessageOnRestore()
718 1
    {
719 1
        if (is_null($this->messageOnRestore)) {
0 ignored issues
show
introduced by
The condition is_null($this->messageOnRestore) is always false.
Loading history...
720 1
            $this->messageOnRestore = parent::getMessageOnRestore();
721
        }
722 1
723
        return $this->messageOnRestore;
724
    }
725
726
    /**
727
     * @param string $messageOnRestore
728
     *
729
     * @return $this
730 1
     */
731
    public function setMessageOnRestore($messageOnRestore)
732 1
    {
733
        $this->messageOnRestore = $messageOnRestore;
734 1
735
        return $this;
736
    }
737
}
738