Completed
Push — develop ( 52a02d...d0b879 )
by Abdelrahman
06:13
created

PagesController::stash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Pages\Http\Controllers\Managerarea;
6
7
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
8
use Cortex\Pages\Models\Page;
9
use Exception;
10
use Illuminate\Foundation\Http\FormRequest;
11
use Cortex\Foundation\DataTables\LogsDataTable;
12
use Cortex\Foundation\Importers\DefaultImporter;
13
use Cortex\Foundation\DataTables\ImportLogsDataTable;
14
use Cortex\Foundation\Http\Requests\ImportFormRequest;
15
use Cortex\Pages\DataTables\Managerarea\PagesDataTable;
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
     * Create new page.
139
     *
140
     * @param \Cortex\Pages\Models\Page $page
141
     *
142
     * @return \Illuminate\View\View
143
     */
144
    public function create(Page $page)
145
    {
146
        return $this->form($page);
147
    }
148
149
    /**
150
     * Edit given page.
151
     *
152
     * @param \Cortex\Pages\Models\Page $page
153
     *
154
     * @return \Illuminate\View\View
155
     */
156
    public function edit(Page $page)
157
    {
158
        return $this->form($page);
159
    }
160
161
    /**
162
     * Show page create/edit form.
163
     *
164
     * @param \Cortex\Pages\Models\Page $page
165
     *
166
     * @return \Illuminate\View\View
167
     */
168
    protected function form(Page $page)
169
    {
170
        $tags = app('rinvex.tags.tag')->pluck('name', 'id');
171
172
        return view('cortex/pages::managerarea.pages.page', compact('page', 'tags'));
173
    }
174
175
    /**
176
     * Store new page.
177
     *
178
     * @param \Cortex\Pages\Http\Requests\Managerarea\PageFormRequest $request
179
     * @param \Cortex\Pages\Models\Page                               $page
180
     *
181
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
182
     */
183
    public function store(PageFormRequest $request, Page $page)
184
    {
185
        return $this->process($request, $page);
186
    }
187
188
    /**
189
     * Update given page.
190
     *
191
     * @param \Cortex\Pages\Http\Requests\Managerarea\PageFormRequest $request
192
     * @param \Cortex\Pages\Models\Page                               $page
193
     *
194
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
195
     */
196
    public function update(PageFormRequest $request, Page $page)
197
    {
198
        return $this->process($request, $page);
199
    }
200
201
    /**
202
     * Process stored/updated page.
203
     *
204
     * @param \Illuminate\Foundation\Http\FormRequest $request
205
     * @param \Cortex\Pages\Models\Page               $page
206
     *
207
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
208
     */
209
    protected function process(FormRequest $request, Page $page)
210
    {
211
        // Prepare required input fields
212
        $data = $request->validated();
213
214
        // Verify existing view
215
        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...
216
            return intend([
217
                'back' => true,
218
                'withInput' => $request->all(),
219
                'withErrors' => ['view' => trans('cortex/pages::messages.page.invalid_view')],
220
            ]);
221
        }
222
223
        // Save page
224
        $page->fill($data)->save();
225
226
        return intend([
227
            'url' => route('managerarea.pages.index'),
228
            '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...
229
        ]);
230
    }
231
232
    /**
233
     * Destroy given page.
234
     *
235
     * @param \Cortex\Pages\Models\Page $page
236
     *
237
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
238
     */
239
    public function destroy(Page $page)
240
    {
241
        $page->delete();
242
243
        return intend([
244
            'url' => route('managerarea.pages.index'),
245
            '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...
246
        ]);
247
    }
248
}
249