Completed
Push — develop ( 088573...be0805 )
by Abdelrahman
01:32
created

PagesController::process()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Adminarea;
6
7
use Cortex\Pages\Models\Page;
8
use Illuminate\Foundation\Http\FormRequest;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Foundation\Importers\DefaultImporter;
11
use Cortex\Foundation\DataTables\ImportLogsDataTable;
12
use Cortex\Pages\DataTables\Adminarea\PagesDataTable;
13
use Cortex\Foundation\Http\Requests\ImportFormRequest;
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
        $tags = app('rinvex.tags.tag')->pluck('title', 'id');
138
139
        return view('cortex/pages::adminarea.pages.page', compact('page', 'tags'));
140
    }
141
142
    /**
143
     * Store new page.
144
     *
145
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
146
     * @param \Cortex\Pages\Models\Page                             $page
147
     *
148
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
149
     */
150
    public function store(PageFormRequest $request, Page $page)
151
    {
152
        return $this->process($request, $page);
153
    }
154
155
    /**
156
     * Update given page.
157
     *
158
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
159
     * @param \Cortex\Pages\Models\Page                             $page
160
     *
161
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
162
     */
163
    public function update(PageFormRequest $request, Page $page)
164
    {
165
        return $this->process($request, $page);
166
    }
167
168
    /**
169
     * Process stored/updated page.
170
     *
171
     * @param \Illuminate\Foundation\Http\FormRequest $request
172
     * @param \Cortex\Pages\Models\Page               $page
173
     *
174
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
175
     */
176
    protected function process(FormRequest $request, Page $page)
177
    {
178
        // Prepare required input fields
179
        $data = $request->validated();
180
181
        // Verify existing view
182
        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...
183
            return intend([
184
                'back' => true,
185
                'withInput' => $request->all(),
186
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
187
            ]);
188
        }
189
190
        ! $request->hasFile('profile_picture')
191
        || $page->addMediaFromRequest('profile_picture')
192
                  ->sanitizingFileName(function ($fileName) {
193
                      return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
194
                  })
195
                  ->toMediaCollection('profile_picture', config('cortex.auth.media.disk'));
196
197
        ! $request->hasFile('cover_photo')
198
        || $page->addMediaFromRequest('cover_photo')
199
                  ->sanitizingFileName(function ($fileName) {
200
                      return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
201
                  })
202
                  ->toMediaCollection('cover_photo', config('cortex.auth.media.disk'));
203
204
        // Save page
205
        $page->fill($data)->save();
206
207
        return intend([
208
            'url' => route('adminarea.pages.index'),
209
            '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...
210
        ]);
211
    }
212
213
    /**
214
     * Destroy given page.
215
     *
216
     * @param \Cortex\Pages\Models\Page $page
217
     *
218
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
219
     */
220
    public function destroy(Page $page)
221
    {
222
        $page->delete();
223
224
        return intend([
225
            'url' => route('adminarea.pages.index'),
226
            '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...
227
        ]);
228
    }
229
}
230