Completed
Push — develop ( 7f277e...be549d )
by Abdelrahman
01:26
created

PagesController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Managerarea;
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\Foundation\Http\Requests\ImportFormRequest;
14
use Cortex\Pages\DataTables\Managerarea\PagesDataTable;
15
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
16
use Cortex\Pages\Http\Requests\Managerarea\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\Managerarea\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' => 'managerarea-pages-index-table',
37
        ])->render('cortex/foundation::managerarea.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
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...
47
     */
48
    public function logs(Page $page, LogsDataTable $logsDataTable)
49
    {
50
        return $logsDataTable->with([
51
            'resource' => $page,
52
            'tabs' => 'managerarea.pages.tabs',
53
            'id' => "managerarea-pages-{$page->getRouteKey()}-logs-table",
54
        ])->render('cortex/foundation::managerarea.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)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $importRecordsDataTable exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
66
    {
67
        return $importRecordsDataTable->with([
68
            'resource' => $page,
69
            'tabs' => 'managerarea.pages.tabs',
70
            'url' => route('managerarea.pages.stash'),
71
            'id' => "managerarea-pages-{$page->getRouteKey()}-import-table",
72
        ])->render('cortex/foundation::managerarea.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
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...
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();
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...
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" : '');
0 ignored issues
show
Bug introduced by
The method getMessageBag() does not exist on Exception. Did you maybe mean getMessage()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 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...
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
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...
127
     */
128
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
129
    {
130
        return $importLogsDatatable->with([
131
            'resource' => trans('cortex/pages::common.page'),
132
            'tabs' => 'managerarea.pages.tabs',
133
            'id' => 'managerarea-pages-import-logs-table',
134
        ])->render('cortex/foundation::managerarea.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::managerarea.pages.page', compact('page', 'tags'));
149
    }
150
151
    /**
152
     * Store new page.
153
     *
154
     * @param \Cortex\Pages\Http\Requests\Managerarea\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\Managerarea\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
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...
192
            return intend([
193
                'back' => true,
194
                'withInput' => $request->all(),
195
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
196
            ]);
197
        }
198
199
        // Save page
200
        $page->fill($data)->save();
201
202
        return intend([
203
            'url' => route('managerarea.pages.index'),
204
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/pages::common.page'), 'identifier' => $page->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 170 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...
205
        ]);
206
    }
207
208
    /**
209
     * Destroy given page.
210
     *
211
     * @param \Cortex\Pages\Models\Page $page
212
     *
213
     * @throws \Exception
214
     *
215
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
216
     */
217
    public function destroy(Page $page)
218
    {
219
        $page->delete();
220
221
        return intend([
222
            'url' => route('managerarea.pages.index'),
223
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/pages::common.page'), 'identifier' => $page->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 172 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...
224
        ]);
225
    }
226
}
227