Total Complexity | 63 |
Total Lines | 436 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | Class Controller extends BaseController { |
||
24 | |||
25 | |||
26 | /** |
||
27 | * @var DatastoreRepository |
||
28 | */ |
||
29 | protected $datastoreRepository; |
||
30 | |||
31 | /** |
||
32 | * UserController constructor. |
||
33 | * |
||
34 | * @param DatastoreRepository $datastoreRepository |
||
35 | */ |
||
36 | public function __construct(DatastoreRepository $datastoreRepository) |
||
37 | { |
||
38 | $this->datastoreRepository = $datastoreRepository; |
||
39 | } |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * @param Request $request |
||
45 | * |
||
46 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
47 | */ |
||
48 | public function list($asset,Request $request){ |
||
49 | |||
50 | $assetData = Helpers::parseAssetType($asset); |
||
51 | |||
52 | return view('phpsa-datastore::backend.ams.list') |
||
53 | ->withAsset($assetData) |
||
54 | ->withAssetType($asset) |
||
55 | ->withContent($this->datastoreRepository->paginateAssets($assetData, 25, 'page' )); |
||
56 | |||
57 | } |
||
58 | |||
59 | public function inject(Request $request){ |
||
60 | $assetData = Helpers::parseAssetType($request->input('asset'), true); |
||
61 | $newAsset = Datastore::getAsset($assetData); |
||
62 | return response($newAsset->injectForm($request->input('idx'))); |
||
63 | } |
||
64 | |||
65 | |||
66 | protected function _getKids($asset){ |
||
67 | $children = []; |
||
68 | if ( ! empty($asset->children)) |
||
69 | { |
||
70 | $children = Helpers::assetInfo($asset->children); |
||
71 | $children['classname'] = $asset->children; |
||
72 | $children['path'] = Helpers::getPath($asset->children); |
||
73 | |||
74 | $kids = $asset->children(); |
||
75 | |||
76 | if (!$kids) |
||
77 | { |
||
78 | $kids[] = Datastore::getAsset($children['classname']); |
||
79 | } |
||
80 | |||
81 | $children['kids'] = $kids; |
||
82 | } |
||
83 | return $children; |
||
84 | } |
||
85 | |||
86 | protected function _getParents($asset){ |
||
87 | |||
88 | $type = str_replace('\\', "\\\\", $asset->type); |
||
89 | $parentAssets = $this->datastoreRepository->where('accept', '%' . $type . '%', 'like')->distinct()->get(['type', 'name', 'accept_limit'])->toArray(); |
||
90 | |||
91 | if ($parentAssets) |
||
92 | { |
||
93 | // get these parents |
||
94 | foreach ($parentAssets as $k => $parent) |
||
95 | { |
||
96 | $parentAssets[$k]['key'] = Str::slug($parent['type']); |
||
97 | $parents = $this->datastoreRepository->where('type', $parent['type'])->get()->toArray(); |
||
98 | if ($parents) |
||
99 | { |
||
100 | foreach ($parents as $key => $p) |
||
101 | { |
||
102 | |||
103 | $found = DatastoreDatastore::where('datastore_id', $asset->getId()) |
||
104 | ->where('datastore2_id', $p['id'])->exists(); |
||
105 | if ($found) |
||
106 | { |
||
107 | $parents[$key]['assigned_and_found'] = true; |
||
108 | } |
||
109 | } |
||
110 | $parentAssets[$k]['related'] = $parents; |
||
111 | } |
||
112 | else |
||
113 | { |
||
114 | $parentAssets[$k]['related'] = false; |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | return $parentAssets; |
||
119 | } |
||
120 | |||
121 | protected function _getPageData($asset){ |
||
122 | return ($asset->is_child || !$asset->id )? null : DatastorePages::where('asset', $asset->id)->first(); |
||
123 | } |
||
124 | |||
125 | public function create($assetType, Request $request){ |
||
126 | |||
127 | |||
128 | $assetData = Helpers::parseAssetType($assetType, true); |
||
129 | $newAsset = Datastore::getAsset($assetData); |
||
130 | |||
131 | if($newAsset->max_instances > 0){ |
||
132 | $count = DatastoreModel::where('type', $assetData)->count(); |
||
133 | if($count >= $newAsset->max_instances){ |
||
134 | return redirect()->route('admin.ams.content.list', $assetType)->withFlashDanger("You can only create " . $newAsset->max_instances . " instances of this Asset"); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | $children = $this->_getKids($newAsset); |
||
140 | $parentAssets = $this->_getParents($newAsset); |
||
141 | $pageData = $this->_getPageData($newAsset); |
||
142 | |||
143 | return view('phpsa-datastore::backend.ams.create') |
||
144 | ->withAsset($newAsset) |
||
145 | ->withParents($parentAssets) |
||
146 | ->withChildren($children) |
||
147 | ->withPageData($pageData); |
||
148 | } |
||
149 | |||
150 | public function edit($assetType, $id){ |
||
164 | } |
||
165 | |||
166 | public function save($assetType , Request $request){ |
||
167 | |||
168 | |||
169 | $assetData = Helpers::parseAssetType($assetType, true); |
||
170 | $newAsset = Datastore::getAsset($assetData, $request->input('id')); |
||
171 | |||
172 | $form = $request->all(); |
||
173 | |||
174 | $to_delete = (isset($form['assetRemove'])) ? $form['assetRemove'] : false; |
||
175 | unset($form['assetRemove']); |
||
176 | |||
177 | |||
178 | $form_valid = $newAsset->validate($form); |
||
179 | if(!$form_valid){ |
||
180 | die("@TODO STILL"); |
||
181 | } |
||
182 | |||
183 | $newAsset->populateAsset($form); |
||
184 | |||
185 | if ($to_delete) |
||
186 | { |
||
187 | foreach ($to_delete as $kill_asset => $del) |
||
188 | { |
||
189 | foreach ($del as $uniq => $a) |
||
190 | { |
||
191 | if ($a['id']) |
||
192 | { |
||
193 | $nnasset = Datastore::getAssetById($id); |
||
194 | if ($nnasset->ownDatastore()) |
||
195 | { |
||
196 | foreach ($nnasset->ownDatastore() as $e) |
||
197 | { |
||
198 | Datastore::getAssetById($e->id); |
||
199 | } |
||
200 | } |
||
201 | $nnasset->delete(); |
||
202 | } |
||
203 | unset($form['assetInjectionform'][$kill_asset][$uniq]); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | $kill_list = array(); |
||
208 | // check what we are to remove |
||
209 | if (!empty($form['related_id'])) |
||
210 | { |
||
211 | foreach ($form['related_id'] as $remove) |
||
212 | { |
||
213 | if (!in_array($remove, $form['related_assets'])) |
||
214 | { |
||
215 | $kill_list[] = $remove; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | |||
221 | |||
222 | $id = $newAsset->store(); |
||
223 | |||
224 | |||
225 | // check for multiforms |
||
226 | if (isset($form['assetInjectionform'])) |
||
227 | { |
||
228 | //dd($form['assetInjectionform']);exit; |
||
229 | foreach ($form['assetInjectionform'] as $masset => $mdata) |
||
230 | { |
||
231 | foreach ($mdata as $childform) |
||
232 | { |
||
233 | $childformId = !empty($childform['id']) ? $childform['id'] : null; |
||
234 | $childasset = Datastore::getAsset($masset, $childformId); |
||
235 | $childasset->populateAsset($childform); |
||
236 | $nid = $childasset->store($id); |
||
237 | |||
238 | $newAsset->ownDatastore[] = $childasset; |
||
239 | |||
240 | //$this->db->query('update datastore set datastore_id = ? where id = ?', array($id, $nid)); |
||
241 | // now we fikken cheat coz this isn't working the way it should |
||
242 | // now add them as children to the asset |
||
243 | //zp('debug')->pre($childasset); |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | if (!empty($form['related_assets'])) |
||
249 | { |
||
250 | foreach ($form['related_assets'] as $asset) |
||
251 | { |
||
252 | DatastoreDatastore::firstOrCreate(['datastore_id' => $id, 'datastore2_id' => $asset]); |
||
253 | // $found = $this->db->query('select id from datastore_datastore where datastore_id = ? and datastore2_id = ?', array($id, $asset))->row(); |
||
254 | |||
255 | // // not found, we need to add it |
||
256 | // if (!$found) |
||
257 | // { |
||
258 | // $this->db->query('insert into datastore_datastore (datastore_id, datastore2_id) values (?, ?)', array($id, $asset)); |
||
259 | // } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | if ($kill_list) |
||
264 | { |
||
265 | foreach ($kill_list as $r) |
||
266 | { |
||
267 | $deletedRows = DatastoreDatastore::where('datastore_id', $id)->where('datastore2_id', $r)->delete(); |
||
268 | // $found = $this->db->query('select id from datastore_datastore where datastore_id = ? and datastore2_id = ?', array($id, $r))->row(); |
||
269 | |||
270 | // //if found, remove it |
||
271 | // if ($found) |
||
272 | // { |
||
273 | // $this->db->query('delete from datastore_datastore where id =?', array($found)); |
||
274 | // } |
||
275 | } |
||
276 | } |
||
277 | |||
278 | if(!empty($form['page_title']) && !empty($form['page_slug'])){ |
||
279 | $pageData = DatastorePages::firstOrNew(['asset' => $id]); |
||
280 | $pageData->title = $form['page_title']; |
||
281 | $pageData->slug = $form['page_slug']; |
||
282 | $pageData->asset = $id; |
||
283 | $pageData->save(); |
||
284 | } |
||
285 | |||
286 | return redirect()->route('admin.ams.content.list', $assetType)->withFlashSuccess(__('phpsa-datastore::backend.labels.content.created')); |
||
287 | |||
288 | |||
289 | } |
||
290 | |||
291 | |||
292 | public function destroy($id, Request $request){ |
||
293 | $asset = DatastoreModel::findOrFail($id); |
||
294 | if(!$asset || $asset->namespace !== 'asset'){ |
||
295 | throw new GeneralException("NOT FOUND"); |
||
296 | } |
||
297 | $path = $asset->content_path; |
||
298 | $asset->delete(); |
||
299 | |||
300 | return redirect()->route('admin.ams.content.list', $asset->content_path)->withFlashSuccess(__('phpsa-datastore::backend.labels.content.deleted')); |
||
301 | |||
302 | |||
303 | } |
||
304 | |||
305 | public function slug(Request $request) |
||
306 | { |
||
307 | DB::enableQueryLog(); |
||
308 | |||
309 | $slug = $request->input('page_slug'); |
||
310 | $id = (int)$request->input('id'); |
||
311 | $generate = $request->input('generate'); |
||
312 | |||
313 | |||
314 | if($generate){ |
||
315 | $slug = Str::slug($slug); |
||
316 | } |
||
317 | |||
318 | $exists = DatastorePages::where('slug', $slug)->where('asset', '!=', $id)->exists(); |
||
319 | |||
320 | if($generate){ |
||
321 | return response()->json([ |
||
322 | 'slug' => $slug, |
||
323 | 'available' => $exists ? 0 : 1 |
||
324 | ]); |
||
325 | } |
||
326 | return response()->json($exists ? "Slug already in use": true); |
||
327 | |||
328 | |||
329 | } |
||
330 | |||
331 | public function file(Request $request) { |
||
332 | $this->validate($request, [ |
||
333 | 'file' => 'required' |
||
334 | ]); |
||
335 | |||
336 | $originalFile= $request->file('file'); |
||
337 | |||
338 | $t = time(); |
||
339 | $filename = Str::slug($t.$originalFile->getClientOriginalName(),"."); |
||
340 | |||
341 | $path = $request->file('file')->storeAs( |
||
342 | 'public', $filename |
||
343 | ); |
||
344 | |||
345 | return response()->json(["file" => $filename]); |
||
346 | } |
||
347 | |||
348 | public function image(Request $request) { |
||
349 | $this->validate($request, [ |
||
350 | 'file' => 'image|required|mimes:jpeg,png,jpg,gif,svg' |
||
351 | ]); |
||
352 | |||
353 | $originalImage= $request->file('file'); |
||
354 | |||
355 | $t = time(); |
||
356 | $filename = Str::slug($t.$originalImage->getClientOriginalName(),"."); |
||
357 | |||
358 | |||
359 | if(!is_dir(public_path().'/vendor/phpsa-datastore/thumbs')){ |
||
360 | mkdir(public_path().'/vendor/phpsa-datastore/thumbs', 0755, true); |
||
361 | } |
||
362 | |||
363 | |||
364 | $thumbnailImage = Image::make($originalImage); |
||
365 | $thumbnailPath = public_path().'/vendor/phpsa-datastore/img/'; |
||
366 | $originalPath = public_path().'/vendor/phpsa-datastore/thumbs/'; |
||
367 | $thumbnailImage->save($originalPath.$filename); |
||
368 | |||
369 | $thumbnailImage->resize(150,150, function ($constraint) { |
||
370 | $constraint->aspectRatio(); |
||
371 | }); |
||
372 | $thumbnailImage->save($thumbnailPath.$filename); |
||
373 | |||
374 | |||
375 | return response()->json(["file" => $filename]); |
||
376 | } |
||
377 | |||
378 | |||
379 | |||
380 | public function indentityAutocomplete(Request $request){ |
||
397 | } |
||
398 | |||
399 | |||
400 | |||
401 | //Testing CallbacksAuth::attempt([ |
||
402 | public function getTypeData(Request $request) { |
||
403 | $term = $request->input('term'); |
||
459 | } |
||
460 | |||
461 | } |
||
462 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths