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/Http/Controllers/AdminController.php (29 issues)

1
<?php
2
3
namespace SleepingOwl\Admin\Http\Controllers;
4
5
use DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Routing\Controller;
11
use Illuminate\Support\Arr;
12
use Illuminate\Validation\ValidationException;
13
use SleepingOwl\Admin\Contracts\AdminInterface;
14
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface;
15
use SleepingOwl\Admin\Contracts\Form\FormInterface;
16
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
17
use SleepingOwl\Admin\Display\DisplayTabbed;
18
use SleepingOwl\Admin\Display\DisplayTable;
19
use SleepingOwl\Admin\Form\Columns\Column;
20
use SleepingOwl\Admin\Form\FormElements;
21
use SleepingOwl\Admin\Model\ModelConfiguration;
22
23
class AdminController extends Controller
24
{
25
    /**
26
     * @var \DaveJamesMiller\Breadcrumbs\BreadcrumbsManager
27
     */
28
    protected $breadcrumbs;
29
30
    /**
31
     * @var
32
     */
33
    protected $breadCrumbsData;
34
35
    /**
36
     * @var AdminInterface
37
     */
38
    protected $admin;
39
40
    /**
41
     * @var
42
     */
43
    private $parentBreadcrumb = 'home';
44
45
    /**
46
     * @var Application
47
     */
48
    public $app;
49
50
    /**
51
     * @var
52
     */
53
    protected $envPolicy;
54
55
    /**
56
     * AdminController constructor.
57
     *
58
     * @param Request        $request
59
     * @param AdminInterface $admin
60
     * @param Application    $application
61
     *
62
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException
63
     */
64
    public function __construct(Request $request, AdminInterface $admin, Application $application)
65
    {
66
        $this->app = $application;
67
        $this->admin = $admin;
68
        $this->breadcrumbs = $admin->template()->breadcrumbs();
69
70
        if ($this->envPolicy = config('sleeping_owl.env_editor_policy')) {
71
            $this->envPolicy = new $this->envPolicy;
72
        }
73
74
        $admin->navigation()->setCurrentUrl($request->getUri());
0 ignored issues
show
The method setCurrentUrl() does not exist on SleepingOwl\Admin\Contra...ion\NavigationInterface. It seems like you code against a sub-type of said class. However, the method does not exist in SleepingOwl\Admin\Contra...avigation\PageInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

74
        $admin->navigation()->/** @scrutinizer ignore-call */ setCurrentUrl($request->getUri());
Loading history...
75
76
        if (! $this->breadcrumbs->exists('home')) {
77
            $this->breadcrumbs->register('home', function (BreadcrumbsGenerator $breadcrumbs) {
78
                $breadcrumbs->push(trans('sleeping_owl::lang.dashboard'), route('admin.dashboard'));
0 ignored issues
show
It seems like trans('sleeping_owl::lang.dashboard') can also be of type array and array; however, parameter $title of DaveJamesMiller\Breadcru...crumbsGenerator::push() does only seem to accept string, 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

78
                $breadcrumbs->push(/** @scrutinizer ignore-type */ trans('sleeping_owl::lang.dashboard'), route('admin.dashboard'));
Loading history...
79
            });
80
        }
81
82
        $this->breadCrumbsData = [];
83
84
        if ($currentPage = $admin->navigation()->getCurrentPage()) {
85
            foreach ($currentPage->getPathArray() as $page) {
86
                $this->breadCrumbsData[] = [
87
                    'id' => $page['id'],
88
                    'title' => $page['title'],
89
                    'url' => $page['url'],
90
                    'parent' => $this->parentBreadcrumb,
91
                ];
92
93
                $this->parentBreadcrumb = $page['id'];
94
            }
95
        }
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getParentBreadcrumb()
102
    {
103
        return $this->parentBreadcrumb;
104
    }
105
106
    /**
107
     * @param string $parentBreadcrumb
108
     */
109
    public function setParentBreadcrumb($parentBreadcrumb)
110
    {
111
        $this->parentBreadcrumb = $parentBreadcrumb;
112
    }
113
114
    /**
115
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
116
     */
117
    public function getDashboard()
118
    {
119
        return $this->renderContent(
120
            $this->admin->template()->view('dashboard'),
0 ignored issues
show
It seems like $this->admin->template()->view('dashboard') can also be of type Illuminate\Contracts\View\Factory; however, parameter $content of SleepingOwl\Admin\Http\C...roller::renderContent() does only seem to accept Illuminate\Contracts\Support\Renderable|string, 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

120
            /** @scrutinizer ignore-type */ $this->admin->template()->view('dashboard'),
Loading history...
121
            trans('sleeping_owl::lang.dashboard')
0 ignored issues
show
It seems like trans('sleeping_owl::lang.dashboard') can also be of type array and array; however, parameter $title of SleepingOwl\Admin\Http\C...roller::renderContent() does only seem to accept null|string, 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

121
            /** @scrutinizer ignore-type */ trans('sleeping_owl::lang.dashboard')
Loading history...
122
        );
123
    }
124
125
    /**
126
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
127
     */
128
    public function getEnvEditor()
129
    {
130
        $envFile = app()->environmentFilePath();
0 ignored issues
show
The method environmentFilePath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

130
        $envFile = app()->/** @scrutinizer ignore-call */ environmentFilePath();
Loading history...
131
        $envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW));
132
133
        /**
134
         * Use filter masks.
135
         * @param $key
136
         * @return bool
137
         */
138
        $envContent = $envContent->filter(function ($value, $key) {
139
            return ! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key);
140
        });
141
142
        $envContent = $envContent->filter(function ($value, $key) {
143
            return $this->validatePolicy('display', $key);
144
        });
145
146
        $envContent = $envContent->map(function ($value, $key) {
147
            return (object) [
148
                'value' => $value,
149
                'editable' => $this->validatePolicy('edit', $key),
150
                'deletable' => $this->validatePolicy('delete', $key),
151
            ];
152
        });
153
154
        return $this->renderContent(
155
            $this->admin->template()->view('env_editor', ['data' => $envContent]),
0 ignored issues
show
It seems like $this->admin->template()...'data' => $envContent)) can also be of type Illuminate\Contracts\View\Factory; however, parameter $content of SleepingOwl\Admin\Http\C...roller::renderContent() does only seem to accept Illuminate\Contracts\Support\Renderable|string, 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

155
            /** @scrutinizer ignore-type */ $this->admin->template()->view('env_editor', ['data' => $envContent]),
Loading history...
156
            trans('sleeping_owl::lang.env_editor.title')
0 ignored issues
show
It seems like trans('sleeping_owl::lang.env_editor.title') can also be of type array and array; however, parameter $title of SleepingOwl\Admin\Http\C...roller::renderContent() does only seem to accept null|string, 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

156
            /** @scrutinizer ignore-type */ trans('sleeping_owl::lang.env_editor.title')
Loading history...
157
        );
158
    }
159
160
    /**
161
     * @param $permission
162
     * @param $key
163
     * @return bool
164
     */
165
    protected function validatePolicy($permission, $key)
166
    {
167
        return ($this->envPolicy && ((method_exists($this->envPolicy, $permission)
168
                    && $this->envPolicy->$permission(\Auth::user(), $key) !== false)))
169
            || ! method_exists($this->envPolicy, $permission) ||
170
            ! $this->envPolicy
171
            ||
172
            $this->validateBeforePolicy($key);
173
    }
174
175
    /**
176
     * @param $key
177
     * @return bool
178
     */
179
    protected function validateBeforePolicy($key)
180
    {
181
        return ($this->envPolicy && (method_exists($this->envPolicy, 'before'))
182
                && $this->envPolicy->before(\Auth::user(), $key) == true)
183
            || ! method_exists($this->envPolicy, 'before') || ! $this->envPolicy;
184
    }
185
186
    /**
187
     * @param Request $request
188
     * @return \Illuminate\Http\RedirectResponse
189
     */
190
    public function postEnvEditor(Request $request)
191
    {
192
        $envFile = app()->environmentFilePath();
193
        $envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW));
194
195
        $requestContent = collect($request->input('variables'));
196
        $removeContent = collect();
