Completed
Push — develop ( 1339d5...d43fde )
by Abdelrahman
10:17
created

PagesController::importLogs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

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 7
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Adminarea;
6
7
use Cortex\Foundation\DataTables\ImportLogsDataTable;
8
use Cortex\Foundation\Http\Requests\ImportFormRequest;
9
use Cortex\Foundation\Importers\DefaultImporter;
10
use Cortex\Pages\Models\Page;
11
use Illuminate\Foundation\Http\FormRequest;
12
use Cortex\Foundation\DataTables\LogsDataTable;
13
use Cortex\Pages\DataTables\Adminarea\PagesDataTable;
14
use Cortex\Pages\Http\Requests\Adminarea\PageFormRequest;
15
use Cortex\Foundation\Http\Controllers\AuthorizedController;
16
17
class PagesController extends AuthorizedController
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resource = Page::class;
23
24
    /**
25
     * List all pages.
26
     *
27
     * @param \Cortex\Pages\DataTables\Adminarea\PagesDataTable $pagesDataTable
28
     *
29
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
30
     */
31
    public function index(PagesDataTable $pagesDataTable)
32
    {
33
        return $pagesDataTable->with([
34
            'id' => 'adminarea-pages-index-table',
35
            'phrase' => trans('cortex/pages::common.pages'),
36
        ])->render('cortex/foundation::adminarea.pages.datatable');
37
    }
38
39
    /**
40
     * List page logs.
41
     *
42
     * @param \Cortex\Pages\Models\Page                   $page
43
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
44
     *
45
     * @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...
46
     */
47
    public function logs(Page $page, LogsDataTable $logsDataTable)
48
    {
49
        return $logsDataTable->with([
50
            'resource' => $page,
51
            'tabs' => 'adminarea.pages.tabs',
52
            'phrase' => trans('cortex/pages::common.pages'),
53
            'id' => "adminarea-pages-{$page->getKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
55
    }
56
57
    /**
58
     * Import pages.
59
     *
60
     * @return \Illuminate\View\View
61
     */
62
    public function import()
63
    {
64
        return view('cortex/foundation::adminarea.pages.import', [
65
            'id' => 'adminarea-pages-import',
66
            'tabs' => 'adminarea.pages.tabs',
67
            'url' => route('adminarea.pages.hoard'),
68
            'phrase' => trans('cortex/pages::common.pages'),
69
        ]);
70
    }
71
72
    /**
73
     * Hoard pages.
74
     *
75
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
76
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
77
     *
78
     * @return void
79
     */
80
    public function hoard(ImportFormRequest $request, DefaultImporter $importer)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        // Handle the import
83
        $importer->config['resource'] = $this->resource;
84
        $importer->handleImport();
85
    }
86
87
    /**
88
     * List page import logs.
89
     *
90
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
91
     *
92
     * @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...
93
     */
94
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
95
    {
96
        return $importLogsDatatable->with([
97
            'resource' => 'page',
98
            'tabs' => 'adminarea.pages.tabs',
99
            'id' => 'adminarea-pages-import-logs-table',
100
            'phrase' => trans('cortex/pages::common.pages'),
101
        ])->render('cortex/foundation::adminarea.pages.datatable-import-logs');
102
    }
103
104
    /**
105
     * Create new page.
106
     *
107
     * @param \Cortex\Pages\Models\Page $page
108
     *
109
     * @return \Illuminate\View\View
110
     */
111
    public function create(Page $page)
112
    {
113
        return $this->form($page);
114
    }
115
116
    /**
117
     * Edit given page.
118
     *
119
     * @param \Cortex\Pages\Models\Page $page
120
     *
121
     * @return \Illuminate\View\View
122
     */
123
    public function edit(Page $page)
124
    {
125
        return $this->form($page);
126
    }
127
128
    /**
129
     * Show page create/edit form.
130
     *
131
     * @param \Cortex\Pages\Models\Page $page
132
     *
133
     * @return \Illuminate\View\View
134
     */
135
    protected function form(Page $page)
136
    {
137
        return view('cortex/pages::adminarea.pages.page', compact('page'));
138
    }
139
140
    /**
141
     * Store new page.
142
     *
143
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
144
     * @param \Cortex\Pages\Models\Page                             $page
145
     *
146
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
147
     */
148
    public function store(PageFormRequest $request, Page $page)
149
    {
150
        return $this->process($request, $page);
151
    }
152
153
    /**
154
     * Update given page.
155
     *
156
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
157
     * @param \Cortex\Pages\Models\Page                             $page
158
     *
159
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
160
     */
161
    public function update(PageFormRequest $request, Page $page)
162
    {
163
        return $this->process($request, $page);
164
    }
165
166
    /**
167
     * Process stored/updated page.
168
     *
169
     * @param \Illuminate\Foundation\Http\FormRequest $request
170
     * @param \Cortex\Pages\Models\Page               $page
171
     *
172
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
173
     */
174
    protected function process(FormRequest $request, Page $page)
175
    {
176
        // Prepare required input fields
177
        $data = $request->validated();
178
179
        // Verify existing view
180
        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...
181
            return intend([
182
                'back' => true,
183
                'withInput' => $request->all(),
184
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
185
            ]);
186
        }
187
188
        // Save page
189
        $page->fill($data)->save();
190
191
        return intend([
192
            'url' => route('adminarea.pages.index'),
193
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'page', 'id' => $page->name])],
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...
194
        ]);
195
    }
196
197
    /**
198
     * Destroy given page.
199
     *
200
     * @param \Cortex\Pages\Models\Page $page
201
     *
202
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
203
     */
204
    public function destroy(Page $page)
205
    {
206
        $page->delete();
207
208
        return intend([
209
            'url' => route('adminarea.pages.index'),
210
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'page', 'id' => $page->name])],
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...
211
        ]);
212
    }
213
}
214