Completed
Push — develop ( fbd9da...b88f7c )
by Abdelrahman
14:33 queued 11:39
created

PagesController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Adminarea;
6
7
use Rinvex\Pages\Models\Page;
8
use Illuminate\Foundation\Http\FormRequest;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Pages\DataTables\Adminarea\PagesDataTable;
11
use Cortex\Pages\Http\Requests\Adminarea\PageFormRequest;
12
use Cortex\Foundation\Http\Controllers\AuthorizedController;
13
14
class PagesController extends AuthorizedController
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $resource = 'page';
20
21
    /**
22
     * List all pages.
23
     *
24
     * @param \Cortex\Pages\DataTables\Adminarea\PagesDataTable $pagesDataTable
25
     *
26
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
27
     */
28
    public function index(PagesDataTable $pagesDataTable)
29
    {
30
        return $pagesDataTable->with([
31
            'id' => 'adminarea-pages-index-table',
32
            'phrase' => trans('cortex/pages::common.pages'),
33
        ])->render('cortex/foundation::adminarea.pages.datatable');
34
    }
35
36
    /**
37
     * List page logs.
38
     *
39
     * @param \Cortex\Pages\Models\Page                   $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
40
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
41
     *
42
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
43
     */
44
    public function logs(Page $page, LogsDataTable $logsDataTable)
45
    {
46
        return $logsDataTable->with([
47
            'resource' => $page,
48
            'tabs' => 'adminarea.pages.tabs',
49
            'phrase' => trans('cortex/pages::common.pages'),
50
            'id' => "adminarea-pages-{$page->getKey()}-logs-table",
51
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
52
    }
53
54
    /**
55
     * Create new page.
56
     *
57
     * @param \Cortex\Pages\Models\Page $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
58
     *
59
     * @return \Illuminate\View\View
60
     */
61
    public function create(Page $page)
62
    {
63
        return $this->form($page);
64
    }
65
66
    /**
67
     * Edit given page.
68
     *
69
     * @param \Cortex\Pages\Models\Page $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
70
     *
71
     * @return \Illuminate\View\View
72
     */
73
    public function edit(Page $page)
74
    {
75
        return $this->form($page);
76
    }
77
78
    /**
79
     * Show page create/edit form.
80
     *
81
     * @param \Cortex\Pages\Models\Page $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
82
     *
83
     * @return \Illuminate\View\View
84
     */
85
    protected function form(Page $page)
86
    {
87
        return view('cortex/pages::adminarea.pages.page', compact('page'));
88
    }
89
90
    /**
91
     * Store new page.
92
     *
93
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
94
     * @param \Cortex\Pages\Models\Page                             $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
95
     *
96
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
97
     */
98
    public function store(PageFormRequest $request, Page $page)
99
    {
100
        return $this->process($request, $page);
101
    }
102
103
    /**
104
     * Update given page.
105
     *
106
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
107
     * @param \Cortex\Pages\Models\Page                             $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
108
     *
109
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
110
     */
111
    public function update(PageFormRequest $request, Page $page)
112
    {
113
        return $this->process($request, $page);
114
    }
115
116
    /**
117
     * Process stored/updated page.
118
     *
119
     * @param \Illuminate\Foundation\Http\FormRequest $request
120
     * @param \Cortex\Pages\Models\Page               $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
121
     *
122
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
123
     */
124
    protected function process(FormRequest $request, Page $page)
125
    {
126
        // Prepare required input fields
127
        $data = $request->validated();
128
129
        // Verify existing view
130
        if (! view()->exists($data['view'])) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
131
            return intend([
132
                'back' => true,
133
                'withInput' => $request->all(),
134
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
135
            ]);
136
        }
137
138
        // Save page
139
        $page->fill($data)->save();
140
141
        return intend([
142
            'url' => route('adminarea.pages.index'),
143
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'page', 'id' => $page->slug])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
144
        ]);
145
    }
146
147
    /**
148
     * Destroy given page.
149
     *
150
     * @param \Cortex\Pages\Models\Page $page
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $page a bit more specific; maybe use Page.
Loading history...
151
     *
152
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
153
     */
154
    public function destroy(Page $page)
155
    {
156
        $page->delete();
157
158
        return intend([
159
            'url' => route('adminarea.pages.index'),
160
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'page', 'id' => $page->slug])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
161
        ]);
162
    }
163
}
164