197
198
        foreach ($envContent as $key => $value) {
199
            if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key)) {
200
                if ($requestContent->has($key)) {
201
                    if ($this->validatePolicy('edit', $key)) {
202
                        $envContent[$key] = $requestContent[$key]['value'];
203
                    }
204
                    $requestContent->forget($key);
205
                } else {
206
                    $envContent->forget($key);
207
                    $removeContent->put($key, null);
208
                }
209
            }
210
        }
211
212
        foreach ($requestContent as $key => $value) {
213
            if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key)
214
                && $this->validatePolicy('create', $key)) {
215
                $this->writeEnvData($key, $value['value'], 1);
216
            }
217
            $requestContent->forget($key);
218
        }
219
220
        foreach ($removeContent as $key => $value) {
221
            if ($this->validatePolicy('delete', $key)) {
222
                $this->writeEnvData($key);
223
            }
224
        }
225
226
        foreach ($envContent as $key => $value) {
227
            $this->writeEnvData($key, $value);
228
        }
229
230
        return redirect()->back()->with('success_message', 'Env Updated');
231
    }
232
233
    /**
234
     * @param $key
235
     * @return bool
236
     */
237
    public function filterKey($key)
238
    {
239
        foreach (config('sleeping_owl.env_editor_excluded_keys') as $val) {
240
            if (strpos($val, '*') !== false) {
241
                $val = str_replace('*', '', $val);
242
                if (strpos($key, $val) !== false) {
243
                    return true;
244
                }
245
            }
246
        }
247
248
        return false;
249
    }
250
251
    /**
252
     * @param $key
253
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
254
     * @param bool $new
255
     * @return bool
256
     */
257
    public function writeEnvData($key, $data = null, $new = null)
258
    {
259
        $envFile = app()->environmentFilePath();
260
        $str = file_get_contents($envFile);
261
262
        if (is_null($data)) {
0 ignored issues
show
The condition is_null($data) is always true.
Loading history...
263
            $str = preg_replace("/$key=.*/m", '', $str);
264
            file_put_contents($envFile, $str);
265
266
            return false;
267
        }
268
269
        if (is_null($new)) {
270
            $str = preg_replace("/$key=.*/m", "$key=$data", $str);
271
            file_put_contents($envFile, $str);
272
273
            return false;
274
        }
275
276
        $str = $str."\r\n$key=$data";
277
        file_put_contents($envFile, $str);
278
279
        return true;
280
    }
281
282
    /**
283
     * @param ModelConfigurationInterface $model
284
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
285
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException
286
     */
287
    public function getDisplay(ModelConfigurationInterface $model)
288
    {
289
        if (! $model->isDisplayable()) {
290
            abort(404);
291
        }
292
293
        $display = $model->fireDisplay();
294
295
        $this->registerBreadcrumbs($model);
296
297
        return $this->render($model, $display);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->render($model, $display) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Contracts\Vie...ry|Illuminate\View\View.
Loading history...
298
    }
299
300
    /**
301
     * @param ModelConfigurationInterface $model
302
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
303
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException
304
     */
305
    public function getCreate(ModelConfigurationInterface $model)
306
    {
307
        if (! $model->isCreatable()) {
308
            abort(404);
309
        }
310
311
        $create = $model->fireCreate();
312
313
        $this->registerBreadcrumbs($model);
314
        $this->registerBreadcrumb($model->getCreateTitle(), $this->parentBreadcrumb);
315
316
        return $this->render($model, $create, $model->getCreateTitle());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->render($mo...odel->getCreateTitle()) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Contracts\Vie...ry|Illuminate\View\View.
Loading history...
It seems like $model->getCreateTitle() can also be of type Symfony\Component\Translation\TranslatorInterface; however, parameter $title of SleepingOwl\Admin\Http\C...minController::render() does only seem to accept null|string, 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

316
        return $this->render($model, $create, /** @scrutinizer ignore-type */ $model->getCreateTitle());
Loading history...
317
    }
318
319
    /**
320
     * @param ModelConfigurationInterface $model
321
     * @param Request $request
322
     *
323
     * @return \Illuminate\Http\RedirectResponse
324
     */
325
    public function postStore(ModelConfigurationInterface $model, Request $request)
