Completed
Push — master ( e955b9...2ea211 )
by Abdelrahman
04:33 queued 01:46
created

src/Http/Controllers/Adminarea/PagesController.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Cortex\Pages\Models\Page;
9
use Illuminate\Foundation\Http\FormRequest;
10
use Cortex\Foundation\DataTables\LogsDataTable;
11
use Cortex\Foundation\Importers\DefaultImporter;
12
use Cortex\Foundation\DataTables\ImportLogsDataTable;
13
use Cortex\Pages\DataTables\Adminarea\PagesDataTable;
14
use Cortex\Foundation\Http\Requests\ImportFormRequest;
15
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
16
use Cortex\Pages\Http\Requests\Adminarea\PageFormRequest;
17
use Cortex\Foundation\Http\Controllers\AuthorizedController;
18
19
class PagesController extends AuthorizedController
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $resource = Page::class;
25
26
    /**
27
     * List all pages.
28
     *
29
     * @param \Cortex\Pages\DataTables\Adminarea\PagesDataTable $pagesDataTable
30
     *
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(PagesDataTable $pagesDataTable)
34
    {
35
        return $pagesDataTable->with([
36
            'id' => 'adminarea-pages-index-table',
37
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
38
    }
39
40
    /**
41
     * List page logs.
42
     *
43
     * @param \Cortex\Pages\Models\Page                   $page
44
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
45
     *
46
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
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...
47
     */
48
    public function logs(Page $page, LogsDataTable $logsDataTable)
49
    {
50
        return $logsDataTable->with([
51
            'resource' => $page,
52
            'tabs' => 'adminarea.pages.tabs',
53
            'id' => "adminarea-pages-{$page->getRouteKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
55
    }
56
57
    /**
58
     * Import pages.
59
     *
60
     * @param \Cortex\Pages\Models\Page                            $page
61
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
62
     *
63
     * @return \Illuminate\View\View
64
     */
65
    public function import(Page $page, ImportRecordsDataTable $importRecordsDataTable)
66
    {
67
        return $importRecordsDataTable->with([
68
            'resource' => $page,
69
            'tabs' => 'adminarea.pages.tabs',
70
            'url' => route('adminarea.pages.stash'),
71
            'id' => "adminarea-pages-{$page->getRouteKey()}-import-table",
72
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
73
    }
74
75
    /**
76
     * Stash pages.
77
     *
78
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
79
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
80
     *
81
     * @return void
82
     */
83
    public function stash(ImportFormRequest $request, DefaultImporter $importer)
0 ignored issues
show
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...
84
    {
85
        // Handle the import
86
        $importer->config['resource'] = $this->resource;
87
        $importer->handleImport();
88
    }
89
90
    /**
91
     * Hoard pages.
92
     *
93
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
94
     *
95
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
96
     */
97
    public function hoard(ImportFormRequest $request)
98
    {
99
        foreach ((array) $request->get('selected_ids') as $recordId) {
100
            $record = app('cortex.foundation.import_record')->find($recordId);
101
102
            try {
103
                $fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.pages.page')->getFillable()))->toArray();
104
105
                tap(app('rinvex.pages.page')->firstOrNew($fillable), function ($instance) use ($record) {
106
                    $instance->save() && $record->delete();
107
                });
108
            } catch (Exception $exception) {
109
                $record->notes = $exception->getMessage().(method_exists($exception, 'getMessageBag') ? "\n".json_encode($exception->getMessageBag())."\n\n" : '');
110
                $record->status = 'fail';
111
                $record->save();
112
            }
113
        }
114
115
        return intend([
116
            'back' => true,
117
            'with' => ['success' => trans('cortex/foundation::messages.import_complete')],
118
        ]);
119
    }
120
121
    /**
122
     * List page import logs.
123
     *
124
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
125
     *
126
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
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...
127
     */
128
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
129
    {
130
        return $importLogsDatatable->with([
131
            'resource' => trans('cortex/pages::common.page'),
132
            'tabs' => 'adminarea.pages.tabs',
133
            'id' => 'adminarea-pages-import-logs-table',
134
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
135
    }
136
137
    /**
138
     * Show page create/edit form.
139
     *
140
     * @param \Cortex\Pages\Models\Page $page
141
     *
142
     * @return \Illuminate\View\View
143
     */
144
    protected function form(Page $page)
145
    {
146
        $tags = app('rinvex.tags.tag')->pluck('name', 'id');
147
148
        return view('cortex/pages::adminarea.pages.page', compact('page', 'tags'));
149
    }
150
151
    /**
152
     * Store new page.
153
     *
154
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
155
     * @param \Cortex\Pages\Models\Page                             $page
156
     *
157
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
158
     */
159
    public function store(PageFormRequest $request, Page $page)
160
    {
161
        return $this->process($request, $page);
162
    }
163
164
    /**
165
     * Update given page.
166
     *
167
     * @param \Cortex\Pages\Http\Requests\Adminarea\PageFormRequest $request
168
     * @param \Cortex\Pages\Models\Page                             $page
169
     *
170
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
171
     */
172
    public function update(PageFormRequest $request, Page $page)
173
    {
174
        return $this->process($request, $page);
175
    }
176
177
    /**
178
     * Process stored/updated page.
179
     *
180
     * @param \Illuminate\Foundation\Http\FormRequest $request
181
     * @param \Cortex\Pages\Models\Page               $page
182
     *
183
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
184
     */
185
    protected function process(FormRequest $request, Page $page)
186
    {
187
        // Prepare required input fields
188
        $data = $request->validated();
189
190
        // Verify existing view
191
        if (! view()->exists($data['view'])) {
0 ignored issues
show
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...
192
            return intend([
193
                'back' => true,
194
                'withInput' => $request->all(),
195
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
196
            ]);
197
        }
198
199
        ! $request->hasFile('profile_picture')
200
        || $page->addMediaFromRequest('profile_picture')
201
                  ->sanitizingFileName(function ($fileName) {
202
                      return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
203
                  })
204
                  ->toMediaCollection('profile_picture', config('cortex.auth.media.disk'));
205
206
        ! $request->hasFile('cover_photo')
207
        || $page->addMediaFromRequest('cover_photo')
208
                  ->sanitizingFileName(function ($fileName) {
209
                      return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
210
                  })
211
                  ->toMediaCollection('cover_photo', config('cortex.auth.media.disk'));
212
213
        // Save page
214
        $page->fill($data)->save();
215
216
        return intend([
217
            'url' => route('adminarea.pages.index'),
218
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/pages::common.page'), 'identifier' => $page->name])],
219
        ]);
220
    }
221
222
    /**
223
     * Destroy given page.
224
     *
225
     * @param \Cortex\Pages\Models\Page $page
226
     *
227
     * @throws \Exception
228
     *
229
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
230
     */
231
    public function destroy(Page $page)
232
    {
233
        $page->delete();
234
235
        return intend([
236
            'url' => route('adminarea.pages.index'),
237
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/pages::common.page'), 'identifier' => $page->name])],
238
        ]);
239
    }
240
}
241