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.

Issues (389)

Branch: master

src/Model/ModelConfigurationManager.php (9 issues)

1
<?php
2
3
namespace SleepingOwl\Admin\Model;
4
5
use BadMethodCallException;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Str;
9
use KodiComponents\Navigation\Contracts\BadgeInterface;
10
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
11
use SleepingOwl\Admin\Contracts\Repositories\RepositoryInterface;
12
use SleepingOwl\Admin\Navigation;
13
use SleepingOwl\Admin\Navigation\Badge;
14
use SleepingOwl\Admin\Navigation\Page;
15
16
/**
17
 * @method bool creating(\Closure $callback)
18
 * @method void created(\Closure $callback)
19
 * @method bool updating(\Closure $callback)
20
 * @method void updated(\Closure $callback)
21
 * @method bool deleting(\Closure $callback)
22
 * @method void deleted(\Closure $callback)
23
 * @method bool restoring(\Closure $callback)
24
 * @method void restored(\Closure $callback)
25
 */
26
abstract class ModelConfigurationManager implements ModelConfigurationInterface
27
{
28
    /**
29
     * Get the event dispatcher instance.
30
     *
31
     * @return \Illuminate\Contracts\Events\Dispatcher
32
     */
33
    public static function getEventDispatcher()
34
    {
35
        return self::$dispatcher;
36
    }
37
38
    /**
39
     * Set the event dispatcher instance.
40
     *
41
     * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
42
     * @return void
43 285
     */
44
    public static function setEventDispatcher(Dispatcher $dispatcher)
45 285
    {
46 285
        self::$dispatcher = $dispatcher;
47
    }
48
49
    /**
50
     * The event dispatcher instance.
51
     *
52
     * @var \Illuminate\Contracts\Events\Dispatcher
53
     */
54
    protected static $dispatcher;
55
56
    /**
57
     * @var \Illuminate\Contracts\Foundation\Application
58
     */
59
    protected $app;
60
61
    /**
62
     * @var string
63
     */
64
    protected $class;
65
66
    /**
67
     * @var Model
68
     */
69
    protected $model;
70
71
    /**
72
     * @var string
73
     */
74
    protected $alias;
75
76
    /**
77
     * @var string|null
78
     */
79
    protected $controllerClass;
80
81
    /**
82
     * @var string
83
     */
84
    protected $title;
85
86
    /**
87
     * @var string
88
     */
89
    protected $icon;
90
91
    /**
92
     * @var bool
93
     */
94
    protected $checkAccess = false;
95
96
    /**
97
     * @var array
98
     */
99
    protected $redirect = ['edit' => 'edit', 'create' => 'edit'];
100
101
    /**
102
     * @var RepositoryInterface
103
     */
104
    private $repository;
105
106
    /**
107
     * @var Model|null
108
     */
109 54
    protected $model_value = null;
110
111 54
    /**
112 54
     * ModelConfigurationManager constructor.
113
     *
114 54
     * @param \Illuminate\Contracts\Foundation\Application $app
115
     * @param                                              $class
116 54
     *
117 54
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
118 54
     * @throws \SleepingOwl\Admin\Exceptions\RepositoryException
119 54
     */
120 54
    public function __construct(\Illuminate\Contracts\Foundation\Application $app, $class)
121 54
    {
122
        $this->app = $app;
123
        $this->class = $class;
124
125
        $this->model = $app->make($class);
126 54
127
        $this->repository = $app->make(RepositoryInterface::class);
128 54
        $this->repository->setClass($class);
129
        if (! $this->alias) {
130
            $this->setDefaultAlias();
131
        }
132
    }
133
134 13
    /**
135
     * @return string
136 13
     */
137
    public function getClass()
138
    {
139
        return $this->class;
140
    }
141
142 6
    /**
143
     * @return Model
144 6
     */
145 6
    public function getModel()
146 6
    {
147 6
        return $this->model;
148
    }
149 6
150
    /**
151
     * @return Model|null
152
     */
153
    public function getModelValue()
154
    {
155 2
        return $this->model_value;
156
    }
157 2
158
    /**
159
     * @param Model $item
160
     */
161
    public function setModelValue($item)
162
    {
163
        $this->model_value = $item;
164
    }
165 1
166
    /**
167 1
     * @return string
168
     */
169 1
    public function getTitle()
170
    {
171
        if (is_null($this->title)) {
0 ignored issues
show
The condition is_null($this->title) is always false.
Loading history...
172
            $title = str_replace('_', ' ', $this->getDefaultClassTitle());
173
            $this->title = ucwords($title);
174
        }
175 12
176
        return $this->title;
177 12
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getIcon()
183 7
    {
184
        return $this->icon;
185 7
    }
186
187
    /**
188
     * @param string $icon
189
     *
190
     * @return $this
191 2
     */
192
    public function setIcon($icon)
193 2
    {
194
        $this->icon = $icon;
195
196
        return $this;
197
    }
198
199 2
    /**
200
     * @return string
201 2
     */
202
    public function getAlias()
203
    {
204
        return $this->alias;
205
    }
206
207 1
    /**
208
     * @return RepositoryInterface
209 1
     */
210
    public function getRepository()
211
    {
212
        return $this->repository;
213
    }
214
215 1
    /**
216
     * @return string|array|\Symfony\Component\Translation\TranslatorInterface
217 1
     */
218
    public function getCreateTitle()
219
    {
220
        return trans('sleeping_owl::lang.model.create', ['title' => $this->getTitle()]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('sleeping_o... => $this->getTitle())) also could return the type array which is incompatible with the return type mandated by SleepingOwl\Admin\Contra...rface::getCreateTitle() of Symfony\Component\Transl...nslatorInterface|string.
Loading history...
221
    }
222
223
    /**
224
     * @return string|array|\Symfony\Component\Translation\TranslatorInterface
225 1
     */
226
    public function getEditTitle()
227 1
    {
228
        return trans('sleeping_owl::lang.model.edit', ['title' => $this->getTitle()]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('sleeping_o... => $this->getTitle())) also could return the type array which is incompatible with the return type mandated by SleepingOwl\Admin\Contra...terface::getEditTitle() of Symfony\Component\Transl...nslatorInterface|string.
Loading history...
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function isDisplayable()
235 1
    {
236
        return $this->can('display', $this->getModel());
237 1
    }
238
239
    /**
240
     * @return bool
241
     */
242
    public function isCreatable()
243
    {
244
        return $this->can('create', $this->getModel());
245 3
    }
246
247 3
    /**
248
     * @param \Illuminate\Database\Eloquent\Model $model
249
     *
250
     * @return bool
251
     */
252
    public function isEditable(Model $model)
253
    {
254
        return $this->can('edit', $model);
255 1
    }
256
257 1
    /**
258
     * @param \Illuminate\Database\Eloquent\Model $model
259
     *
260
     * @return bool
261
     */
262
    public function isDeletable(Model $model)
263 6
    {
264
        return $this->can('delete', $model);
265 6
    }
266
267
    /**
268
     * @param Model $model
269
     *
270
     * @return bool
271
     */
272
    public function isDestroyable(Model $model)
273
    {
274
        return $this->isRestorableModel() && $this->can('destroy', $model);
275
    }
276
277
    /**
278
     * @param \Illuminate\Database\Eloquent\Model $model
279
     *
280
     * @return bool
281
     */
282 1
    public function isRestorable(Model $model)
283
    {
284 1
        return $this->isRestorableModel() && $this->can('restore', $model);
285
    }
286 1
287
    /**
288
     * @return bool
289
     */
290
    public function isRestorableModel()
291
    {
292 1
        return $this->getRepository()->isRestorable();
293
    }
294 1
295
    /**
296 1
     * @param int $id
297
     *
298
     * @return $this
299
     * @deprecated
300
     */
301
    public function fireFullEdit($id)
302
    {
303
        return $this->fireEdit($id);
304
    }
305 2
306
    /**
307 2
     * @return $this
308 2
     */
309
    public function enableAccessCheck()
310
    {
311 1
        $this->checkAccess = true;
312
313
        return $this;
314
    }
315
316
    /**
317
     * @return $this
318
     */
319 1
    public function disableAccessCheck()
320
    {
321 1
        $this->checkAccess = false;
322
323 1
        return $this;
324
    }
325
326
    /**
327
     * @param string $action
328
     * @param \Illuminate\Database\Eloquent\Model $model
329 1
     *
330
     * @return bool
331 1
     */
332
    public function can($action, Model $model)
333
    {
334
        if (! $this->checkAccess) {
335
            return true;
336
        }
337 1
338
        return \Gate::allows($action, [$this, $model]);
339 1
    }
340
341 1
    /**
342
     * @param string $controllerClass
343
     *
344
     * @return $this
345
     */
346
    public function setControllerClass($controllerClass)
347
    {
348
        $this->controllerClass = $controllerClass;
349 1
350
        return $this;
351 1
    }
352
353 1
    /**
354
     * @return null|string
355
     */
356
    public function getControllerClass()
357
    {
358
        return $this->controllerClass;
359
    }
360
361
    /**
362
     * @return null|string|bool
363
     */
364
    public function hasCustomControllerClass()
365
    {
366
        $controller = $this->getControllerClass();
367
368
        return ! is_null($controller) && class_exists($controller);
369
    }
370
371 1
    /**
372
     * @param array $parameters
373 1
     *
374
     * @return string
375 1
     */
376
    public function getDisplayUrl(array $parameters = [])
377
    {
378
        array_unshift($parameters, $this->getAlias());
379
380
        return route('admin.model', $parameters);
381 2
    }
382
383 2
    /**
384
     * @param array $parameters
385
     *
386
     * @return string
387
     */
388
    public function getCancelUrl(array $parameters = [])
389
    {
390
        return $this->getDisplayUrl($parameters);
391 1
    }
392
393 1
    /**
394
     * @param array $parameters
395
     *
396
     * @return string
397
     */
398
    public function getCreateUrl(array $parameters = [])
399
    {
400
        array_unshift($parameters, $this->getAlias());
401 2
402
        return route('admin.model.create', $parameters);
403 2
    }
404
405
    /**
406
     * @param array $parameters
407
     *
408
     * @return string
409
     */
410
    public function getStoreUrl(array $parameters = [])
411 1
    {
412
        array_unshift($parameters, $this->getAlias());
413 1
414
        return route('admin.model.store', $parameters);
415
    }
416
417
    /**
418
     * @param string|int $id
419
     * @param array $parameters
420
     *
421 1
     * @return string
422
     */
423 1
    public function getEditUrl($id, array $parameters = [])
424
    {
425
        if (! $id) {
426
            return '#';
427
        }
428
429
        array_unshift($parameters, $this->getAlias(), $id);
430
431 1
        return route('admin.model.edit', $parameters);
432
    }
433 1
434
    /**
435
     * @param string|int $id
436
     * @param array $parameters
437
     *
438
     * @return string
439 2
     */
440
    public function getUpdateUrl($id, array $parameters = [])
441 2
    {
442
        if (! $id) {
443
            return '#';
444
        }
445
446
        array_unshift($parameters, $this->getAlias(), $id);
447 2
448
        return route('admin.model.update', $parameters);
449 2
    }
450
451
    /**
452
     * @param string|int $id
453
     * @param array $parameters
454
     *
455 2
     * @return string
456
     */
457 2
    public function getDeleteUrl($id, array $parameters = [])
458
    {
459
        if (! $id) {
460
            return '#';
461
        }
462
463 2
        array_unshift($parameters, $this->getAlias(), $id);
464
465 2
        return route('admin.model.delete', $parameters);
466
    }
467
468
    /**
469
     * @param string|int $id
470
     * @param array $parameters
471 2
     *
472
     * @return string
473 2
     */
474
    public function getDestroyUrl($id, array $parameters = [])
475
    {
476
        if (! $id) {
477
            return '#';
478
        }
479 1
480
        array_unshift($parameters, $this->getAlias(), $id);
481 1
482
        return route('admin.model.destroy', $parameters);
483
    }
484
485
    /**
486
     * @param string|int $id
487
     * @param array $parameters
488
     *
489
     * @return string
490 1
     */
491
    public function getRestoreUrl($id, array $parameters = [])
492 1
    {
493
        if (! $id) {
494 1
            return '#';
495
        }
496 1
497
        array_unshift($parameters, $this->getAlias(), $id);
498
499
        return route('admin.model.restore', $parameters);
500
    }
501
502
    /**
503
     * @return string|array
504
     */
505
    public function getMessageOnCreate()
506
    {
507
        return trans('sleeping_owl::lang.message.created');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('sleeping_o...:lang.message.created') also could return the type array which is incompatible with the return type mandated by SleepingOwl\Admin\Contra...e::getMessageOnCreate() of string.
Loading history...
508
    }
509
510
    /**
511
     * @return string|array
512
     */
513
    public function getMessageOnUpdate()
514 1
    {
515
        return trans('sleeping_owl::lang.message.updated');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('sleeping_o...:lang.message.updated') also could return the type array which is incompatible with the return type mandated by SleepingOwl\Admin\Contra...e::getMessageOnUpdate() of string.
Loading history...
516 1
    }
517 1
518
    /**
519 1
     * @return string|array
520
     */
521
    public function getMessageOnDelete()
522
    {
523
        return trans('sleeping_owl::lang.message.deleted');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('sleeping_o...:lang.message.deleted') also could return the type array which is incompatible with the return type mandated by SleepingOwl\Admin\Contra...e::getMessageOnDelete() of string.
Loading history...
524
    }
525
526
    /**
527 1
     * @return string|array
528
     */
529
    public function getMessageOnRestore()
530
    {
531
        return trans('sleeping_owl::lang.message.restored');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('sleeping_o...lang.message.restored') also could return the type array which is incompatible with the return type mandated by SleepingOwl\Admin\Contra...::getMessageOnRestore() of string.
Loading history...
532
    }
533
534
    /**
535
     * @return string|array
536
     */
537
    public function getMessageOnDestroy()
538
    {
539
        return trans('sleeping_owl::lang.message.destroyed');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('sleeping_o...ang.message.destroyed') also could return the type array which is incompatible with the return type mandated by SleepingOwl\Admin\Contra...::getMessageOnDestroy() of string.
Loading history...
540
    }
541
542
    /**
543
     * @return Navigation
544
     */
545
    public function getNavigation()
546
    {
547
        return $this->app['sleeping_owl.navigation'];
548
    }
549
550
    /**
551
     * @param int $priority
552
     * @param string|\Closure|BadgeInterface $badge
553
     *
554
     * @return Page
555
     */
556
    public function addToNavigation($priority = 100, $badge = null)
557
    {
558
        $page = $this->makePage($priority, $badge);
559 3
560
        $this->getNavigation()->addPage($page);
561 3
562
        return $page;
563
    }
564
565 3
    /**
566 2
     * @param $page_id
567 2
     * @return \KodiComponents\Navigation\Contracts\PageInterface|null
568
     */
569
    public function getNavigationPage($page_id)
570
    {
571
        return $this->getNavigation()->getPages()->findById($page_id);
572 3
    }
573
574 3
    /**
575
     * @param int $priority
576 3
     * @param string|\Closure|BadgeInterface $badge
577
     *
578 3
     * @return Page
579
     */
580
    protected function makePage($priority = 100, $badge = null)
581
    {
582
        $page = new Page($this->getClass());
583
        $page->setPriority($priority);
584
585
        if ($badge) {
586
            if (! ($badge instanceof BadgeInterface)) {
587
                $badge = new Badge($badge);
588
            }
589 3
590
            $page->setBadge($badge);
591 3
        }
592 3
593 3
        return $page;
594 3
    }
595 2
596
    /**
597 2
     * @param array $redirect
598
     * @return $this
599
     */
600 1
    public function setRedirect(array $redirect)
601
    {
602
        $this->redirect = $redirect;
603
604
        return $this;
605
    }
606
607
    /**
608 2
     * @return \Illuminate\Support\Collection
609
     */
610 2
    public function getRedirect()
611 2
    {
612 2
        return collect($this->redirect);
613 2
    }
614
615 54
    /**
616
     * Fire the given event for the model.
617 54
     *
618 54
     * @param string $event
619
     * @param bool $halt
620
     * @param Model|null $model
621
     * @param array $payload
622
     *
623 54
     * @return mixed
624
     */
625 54
    public function fireEvent($event, $halt = true, Model $model = null, ...$payload)
626
    {
627
        if (! isset(self::$dispatcher)) {
628
            return true;
629
        }
630
631
        if (is_null($model)) {
632
            $model = $this->getModel();
633
        }
634
635
        // We will append the names of the class to the event to distinguish it from
636
        // other model events that are fired, allowing us to listen on each model
637
        // event set individually instead of catching event for all the models.
638
        $event = "sleeping_owl.section.{$event}: ".$this->getClass();
639
640
        // Laravel 5.8 and 5.4 support fire method
641
        if (version_compare('5.8.0', $this->app->version(), '<=') ||
642
            version_compare('5.5.0', $this->app->version(), '>')) {
643
            $fireMethod = 'dispatch';
644
        } else {
645
            $fireMethod = 'fire';
646
        }
647
648
        $method = $halt ? 'until' : $fireMethod;
649
650
        array_unshift($payload, $this, $model);
651
652
        return self::$dispatcher->$method($event, $payload);
653
    }
654
655
    /**
656
     * Handle dynamic method calls into the model.
657
     *
658
     * @param $method
659
     * @param $arguments
660
     *
661
     * @return mixed
662
     */
663
    public function __call($method, $arguments)
664
    {
665
        if (in_array($method, [
666
            'creating', 'created', 'updating', 'updated', 'saving', 'saved',
667
            'deleting', 'deleted', 'restoring', 'restored',
668
        ])) {
669
            array_unshift($arguments, $method);
670
671
            return call_user_func_array([$this, 'registerEvent'], $arguments);
672
        }
673
674
        throw new BadMethodCallException($method);
675
    }
676
677
    /**
678
     * @param     $event
679
     * @param     $callback
680
     * @param int $priority
681
     */
682
    protected function registerEvent($event, $callback, $priority = 0)
683
    {
684
        if (isset(self::$dispatcher)) {
685
            self::$dispatcher->listen("sleeping_owl.section.{$event}: ".$this->getClass(), $callback, $priority);
0 ignored issues
show
The call to Illuminate\Events\Dispatcher::listen() has too many arguments starting with $priority. ( Ignorable by Annotation )

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

685
            self::$dispatcher->/** @scrutinizer ignore-call */ 
686
                               listen("sleeping_owl.section.{$event}: ".$this->getClass(), $callback, $priority);

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...
686
        }
687
    }
688
689
    protected function setDefaultAlias()
690
    {
691
        $this->alias = $this->getDefaultClassTitle();
692
    }
693
694
    /**
695
     * @return string
696
     */
697
    protected function getDefaultClassTitle()
698
    {
699
        return Str::snake(Str::plural(class_basename($this->getClass())));
700
    }
701
}
702