326
    {
327
        if (! $model->isCreatable()) {
328
            abort(404);
329
        }
330
331
        $createForm = $model->fireCreate();
332
        $nextAction = $request->input('next_action');
333
334
        $backUrl = $this->getBackUrl($request);
335
336
        if ($createForm instanceof FormInterface) {
337
            try {
338
                $createForm->validateForm($request, $model);
339
340
                if ($createForm->saveForm($request, $model) === false) {
0 ignored issues
show
Are you sure the usage of $createForm->saveForm($request, $model) targeting SleepingOwl\Admin\Contra...rmInterface::saveForm() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
The condition $createForm->saveForm($request, $model) === false is always true.
Loading history...
341
                    return redirect()->back()->with([
342
                        '_redirectBack' => $backUrl,
343
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
344
                    ]);
345
                }
346
            } catch (ValidationException $exception) {
347
                return redirect()->back()
348
                    ->withErrors($exception->validator)
349
                    ->withInput()
350
                    ->with([
351
                        '_redirectBack' => $backUrl,
352
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
353
                    ]);
354
            }
355
        }
356
357
        if ($nextAction == 'save_and_continue') {
358
            $newModel = $createForm->getModel();
359
            $primaryKey = $newModel->getKeyName();
360
361
            $redirectUrl = $model->getEditUrl($newModel->{$primaryKey});
362
            $redirectPolicy = $model->getRedirect();
363
364
            /* Make redirect when use in model config && Fix editable redirect */
365
            if ($redirectPolicy->get('create') == 'display' || ! $model->isEditable($newModel)) {
366
                $redirectUrl = $model->getDisplayUrl();
367
            }
368
369
            $response = redirect()->to(
370
                $redirectUrl
371
            )->with([
372
                '_redirectBack' => $backUrl,
373
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
374
            ]);
375
        } elseif ($nextAction == 'save_and_create') {
376
            $response = redirect()->to($model->getCreateUrl($request->except([
377
                '_redirectBack',
378
                '_token',
379
                'url',
380
                'next_action',
381
            ])))->with([
382
                '_redirectBack' => $backUrl,
383
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
384
            ]);
385
        } else {
386
            $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl()));
387
        }
388
389
        return $response->with('success_message', $model->getMessageOnCreate());
390
    }
391
392
    /**
393
     * @param ModelConfigurationInterface $model
394
     * @param $id
395
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
396
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException
397
     */
398
    public function getEdit(ModelConfigurationInterface $model, $id)
399
    {
400
        $item = $model->getRepository()->find($id);
401
402
        if (is_null($item) || ! $model->isEditable($item)) {
403
            abort(404);
404
        }
405
406
        if (method_exists($model, 'setModelValue')) {
407
            $model->setModelValue($item);
408
        }
409
410
        $edit = $model->fireEdit($id);
411
412
        $this->registerBreadcrumbs($model);
413
        $this->registerBreadcrumb($model->getEditTitle(), $this->parentBreadcrumb);
414
415
        return $this->render($model, $edit, $model->getEditTitle());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->render($mo...$model->getEditTitle()) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Contracts\Vie...ry|Illuminate\View\View.
Loading history...
It seems like $model->getEditTitle() can also be of type Symfony\Component\Translation\TranslatorInterface; however, parameter $title of SleepingOwl\Admin\Http\C...minController::render() does only seem to accept null|string, 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

415
        return $this->render($model, $edit, /** @scrutinizer ignore-type */ $model->getEditTitle());
Loading history...
416
    }
417
418
    /**
419
     * @param ModelConfigurationInterface $model
420
     * @param Request $request
421
     * @param int $id
422
     *
423
     * @return \Illuminate\Http\RedirectResponse
424
     *
425
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
426
     */
427
    public function postUpdate(ModelConfigurationInterface $model, Request $request, $id)
