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
Pull Request — new (#872)
by
unknown
09:00
created

AdminController::validatePolicy()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 3
nc 6
nop 2
dl 0
loc 5
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Routing\Controller;
7
use SleepingOwl\Admin\Form\FormElements;
8
use DaveJamesMiller\Breadcrumbs\Generator;
9
use SleepingOwl\Admin\Form\Columns\Column;
10
use SleepingOwl\Admin\Display\DisplayTable;
11
use Illuminate\Contracts\Support\Renderable;
12
use SleepingOwl\Admin\Display\DisplayTabbed;
13
use Illuminate\Validation\ValidationException;
14
use SleepingOwl\Admin\Contracts\AdminInterface;
15
use SleepingOwl\Admin\Model\ModelConfiguration;
16
use Illuminate\Contracts\Foundation\Application;
17
use SleepingOwl\Admin\Contracts\Form\FormInterface;
18
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
19
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface;
20
21
class AdminController extends Controller
22
{
23
    /**
24
     * @var \DaveJamesMiller\Breadcrumbs\Manager
25
     */
26
    protected $breadcrumbs;
27
28
    /**
29
     * @var
30
     */
31
    protected $breadCrumbsData;
32
33
    /**
34
     * @var AdminInterface
35
     */
36
    protected $admin;
37
38
    /**
39
     * @var
40
     */
41
    private $parentBreadcrumb = 'home';
42
43
    /**
44
     * @var Application
45
     */
46
    public $app;
47
48
    /**
49
     * @var
50
     */
51
    protected $envPolicy;
52
53
    /**
54
     * AdminController constructor.
55
     *
56
     * @param Request $request
57
     * @param AdminInterface $admin
58
     * @param Application $application
59
     */
60
    public function __construct(Request $request, AdminInterface $admin, Application $application)
61
    {
62
        $this->app = $application;
63
        $this->admin = $admin;
64
        $this->breadcrumbs = $admin->template()->breadcrumbs();
65
66
        if ($this->envPolicy = config('sleeping_owl.env_editor_policy')) {
67
            $this->envPolicy = new $this->envPolicy;
68
        }
69
70
        $admin->navigation()->setCurrentUrl($request->getUri());
0 ignored issues
show
Bug introduced by
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

70
        $admin->navigation()->/** @scrutinizer ignore-call */ setCurrentUrl($request->getUri());
Loading history...
71
72
        if (! $this->breadcrumbs->exists('home')) {
73
            $this->breadcrumbs->register('home', function (Generator $breadcrumbs) {
74
                $breadcrumbs->push(trans('sleeping_owl::lang.dashboard'), route('admin.dashboard'));
75
            });
76
        }
77
78
        $this->breadCrumbsData = [];
79
80
        if ($currentPage = $admin->navigation()->getCurrentPage()) {
81
            foreach ($currentPage->getPathArray() as $page) {
82
                $this->breadCrumbsData[] = [
83
                    'id'     => $page['id'],
84
                    'title'  => $page['title'],
85
                    'url'    => $page['url'],
86
                    'parent' => $this->parentBreadcrumb,
87
                ];
88
89
                $this->parentBreadcrumb = $page['id'];
90
            }
91
        }
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getParentBreadcrumb()
98
    {
99
        return $this->parentBreadcrumb;
100
    }
101
102
    /**
103
     * @param string $parentBreadcrumb
104
     */
105
    public function setParentBreadcrumb($parentBreadcrumb)
106
    {
107
        $this->parentBreadcrumb = $parentBreadcrumb;
108
    }
109
110
    /**
111
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
112
     */
113
    public function getDashboard()
114
    {
115
        return $this->renderContent(
116
            $this->admin->template()->view('dashboard'),
0 ignored issues
show
Bug introduced by
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 string|Illuminate\Contracts\Support\Renderable, 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

116
            /** @scrutinizer ignore-type */ $this->admin->template()->view('dashboard'),
Loading history...
117
            trans('sleeping_owl::lang.dashboard')
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.dashboard') can also be of type 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

117
            /** @scrutinizer ignore-type */ trans('sleeping_owl::lang.dashboard')
Loading history...
118
        );
119
    }
120
121
    /**
122
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
123
     */
124
    public function getEnvEditor()
125
    {
126
        $envFile = app()->environmentFilePath();
0 ignored issues
show
introduced by
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

126
        $envFile = app()->/** @scrutinizer ignore-call */ environmentFilePath();
Loading history...
127
        $envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW));
128
129
        /**
130
         * Use filter masks.
131
         * @param $key
132
         * @return bool
133
         */
134
        $envContent = $envContent->filter(function ($value, $key) {
135
            return ! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key);
136
        });
137
138
        $envContent = $envContent->filter(function ($value, $key) {
139
            return $this->validatePolicy('display', $key);
140
        });
141
142
        $envContent = $envContent->map(function ($value, $key) {
143
            return (object)[
144
                'value'     => $value,
145
                'editable'  => $this->validatePolicy('edit', $key),
146
                'deletable' => $this->validatePolicy('delete', $key),
147
            ];
148
        });
149
150
        return $this->renderContent(
151
            $this->admin->template()->view('env_editor', ['data' => $envContent]),
0 ignored issues
show
Bug introduced by
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 string|Illuminate\Contracts\Support\Renderable, 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

151
            /** @scrutinizer ignore-type */ $this->admin->template()->view('env_editor', ['data' => $envContent]),
Loading history...
152
            trans('sleeping_owl::lang.env_editor.title')
0 ignored issues
show
Bug introduced by
It seems like trans('sleeping_owl::lang.env_editor.title') can also be of type 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

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

309
        return $this->render($model, $create, /** @scrutinizer ignore-type */ $model->getCreateTitle());
Loading history...
310
    }
311
312
    /**
313
     * @param ModelConfigurationInterface $model
314
     * @param Request $request
315
     *
316
     * @return \Illuminate\Http\RedirectResponse
317
     */
318
    public function postStore(ModelConfigurationInterface $model, Request $request)
319
    {
320
        if (! $model->isCreatable()) {
321
            abort(404);
322
        }
323
324
        $createForm = $model->fireCreate();
325
        $nextAction = $request->input('next_action');
326
327
        $backUrl = $this->getBackUrl($request);
328
329
        if ($createForm instanceof FormInterface) {
330
            try {
331
                $createForm->validateForm($request, $model);
332
333
                if ($createForm->saveForm($request, $model) === false) {
0 ignored issues
show
Bug introduced by
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...
introduced by
The condition $createForm->saveForm($request, $model) === false is always true.
Loading history...
334
                    return redirect()->back()->with([
335
                        '_redirectBack'       => $backUrl,
336
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
337
                    ]);
338
                }
339
            } catch (ValidationException $exception) {
340
                return redirect()->back()
341
                    ->withErrors($exception->validator)
342
                    ->withInput()
343
                    ->with([
344
                        '_redirectBack'       => $backUrl,
345
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
346
                    ]);
347
            }
348
        }
349
350
        if ($nextAction == 'save_and_continue') {
351
            $newModel = $createForm->getModel();
352
            $primaryKey = $newModel->getKeyName();
353
354
            $redirectUrl = $model->getEditUrl($newModel->{$primaryKey});
355
            $redirectPolicy = $model->getRedirect();
356
357
            /* Make redirect when use in model config && Fix editable redirect */
358
            if ($redirectPolicy->get('create') == 'display' || ! $model->isEditable($newModel)) {
359
                $redirectUrl = $model->getDisplayUrl();
360
            }
361
362
            $response = redirect()->to(
363
                $redirectUrl
364
            )->with([
365
                '_redirectBack'       => $backUrl,
366
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
367
            ]);
368
        } elseif ($nextAction == 'save_and_create') {
369
            $response = redirect()->to($model->getCreateUrl($request->except([
370
                '_redirectBack',
371
                '_token',
372
                'url',
373
                'next_action',
374
            ])))->with([
375
                '_redirectBack'       => $backUrl,
376
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
377
            ]);
378
        } else {
379
            $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl()));
380
        }
