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.
Passed
Push — analysis-qMQ15l ( a3370e )
by butschster
10:08 queued 21s
created

AdminController::setParentBreadcrumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

117
            /** @scrutinizer ignore-type */ $this->admin->template()->view('dashboard'),
Loading history...
118
            trans('sleeping_owl::lang.dashboard')
119
        );
120
    }
121
122
    /**
123
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
124
     */
125
    public function getEnvEditor()
126
    {
127
        $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

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

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

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

405
        return $this->render($model, $edit, /** @scrutinizer ignore-type */ $model->getEditTitle());
Loading history...
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...
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.
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 */
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...
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
     * @return \Illuminate\Http\RedirectResponse
598
     */
599
    public function deletedAll(ModelConfigurationInterface $model, Request $request)
600
    {
601
        if (is_null($request->_id)) {
602
            return redirect()->back();
603
        }
604
605
        $items = $request->_id;
606
607
        foreach ($items as $id) {
608
            $item = $model->getRepository()->find($id);
609
610
            if (! $item) {
611
                return response()->Json(['error' => 'Haven`t row']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->Json(...ror' => 'Haven`t row')) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
612
            }
613
614
            if (isset($item->deleted_at) && $item->deleted_at) {
615
                $model->getRepository()->forceDelete($id);
616
            } else {
617
                $model->getRepository()->delete($id);
618
            }
619
        }
620
621
        $response = redirect()
622
        ->to($request
623
        ->input('_redirectBack', $model->getDisplayUrl()));
624
625
        return $response
626
        ->with('success_message', $model->getMessageOnDelete());
627
    }
628
629
    /**
630
     * @param ModelConfigurationInterface $model
631
     * @param Request $request
632
     * @param int $id
633
     *
634
     * @return \Illuminate\Http\RedirectResponse
635
     *
636
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
637
     */
638
    public function deleteDestroy(ModelConfigurationInterface $model, Request $request, $id)
639
    {
640
        if (! $model->isRestorableModel()) {
641
            abort(404);
642
        }
643
644
        $item = $model->getRepository()->findOnlyTrashed($id);
645
646
        if (is_null($item) || ! $model->isRestorable($item)) {
647
            abort(404);
648
        }
649
650
        $model->fireDestroy($id);
651
652
        if ($model->fireEvent('destroying', true, $item, $request) === false) {
653
            return redirect()->back();
654
        }
655
656
        $model->getRepository()->forceDelete($id);
657
658
        $model->fireEvent('destroyed', false, $item, $request);
659
660
        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...
661
            ->with('success_message', $model->getMessageOnDestroy());
662
    }
663
664
    /**
665
     * @param ModelConfigurationInterface|ModelConfiguration $model
666
     * @param Request $request
667
     * @param int $id
668
     *
669
     * @return \Illuminate\Http\RedirectResponse
670
     *
671
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
672
     */
673
    public function postRestore(ModelConfigurationInterface $model, Request $request, $id)
674
    {
675
        if (! $model->isRestorableModel()) {
676
            abort(404);
677
        }
678
679
        $item = $model->getRepository()->findOnlyTrashed($id);
680
681
        if (is_null($item) || ! $model->isRestorable($item)) {
682
            abort(404);
683
        }
684
685
        $model->fireRestore($id);
686
687
        if ($model->fireEvent('restoring', true, $item, $request) === false) {
688
            return redirect()->back();
689
        }
690
691
        $model->getRepository()->restore($id);
692
693
        $model->fireEvent('restored', false, $item, $request);
694
695
        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...
696
            ->with('success_message', $model->getMessageOnRestore());
697
    }
698
699
    /**
700
     * @param ModelConfigurationInterface $model
701
     * @param Renderable|RedirectResponse|string $content
702
     * @param string|null $title
703
     *
704
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Illuminate\Http\RedirectResponse
705
     */
706
    public function render(ModelConfigurationInterface $model, $content, $title = null)
707
    {
708
        if ($content instanceof RedirectResponse) {
709
            return $content;
710
        }
711
712
        if ($content instanceof Renderable) {
713
            $content = $content->render();
714
        }
715
716
        if (is_null($title)) {
717
            $title = $model->getTitle();
718
        }
719
720
        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...
721
            ->with('title', $title)
722
            ->with('content', $content)
723
            ->with('breadcrumbKey', $this->parentBreadcrumb);
724
    }
725
726
    /**
727
     * @param Renderable|string $content
728
     * @param string|null $title
729
     *
730
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
731
     */
732
    public function renderContent($content, $title = null)
733
    {
734
        if ($content instanceof Renderable) {
735
            $content = $content->render();
736
        }
737
738
        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...
739
            ->with('title', $title)
740
            ->with('content', $content)
741
            ->with('breadcrumbKey', $this->parentBreadcrumb);
742
    }
743
744
    /**
745
     * @param Request $request
746
     *
747
     * @return null|string
748
     */
749
    protected function getBackUrl(Request $request)
750
    {
751
        if (($backUrl = $request->input('_redirectBack')) == \URL::previous()) {
752
            $backUrl = null;
753
            $request->merge(['_redirectBack' => $backUrl]);
754
        }
755
756
        return $backUrl;
757
    }
758
759
    public function getWildcard()
760
    {
761
        abort(404);
762
    }
763
764
    /**
765
     * @param $title
766
     * @param $parent
767
     * @param $name
768
     * @param $url
769
     *
770
     * @throws \DaveJamesMiller\Breadcrumbs\Exception
771
     */
772
    protected function registerBreadcrumb($title, $parent, $name = 'render', $url = null)
773
    {
774
        $this->breadcrumbs->register($name, function (Generator $breadcrumbs) use ($title, $parent, $url) {
775
            $breadcrumbs->parent($parent);
776
            $breadcrumbs->push($title, $url);
777
        });
778
779
        $this->parentBreadcrumb = $name;
780
    }
781
782
    /**
783
     * @param ModelConfigurationInterface $model
784
     * @throws \DaveJamesMiller\Breadcrumbs\Exception
785
     */
786
    protected function registerBreadcrumbs(ModelConfigurationInterface $model)
787
    {
788
        $this->breadCrumbsData = array_merge($this->breadCrumbsData, $model->getBreadCrumbs());
0 ignored issues
show
Bug introduced by
$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

788
        $this->breadCrumbsData = array_merge($this->breadCrumbsData, /** @scrutinizer ignore-type */ $model->getBreadCrumbs());
Loading history...
789
790
        foreach ($this->breadCrumbsData as $breadcrumb) {
791
            if (! $this->breadcrumbs->exists($breadcrumb['id'])) {
792
                $this->breadcrumbs->register($breadcrumb['id'], function (Generator $breadcrumbs) use ($breadcrumb) {
793
                    $breadcrumbs->parent($breadcrumb['parent']);
794
                    $breadcrumbs->push($breadcrumb['title'], $breadcrumb['url']);
795
                });
796
            }
797
        }
798
799
        $this->parentBreadcrumb = data_get(array_last($this->breadCrumbsData), 'id', 'render');
800
    }
801
}
802