428
    {
429
        /** @var FormInterface $editForm */
430
        $editForm = $model->fireEdit($id);
431
        $item = $editForm->getModel();
432
433
        if (is_null($item) || ! $model->isEditable($item)) {
434
            abort(404);
435
        }
436
437
        $nextAction = $request->input('next_action');
438
439
        $backUrl = $this->getBackUrl($request);
440
441
        if ($editForm instanceof FormInterface) {
0 ignored issues
show
$editForm is always a sub-type of SleepingOwl\Admin\Contracts\Form\FormInterface.
Loading history...
442
            try {
443
                $editForm->validateForm($request, $model);
444
445
                if ($editForm->saveForm($request, $model) === false) {
0 ignored issues
show
The condition $editForm->saveForm($request, $model) === false is always true.
Loading history...
Are you sure the usage of $editForm->saveForm($request, $model) targeting SleepingOwl\Admin\Contra...rmInterface::saveForm() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
446
                    return redirect()->back()->with([
447
                        '_redirectBack' => $backUrl,
448
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
449
                    ]);
450
                }
451
            } catch (ValidationException $exception) {
452
                return redirect()->back()
453
                    ->withErrors($exception->validator)
454
                    ->withInput()
455
                    ->with([
456
                        '_redirectBack' => $backUrl,
457
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
458
                    ]);
459
            }
460
        }
461
462
        $redirectPolicy = $model->getRedirect();
463
464
        if ($nextAction == 'save_and_continue') {
465
            $response = redirect()->back()->with([
466
                '_redirectBack' => $backUrl,
467
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
468
            ]);
469
470
            if ($redirectPolicy->get('edit') == 'display') {
471
                $response = redirect()->to(
472
                    $model->getDisplayUrl()
473
                )->with([
474
                    '_redirectBack' => $backUrl,
475
                    'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
476
                ]);
477
            }
478
        } elseif ($nextAction == 'save_and_create') {
479
            $response = redirect()->to($model->getCreateUrl($request->except([
480
                '_redirectBack',
481
                '_token',
482
                'url',
483
                'next_action',
484
            ])))->with([
485
                '_redirectBack' => $backUrl,
486
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
487
            ]);
488
        } else {
489
            $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl()));
490
        }
491
492
        return $response->with('success_message', $model->getMessageOnUpdate());
493
    }
494
495
    /**
496
     * @param ModelConfigurationInterface $model
497
     * @param Request $request
498
     *
499
     * @return bool
500
     *
501
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
502
     */
503
    public function inlineEdit(ModelConfigurationInterface $model, Request $request)