381
382
        return $response->with('success_message', $model->getMessageOnCreate());
383
    }
384
385
    /**
386
     * @param ModelConfigurationInterface $model
387
     * @param $id
388
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
389
     * @throws \DaveJamesMiller\Breadcrumbs\Exception
390
     */
391
    public function getEdit(ModelConfigurationInterface $model, $id)
392
    {
393
        $item = $model->getRepository()->find($id);
394
395
        if (is_null($item) || ! $model->isEditable($item)) {
396
            abort(404);
397
        }
398
399
        $this->registerBreadcrumb($model->getEditTitle($item), $this->parentBreadcrumb);
400
401
        $edit = $model->fireEdit($id);
402
403
        $this->registerBreadcrumbs($model);
404
405
        return $this->render($model, $edit, $model->getEditTitle($item));
0 ignored issues
show
Bug introduced by
It seems like $model->getEditTitle($item) 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

405
        return $this->render($model, $edit, /** @scrutinizer ignore-type */ $model->getEditTitle($item));
Loading history...
406
    }
407
408
    /**
409
     * @param ModelConfigurationInterface $model
410
     * @param Request $request
411
     * @param int $id
412
     *
413
     * @return \Illuminate\Http\RedirectResponse
414
     *
415
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
416
     */
417
    public function postUpdate(ModelConfigurationInterface $model, Request $request, $id)
418
    {
419
        /** @var FormInterface $editForm */
420
        $editForm = $model->fireEdit($id);
421
        $item = $editForm->getModel();
422
423
        if (is_null($item) || ! $model->isEditable($item)) {
424
            abort(404);
425
        }
426
427
        $nextAction = $request->input('next_action');
428
429
        $backUrl = $this->getBackUrl($request);
430
431
        if ($editForm instanceof FormInterface) {
0 ignored issues
show
introduced by
$editForm is always a sub-type of SleepingOwl\Admin\Contracts\Form\FormInterface. If $editForm can have other possible types, add them to src/Http/Controllers/AdminController.php:419.
Loading history...
432
            try {
433
                $editForm->validateForm($request, $model);
434
435
                if ($editForm->saveForm($request, $model) === false) {
0 ignored issues
show
Bug introduced by
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...
introduced by
The condition $editForm->saveForm($request, $model) === false is always true.
Loading history...
436
                    return redirect()->back()->with([
437
                        '_redirectBack'       => $backUrl,
438
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
439
                    ]);
440
                }
441
            } catch (ValidationException $exception) {
442
                return redirect()->back()
443
                    ->withErrors($exception->validator)
444
                    ->withInput()
445
                    ->with([
446
                        '_redirectBack'       => $backUrl,
447
                        'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
448
                    ]);
449
            }
450
        }
451
452
        $redirectPolicy = $model->getRedirect();
453
454
        if ($nextAction == 'save_and_continue') {
455
            $response = redirect()->back()->with([
456
                '_redirectBack'       => $backUrl,
457
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
458
            ]);
459
460
            if ($redirectPolicy->get('edit') == 'display') {
461
                $response = redirect()->to(
462
                    $model->getDisplayUrl()
463
                )->with([
464
                    '_redirectBack'       => $backUrl,
465
                    'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
466
                ]);
467
            }
468
        } elseif ($nextAction == 'save_and_create') {
469
            $response = redirect()->to($model->getCreateUrl($request->except([
470
                '_redirectBack',
471
                '_token',
472
                'url',
473
                'next_action',
474
            ])))->with([
475
                '_redirectBack'       => $backUrl,
476
                'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null,
477
            ]);
478
        } else {
479
            $response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl()));
480
        }
481
482
        return $response->with('success_message', $model->getMessageOnUpdate());
483
    }
484
485
    /**
486
     * @param ModelConfigurationInterface $model
487
     * @param Request $request
488
     *
489
     * @return bool
490
     *
491
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
492
     */
493
    public function inlineEdit(ModelConfigurationInterface $model, Request $request)
494
    {
495
        $field = $request->input('name');
496
        $id = $request->input('pk');
497
        $display = $model->fireDisplay();
498
        $column = null;
499
500
        /* @var ColumnEditableInterface|null $column */
501
        if (is_callable([$display, 'getColumns'])) {
502
            $column = $display->getColumns()->all()->filter(function ($column) use ($field) {
0 ignored issues
show
Bug introduced by
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

502
            $column = $display->/** @scrutinizer ignore-call */ getColumns()->all()->filter(function ($column) use ($field) {
Loading history...
503
                return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
0 ignored issues
show
Bug introduced by
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

503
                return ($column instanceof ColumnEditableInterface) && $field == $column->/** @scrutinizer ignore-call */ getName();
Loading history...
504
            })->first();
505
        } else {
506
            if ($display instanceof DisplayTabbed) {
507
                foreach ($display->getTabs() as $tab) {
508
                    $content = $tab->getContent();
509
510
                    if ($content instanceof DisplayTable) {
511
                        $column = $content->getColumns()->all()->filter(function ($column) use ($field) {
512
                            return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
513
                        })->first();
514
                    }
515
                    if ($content instanceof FormElements) {
516
                        foreach ($content->getElements() as $element) {
517
518
                            //Return data-table if inside FormElements
519
                            if ($element instanceof DisplayTable) {
520
                                $column = $element->getColumns()->all()->filter(function ($column) use ($field) {
521
                                    return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
522
                                })->first();
523
                            }
524
525
                            //Try to find inline Editable in columns
526
                            if ($element instanceof Column) {
527
                                foreach ($element->getElements() as $columnElement) {
528
                                    if ($columnElement instanceof DisplayTable) {
529
                                        $column = $columnElement->getColumns()->all()->filter(function ($column) use (
530
                                            $field
531
                                        ) {
532
                                            return ($column instanceof ColumnEditableInterface) && $field == $column->getName();
533
                                        })->first();
534
                                    }
535
                                }
536
                            }
537
                        }
538
                    }
539
                }
540
            }
541
        }
542
543
        if (is_null($column)) {
544
            abort(404);
545
        }
546
547
        $repository = $model->getRepository();
548
        $item = $repository->find($id);
549
550
        if (is_null($item) || ! $model->isEditable($item)) {
551
            abort(404);
552
        }
553
554
        $column->setModel($item);
555
556
        if ($model->fireEvent('updating', true, $item, $request) === false) {
0 ignored issues
show
Bug introduced by
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

556
        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...
557
            return;
558
        }
559
560
        $column->save($request, $model);
561
562
        $model->fireEvent('updated', false, $item, $request);
563
    }
564
565
    /**
566
     * @param ModelConfigurationInterface $model
567
     * @param Request $request
568
     * @param int $id
569
     * @return \Illuminate\Http\RedirectResponse
570
     */
571
    public function deleteDelete(ModelConfigurationInterface $model, Request $request, $id)
572
    {
573
        $item = $model->getRepository()->find($id);
574
575
        if (is_null($item) || ! $model->isDeletable($item)) {
576
            abort(404);
577
        }
578
579
        $model->fireDelete($id);
580
581
        if ($model->fireEvent('deleting', true, $item, $request) === false) {
582
            return redirect()->back();
583
        }
584
585
        $model->getRepository()->delete($id);
586
587
        $model->fireEvent('deleted', false, $item, $request);
588
589
        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...
590
            ->with('success_message', $model->getMessageOnDelete());
591
    }
592
593
    /**
594
     * @param ModelConfigurationInterface $model
595
     * @param Request $request
596
     * @param int $id
597
     *
598
     * @return \Illuminate\Http\RedirectResponse
599
     *
600
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
601
     */
602
    public function deleteDestroy(ModelConfigurationInterface $model, Request $request, $id)
603
    {
604
        if (! $model->isRestorableModel()) {
605
            abort(404);
606
        }
607
608
        $item = $model->getRepository()->findOnlyTrashed($id);
609
610
        if (is_null($item) || ! $model->isRestorable($item)) {
611
            abort(404);
612
        }
613
614
        $model->fireDestroy($id);
615
616
        if ($model->fireEvent('destroying', true, $item, $request) === false) {
617
            return redirect()->back();
618
        }
619
620
        $model->getRepository()->forceDelete($id);
621
622
        $model->fireEvent('destroyed', false, $item, $request);
623
624
        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...
625
            ->with('success_message', $model->getMessageOnDestroy());
626
    }
627
628
    /**
629
     * @param ModelConfigurationInterface|ModelConfiguration $model
630
     * @param Request $request
631
     * @param int $id
632
     *
633
     * @return \Illuminate\Http\RedirectResponse
634
     *
635
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
636
     */
637
    public function postRestore(ModelConfigurationInterface $model, Request $request, $id)
638
    {
639
        if (! $model->isRestorableModel()) {
640
            abort(404);
641
        }
642
643
        $item = $model->getRepository()->findOnlyTrashed($id);
644
645
        if (is_null($item) || ! $model->isRestorable($item)) {
646
            abort(404);
647
        }
648
649
        $model->fireRestore($id);
650
651
        if ($model->fireEvent('restoring', true, $item, $request) === false) {
652
            return redirect()->back();
653
        }
654
655
        $model->getRepository()->restore($id);
656
657
        $model->fireEvent('restored', false, $item, $request);
658
659
        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...
660
            ->with('success_message', $model->getMessageOnRestore());
661
    }
662
663
    /**
664
     * @param ModelConfigurationInterface $model
665
     * @param Renderable|string $content
666
     * @param string|null $title
667
     *
668
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
669
     */
670
    public function render(ModelConfigurationInterface $model, $content, $title = null)
671
    {
672
        if ($content instanceof Renderable) {
673
            $content = $content->render();
674
        }
675
676
        if (is_null($title)) {
677
            $title = $model->getTitle();
678
        }
679
680
        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\View\View|Ill...\Contracts\View\Factory.
Loading history...
681
            ->with('title', $title)
0 ignored issues
show
Bug introduced by
The method with() does not exist on Illuminate\Contracts\View\Factory. ( Ignorable by Annotation )

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

681
            ->/** @scrutinizer ignore-call */ with('title', $title)

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...
682
            ->with('content', $content)
683
            ->with('breadcrumbKey', $this->parentBreadcrumb);
684
    }
685
686
    /**
687
     * @param Renderable|string $content
688
     * @param string|null $title
689
     *
690
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
691
     */
692
    public function renderContent($content, $title = null)
693
    {
694
        if ($content instanceof Renderable) {
695
            $content = $content->render();
696
        }
697
698
        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\View\View|Ill...\Contracts\View\Factory.
Loading history...
699
            ->with('title', $title)
700
            ->with('content', $content)
701
            ->with('breadcrumbKey', $this->parentBreadcrumb);
702
    }
703
704
    /**
705
     * @param Request $request
706
     *
707
     * @return null|string
708
     */
709
    protected function getBackUrl(Request $request)
710
    {
711
        if (($backUrl = $request->input('_redirectBack')) == \URL::previous()) {
712
            $backUrl = null;
713
            $request->merge(['_redirectBack' => $backUrl]);
714
        }
715
716
        return $backUrl;
717
    }
718
719
    public function getWildcard()
720
    {
721
        abort(404);
722
    }
723
724
    /**
725
     * @param $title
726
     * @param $parent
727
     * @throws \DaveJamesMiller\Breadcrumbs\Exception
728
     */
729
    protected function registerBreadcrumb($title, $parent)
730
    {
731
        $this->breadcrumbs->register('render', function (Generator $breadcrumbs) use ($title, $parent) {
732
            $breadcrumbs->parent($parent);
733
            $breadcrumbs->push($title);
734
        });
735
736
        $this->parentBreadcrumb = 'render';
737
    }
738
739
    /**
740
     * @param ModelConfigurationInterface $model
741
     * @throws \DaveJamesMiller\Breadcrumbs\Exception
742
     */
743
    protected function registerBreadcrumbs(ModelConfigurationInterface $model)
744
    {
745
        $this->breadCrumbsData = $this->breadCrumbsData + (array) $model->getBreadCrumbs();
746
747
        foreach ($this->breadCrumbsData as $breadcrumb) {
748
            if (! $this->breadcrumbs->exists($breadcrumb['id'])) {
749
                $this->breadcrumbs->register($breadcrumb['id'], function (Generator $breadcrumbs) use ($breadcrumb) {
750
                    $breadcrumbs->parent($breadcrumb['parent']);
751
                    $breadcrumbs->push($breadcrumb['title'], $breadcrumb['url']);
752
                });
753
            }
754
        }
755
    }
756
}
757