ContentController   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 286
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 139
c 1
b 0
f 0
dl 0
loc 286
rs 9.92
wmc 31

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resourceCreate() 0 3 1
A repeaterIndex() 0 5 1
A resourceDelete() 0 12 2
A repeaterEntriesCreate() 0 5 1
A repeaterEntriesIndex() 0 6 1
A repeaterJson() 0 11 1
A repeaterEntriesDelete() 0 6 1
A resourceSave() 0 22 3
A repeaterEntriesSave() 0 10 2
B repeaterImport() 0 55 6
A resourceIndex() 0 5 1
A repeaterCreate() 0 5 1
A repeaterDelete() 0 13 2
A repeaterEntriesEdit() 0 6 1
A repeaterEdit() 0 6 1
A repeaterSave() 0 40 4
A resourceEdit() 0 5 1
1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Chuckbe\Chuckcms\Models\Content;
6
use Chuckbe\Chuckcms\Models\Repeater;
7
use Chuckbe\Chuckcms\Models\Resource;
8
use Chuckbe\Chuckcms\Models\Template;
9
use Chuckbe\Chuckcms\Models\User;
10
use ChuckSite;
0 ignored issues
show
Bug introduced by
The type ChuckSite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
12
use Illuminate\Foundation\Bus\DispatchesJobs;
13
use Illuminate\Foundation\Validation\ValidatesRequests;
14
use Illuminate\Http\Request;
15
use Illuminate\Routing\Controller as BaseController;
16
use Illuminate\Support\Facades\View;
17
18
class ContentController extends BaseController
19
{
20
    use AuthorizesRequests;
21
    use DispatchesJobs;
22
    use ValidatesRequests;
23
24
    private $content;
25
    private $resource;
26
    private $repeater;
27
    private $template;
28
    private $user;
29
30
    /**
31
     * Create a new controller instance.
32
     *
33
     * @return void
34
     */
35
    public function __construct(Content $content, Resource $resource, Repeater $repeater, Template $template, User $user)
36
    {
37
        $this->content = $content;
38
        $this->resource = $resource;
39
        $this->repeater = $repeater;
40
        $this->template = $template;
41
        $this->user = $user;
42
    }
43
44
    public function resourceIndex()
45
    {
46
        $resources = $this->resource->get();
47
48
        return view('chuckcms::backend.content.resource.index', compact('resources'));
49
    }
50
51
    public function resourceCreate()
52
    {
53
        return view('chuckcms::backend.content.resource.create');
54
    }
55
56
    public function resourceEdit($slug)
57
    {
58
        $resource = Resource::where('slug', $slug)->first();
59
60
        return view('chuckcms::backend.content.resource.edit', compact('resource'));
61
    }
62
63
    public function resourceSave(Request $request)
64
    {
65
        //validate the request
66
        $this->validate(request(), [//@todo create custom Request class for site validation
67
            'slug'             => 'required',
68
            'resource_key.*'   => 'required',
69
            'resource_value.*' => 'required',
70
        ]);
71
72
        $resource = Resource::firstOrNew(['slug' => $request->get('slug')[0]]);
73
        $resource->slug = $request->get('slug')[0];
74
        $json = [];
75
        foreach (ChuckSite::getSupportedLocales() as $langKey => $langValue) {
76
            $count = count($request->get('resource_key')[$langKey]);
77
            for ($i = 0; $i < $count; $i++) {
78
                $json[$langKey][$request->get('resource_key')[$langKey][$i]] = $request->get('resource_value')[$langKey][$i];
79
            }
80
        }
81
        $resource->json = $json;
82
        $resource->save();
83
84
        return redirect()->route('dashboard.content.resources');
85
    }
86
87
    public function resourceDelete(Request $request)
88
    {
89
        $this->validate(request(), [//@todo create custom Request class for site validation
90
            'resource_id' => 'required',
91
        ]);
92
93
        $resource = Resource::where('id', $request->get('resource_id'))->first();
94
95
        if ($resource->delete()) {
96
            return 'success';
97
        } else {
98
            return 'error';
99
        }
100
    }
101
102
    public function repeaterIndex()
103
    {
104
        $repeaters = $this->content->where('type', 'repeater')->get();
105
106
        return view('chuckcms::backend.content.repeater.index', compact('repeaters'));
107
    }
108
109
    public function repeaterCreate()
110
    {
111
        $pageViews = $this->template->getPageViews();
112
113
        return view('chuckcms::backend.content.repeater.create', compact('pageViews'));
114
    }
115
116
    public function repeaterEdit($slug)
117
    {
118
        $pageViews = $this->template->getPageViews();
119
        $repeater = Content::where('slug', $slug)->first();
120
121
        return view('chuckcms::backend.content.repeater.edit', compact('pageViews', 'repeater'));
122
    }
123
124
    public function repeaterJson($slug)
125
    {
126
        $repeater = Content::where('slug', $slug)->first();
127
128
        $filename = $repeater->slug.'.json';
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Chuckbe\Chuckcms\Models\Content. Since you implemented __get, consider adding a @property annotation.
Loading history...
129
        $handle = fopen($filename, 'w+');
130
        fputs($handle, $repeater->toJson(JSON_PRETTY_PRINT));
131
        fclose($handle);
132
        $headers = ['Content-type'=> 'application/json'];
133
134
        return response()->download($filename, $filename, $headers)->deleteFileAfterSend();
135
136
        //return view('chuckcms::backend.content.repeater.edit', compact('pageViews', 'repeater'));
137
    }
138
139
    public function repeaterSave(Request $request)
140
    {
141
        //add validation / move to repository...
142
        $content = [];
143
        $content_slug = $request->get('content_slug');
144
        $fields_slug = $request->get('fields_slug');
145
        $count = count($fields_slug);
0 ignored issues
show
Bug introduced by
It seems like $fields_slug can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
        $count = count(/** @scrutinizer ignore-type */ $fields_slug);
Loading history...
146
        for ($i = 0; $i < $count; $i++) {
147
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['label'] = $request->get('fields_label')[$i];
148
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['type'] = $request->get('fields_type')[$i];
149
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['class'] = $request->get('fields_class')[$i];
150
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['placeholder'] = $request->get('fields_placeholder')[$i];
151
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['validation'] = $request->get('fields_validation')[$i];
152
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['value'] = $request->get('fields_value')[$i];
153
            $fieldsCount = count(explode(';', $request->get('fields_attributes_name')[$i]));
154
            for ($k = 0; $k < $fieldsCount; $k++) {
155
                $content['fields'][$content_slug.'_'.$fields_slug[$i]]['attributes'][explode(';', $request->get('fields_attributes_name')[$i])[$k]] = explode(';', $request->get('fields_attributes_value')[$i])[$k];
156
            }
157
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['required'] = $request->get('fields_required')[$i];
158
            $content['fields'][$content_slug.'_'.$fields_slug[$i]]['table'] = $request->get('fields_table')[$i];
159
        }
160
161
        $content['actions']['store'] = $request->get('action_store');
162
        if ($request->get('action_detail') == 'true') {
163
            $content['actions']['detail']['url'] = $request->get('action_detail_url');
164
            $content['actions']['detail']['page'] = $request->get('action_detail_page');
165
        } else {
166
            $content['actions']['detail'] = 'false';
167
        }
168
169
        $content['files'] = $request->get('files_allowed');
170
171
        Content::updateOrCreate(
172
            ['id' => $request->get('content_id')],
173
            ['slug'       => $request->get('content_slug'),
174
                'type'    => $request->get('content_type'),
175
                'content' => $content, ]
176
        );
177
178
        return redirect()->route('dashboard.content.repeaters');
179
    }
180
181
    public function repeaterImport(Request $request)
182
    {
183
        $this->validate(request(), [//@todo create custom Request class for page validation
184
            'slug' => 'required',
185
            'file' => 'required|file|mimetypes:application/json,application/octet-stream,text/plain',
186
        ]);
187
188
        $file_contents = file_get_contents($request->file('file'));
0 ignored issues
show
Bug introduced by
It seems like $request->file('file') can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
        $file_contents = file_get_contents(/** @scrutinizer ignore-type */ $request->file('file'));
Loading history...
189
190
        $new_slug = $request->get('slug');
191
        $old_slug = json_decode($file_contents, true)['slug'];
192
193
        $json_string = str_replace($old_slug, $new_slug, $file_contents);
194
        $json_file_array = json_decode($json_string, true);
195
196
        if (!array_key_exists('type', $json_file_array)) {
197
            $notification = ['type' => 'error', 'message' => 'The "type" key was not present in the JSON file.'];
198
199
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
200
        }
201
202
        if (!array_key_exists('content', $json_file_array)) {
203
            $notification = ['type' => 'error', 'message' => 'The "content" key was not present in the JSON file.'];
204
205
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
206
        }
207
208
        if (!array_key_exists('fields', $json_file_array['content'])) {
209
            $notification = ['type' => 'error', 'message' => 'The "fields" key was not present in the JSON file.'];
210
211
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
212
        }
213
214
        if (!array_key_exists('actions', $json_file_array['content'])) {
215
            $notification = ['type' => 'error', 'message' => 'The "actions" key was not present in the JSON file.'];
216
217
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
218
        }
219
220
        if (!array_key_exists('files', $json_file_array['content'])) {
221
            $notification = ['type' => 'error', 'message' => 'The "files" key was not present in the JSON file.'];
222
223
            return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
224
        }
225
226
        Content::updateOrCreate(
227
            ['id' => null],
228
            ['slug'       => $new_slug,
229
                'type'    => $json_file_array['type'],
230
                'content' => $json_file_array['content'], ]
231
        );
232
233
        $notification = ['type' => 'success', 'message' => 'The JSON file was successfully imported.'];
234
235
        return redirect()->route('dashboard.content.repeaters')->with('notification', $notification);
236
    }
237
238
    public function repeaterDelete(Request $request)
239
    {
240
        $this->validate(request(), [//@todo create custom Request class for site validation
241
            'content_id' => 'required',
242
        ]);
243
244
        $content = Content::where('id', $request->get('content_id'))->first();
245
        $repeaters = Repeater::where('slug', $content->slug)->delete();
0 ignored issues
show
Unused Code introduced by
The assignment to $repeaters is dead and can be removed.
Loading history...
Bug Best Practice introduced by
The property slug does not exist on Chuckbe\Chuckcms\Models\Content. Since you implemented __get, consider adding a @property annotation.
Loading history...
246
247
        if ($content->delete()) {
248
            return 'success';
249
        } else {
250
            return 'error';
251
        }
252
    }
253
254
    public function repeaterEntriesIndex($slug)
255
    {
256
        $content = Content::where('slug', $slug)->first();
257
        $repeaters = $this->repeater->where('slug', $slug)->get();
258
259
        return view('chuckcms::backend.content.repeater.entries.index', compact('content', 'repeaters'));
260
    }
261
262
    public function repeaterEntriesCreate($slug)
263
    {
264
        $content = Content::where('slug', $slug)->first();
265
266
        return view('chuckcms::backend.content.repeater.entries.create', compact('content'));
267
    }
268
269
    public function repeaterEntriesSave(Request $request)
270
    {
271
        $slug = $request->get('content_slug');
272
        $content = $this->content->getBySlug($slug);
273
        $rules = $content->getRules();
274
        $this->validate(request(), $rules);
275
        $store = $content->storeEntry($request);
276
        if ($store == 'success') {
277
            return redirect()->route('dashboard.content.repeaters.entries', ['slug' => $slug]);
278
        } else {
279
            // error catching ... ?
280
        }
281
    }
282
283
    public function repeaterEntriesEdit($slug, $id)
284
    {
285
        $content = Content::where('slug', $slug)->first();
286
        $repeater = Repeater::where('id', $id)->first();
287
288
        return view('chuckcms::backend.content.repeater.entries.edit', compact('content', 'repeater'));
289
    }
290
291
    /**
292
     * Delete the resource from the page.
293
     *
294
     * @param Request $request
295
     *
296
     * @return string $status
297
     */
298
    public function repeaterEntriesDelete(Request $request)
299
    {
300
        // AUTHORIZE ... COMES HERE
301
        $status = $this->content->deleteById($request->get('repeater_id'));
302
303
        return $status;
304
    }
305
}
306