504
    {
505
        $field = $request->input('name');
506
        $id = $request->input('pk');
507
        $display = $model->fireDisplay();
508
        $column = null;
509
510
        /* @var ColumnEditableInterface|null $column */
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
511
        if (is_callable([$display, 'getColumns'])) {
512
            $column = $display->getColumns()->all()->filter(function ($column) use ($field) {
0 ignored issues
show
The method getColumns() does not exist on SleepingOwl\Admin\Contra...isplay\DisplayInterface. It seems like you code against a sub-type of SleepingOwl\Admin\Contra...isplay\DisplayInterface such as SleepingOwl\Admin\Display\Display. ( Ignorable by Annotation )

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

512
            $column = $display->/** @scrutinizer ignore-call */ getColumns()->all()->filter(function ($column) use ($field) {
Loading history...
513
                return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
0 ignored issues
show
The method getName() does not exist on SleepingOwl\Admin\Contra...ColumnEditableInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contra...ColumnEditableInterface. ( Ignorable by Annotation )

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

513
                return ($column instanceof ColumnEditableInterface) && $field == $column->/** @scrutinizer ignore-call */ getName();
Loading history...
514
            })->first();
515
        } else {
516
            if ($display instanceof DisplayTabbed) {
517
                foreach ($display->getTabs() as $tab) {
518
                    $content = $tab->getContent();
519
520
                    if ($content instanceof DisplayTable) {
521
                        $column = $content->getColumns()->all()->filter(function ($column) use ($field) {
522
                            return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
523
                        })->first();
524
                    }
525
                    if ($content instanceof FormElements) {
526
                        foreach ($content->getElements() as $element) {
527
528
                            //Return data-table if inside FormElements
529
                            if ($element instanceof DisplayTable) {
530
                                $column = $element->getColumns()->all()->filter(function ($column) use ($field) {
531
                                    return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
532
                                })->first();
533
                            }
534
535
                            //Try to find inline Editable in columns
536
                            if ($element instanceof Column) {
537
                                foreach ($element->getElements() as $columnElement) {
538
                                    if ($columnElement instanceof DisplayTable) {
539
                                        $column = $columnElement->getColumns()->all()->filter(function ($column) use (
540
                                            $field
541
                                        ) {
542
                                            return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
543
                                        })->first();
544
                                    }
545
                                }
546
                            }
547
                        }
548
                    }
549
                }
550
            }
551
        }
552
553
        if (is_null($column)) {
554
            abort(404);
555
        }
556
557
        $repository = $model->getRepository();
558
        $item = $repository->find($id);
559
560
        if (is_null($item) || ! $model->isEditable($item)) {
561
            abort(404);
562
        }
563
564
        $column->setModel($item);
565
566
        if ($model->fireEvent('updating', true, $item, $request) === false) {
0 ignored issues
show
The method fireEvent() does not exist on SleepingOwl\Admin\Contra...lConfigurationInterface. Did you maybe mean fireEdit()? ( Ignorable by Annotation )

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

566
        if ($model->/** @scrutinizer ignore-call */ fireEvent('updating', true, $item, $request) === false) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
567
            return;
568
        }
569
570
        $column->save($request, $model);
571
572
        $model->fireEvent('updated', false, $item, $request);
573
    }
574
575
    /**
576
     * @param ModelConfigurationInterface $model
577
     * @param Request $request
578
     * @param int $id
579
     * @return \Illuminate\Http\RedirectResponse
580
     */
581
    public function deleteDelete(ModelConfigurationInterface $model, Request $request, $id)
582
    {
583
        $item = $model->getRepository()->find($id);
584
585
        if (is_null($item) || ! $model->isDeletable($item)) {
586
            abort(404);
587
        }
588
589
        $model->fireDelete($id);
590
591
        if ($model->fireEvent('deleting', true, $item, $request) === false) {
592
            return redirect()->back();
593
        }
594
595
        $model->getRepository()->delete($id);
596
597
        $model->fireEvent('deleted', false, $item, $request);
598
599
        return redirect($request->input('_redirectBack', back()->getTargetUrl()))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($request...->getMessageOnDelete()) also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
600
            ->with('success_message', $model->getMessageOnDelete());
601
    }
602
603
    /**
604
     * @param ModelConfigurationInterface $model
605
     * @param Request $request
606
     * @param int $id
607
     *
608
     * @return \Illuminate\Http\RedirectResponse
609
     *
610
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
611
     */
612
    public function deleteDestroy(ModelConfigurationInterface $model, Request $request, $id)
613
    {
614
        if (! $model->isRestorableModel()) {
615
            abort(404);
616
        }
617
618
        $item = $model->getRepository()->findOnlyTrashed($id);
619
620
        if (is_null($item) || ! $model->isRestorable($item)) {
621
            abort(404);
622
        }
623
624
        $model->fireDestroy($id);
625
626
        if ($model->fireEvent('destroying', true, $item, $request) === false) {
627
            return redirect()->back();
628
        }
629
630
        $model->getRepository()->forceDelete($id);
631
632
        $model->fireEvent('destroyed', false, $item, $request);
633
634
        return redirect($request->input('_redirectBack', back()->getTargetUrl()))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($request...>getMessageOnDestroy()) also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
635
            ->with('success_message', $model->getMessageOnDestroy());
636
    }
637
638
    /**
639
     * @param ModelConfigurationInterface|ModelConfiguration $model
640
     * @param Request $request
641
     * @param int $id
642
     *
643
     * @return \Illuminate\Http\RedirectResponse
644
     *
645
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
646
     */
647
    public function postRestore(ModelConfigurationInterface $model, Request $request, $id)
648
    {
649
        if (! $model->isRestorableModel()) {
650
            abort(404);
651
        }
652
653
        $item = $model->getRepository()->findOnlyTrashed($id);
654
655
        if (is_null($item) || ! $model->isRestorable($item)) {
656
            abort(404);
657
        }
658
659
        $model->fireRestore($id);
660
661
        if ($model->fireEvent('restoring', true, $item, $request) === false) {
662
            return redirect()->back();
663
        }
664
665
        $model->getRepository()->restore($id);
666
667
        $model->fireEvent('restored', false, $item, $request);
668
669
        return redirect($request->input('_redirectBack', back()->getTargetUrl()))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($request...>getMessageOnRestore()) also could return the type Illuminate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
670
            ->with('success_message', $model->getMessageOnRestore());
671
    }
672
673
    /**
674
     * @param ModelConfigurationInterface $model
675
     * @param Renderable|RedirectResponse|string $content
676
     * @param string|null $title
677
     *
678
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Illuminate\Http\RedirectResponse
679
     */
680
    public function render(ModelConfigurationInterface $model, $content, $title = null)
681
    {
682
        if ($content instanceof RedirectResponse) {
683
            return $content;
684
        }
685
686
        if ($content instanceof Renderable) {
687
            $content = $content->render();
688
        }
689
690
        if (is_null($title)) {
691
            $title = $model->getTitle();
692
        }
693
694
        return $this->admin->template()->view('_layout.inner')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->admin->tem...this->parentBreadcrumb) also could return the type boolean which is incompatible with the documented return type Illuminate\Contracts\Vie...se|Illuminate\View\View.
Loading history...
695
            ->with('title', $title)
696
            ->with('content', $content)
697
            ->with('breadcrumbKey', $this->parentBreadcrumb);
698
    }
699
700
    /**
701
     * @param Renderable|string $content
702
     * @param string|null $title
703
     *
704
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
705
     */
706
    public function renderContent($content, $title = null)
707
    {
708
        if ($content instanceof Renderable) {
709
            $content = $content->render();
710
        }
711
712
        return $this->admin->template()->view('_layout.inner')
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->admin->tem...this->parentBreadcrumb) also could return the type boolean which is incompatible with the documented return type Illuminate\Contracts\Vie...ry|Illuminate\View\View.
Loading history...
713
            ->with('title', $title)
714
            ->with('content', $content)
715
            ->with('breadcrumbKey', $this->parentBreadcrumb);
716
    }
717
718
    /**
719
     * @param Request $request
720
     *
721
     * @return null|string
722
     */
723
    protected function getBackUrl(Request $request)
724
    {
725
        if (($backUrl = $request->input('_redirectBack')) == \URL::previous()) {
726
            $backUrl = null;
727
            $request->merge(['_redirectBack' => $backUrl]);
728
        }
729
730
        return $backUrl;
731
    }
732
733
    public function getWildcard()
734
    {
735
        abort(404);
736
    }
737
738
    /**
739
     * @param $title
740
     * @param $parent
741
     * @param $name
742
     * @param $url
743
     *
744
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException
745
     */
746
    protected function registerBreadcrumb($title, $parent, $name = 'render', $url = null)
747
    {
748
        $this->breadcrumbs->register($name, function (BreadcrumbsGenerator $breadcrumbs) use ($title, $parent, $url) {
749
            $breadcrumbs->parent($parent);
750
            $breadcrumbs->push($title, $url);
751
        });
752
753
        $this->parentBreadcrumb = $name;
754
    }
755
756
    /**
757
     * @param ModelConfigurationInterface $model
758
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException
759
     */
760
    protected function registerBreadcrumbs(ModelConfigurationInterface $model)
761
    {
762
        $this->breadCrumbsData = array_merge($this->breadCrumbsData, $model->getBreadCrumbs());
0 ignored issues
show
$model->getBreadCrumbs() of type Illuminate\Support\Collection is incompatible with the type array|null expected by parameter $array2 of array_merge(). ( Ignorable by Annotation )

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

762
        $this->breadCrumbsData = array_merge($this->breadCrumbsData, /** @scrutinizer ignore-type */ $model->getBreadCrumbs());
Loading history...
763
764
        foreach ($this->breadCrumbsData as $breadcrumb) {
765
            if (! $this->breadcrumbs->exists($breadcrumb['id'])) {
766
                $this->breadcrumbs->register($breadcrumb['id'], function (BreadcrumbsGenerator $breadcrumbs) use ($breadcrumb) {
767
                    $breadcrumbs->parent($breadcrumb['parent']);
768
                    $breadcrumbs->push($breadcrumb['title'], $breadcrumb['url']);
769
                });
770
            }
771
        }
772
773
        $this->parentBreadcrumb = data_get(Arr::last($this->breadCrumbsData), 'id', 'render');
774
    }
775
}
776