Conditions | 10 |
Paths | 7 |
Total Lines | 57 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | public function read(): ?string |
||
31 | { |
||
32 | // get raw path without controller-action |
||
33 | $rawPath = $this->request->getPathWithoutControllerAction(); |
||
34 | $arrayPath = explode('/', $rawPath); |
||
35 | // get category and content item path as string |
||
36 | $contentPath = array_pop($arrayPath); |
||
37 | $categoryPath = implode('/', $arrayPath); |
||
38 | |||
39 | // try to find category object by string pathway |
||
40 | $categoryRecord = ContentCategory::getByPath($categoryPath); |
||
41 | |||
42 | // if no categories are available for this path - throw exception |
||
43 | if (!$categoryRecord || $categoryRecord->count() < 1) { |
||
44 | throw new NotFoundException(__('Page not found')); |
||
45 | } |
||
46 | |||
47 | // try to find content entity record |
||
48 | $contentRecord = ContentEntity::where('path', '=', $contentPath) |
||
49 | ->where('category_id', '=', $categoryRecord->id); |
||
50 | $trash = false; |
||
51 | |||
52 | // if no entity is founded for this path lets try to find on trashed |
||
53 | if (!$contentRecord || $contentRecord->count() !== 1) { |
||
|
|||
54 | // check if user can access to content list on admin panel |
||
55 | if (!App::$User->isAuth() || !App::$User->identity()->role->can('Admin/Content/Index')) { |
||
56 | throw new NotFoundException(__('Content not found!')); |
||
57 | } |
||
58 | // lets try to find in trashed |
||
59 | $contentRecord->withTrashed(); |
||
60 | // no way, lets throw exception |
||
61 | if ($contentRecord->count() !== 1) { |
||
62 | throw new NotFoundException(); |
||
63 | } |
||
64 | // set trashed marker for this item |
||
65 | $trash = true; |
||
66 | } |
||
67 | |||
68 | // lets init entity model for content transfer to view |
||
69 | $model = new EntityContentRead($categoryRecord, $contentRecord->first()); |
||
70 | $search = null; |
||
71 | // check if similar search is enabled for item category |
||
72 | if ((bool)$model->getCategory()->getProperty('showSimilar') && !$trash) { |
||
73 | $search = new EntityContentSearch($model->title, $model->id, $model->getCategory()->id); |
||
74 | } |
||
75 | |||
76 | // define read event |
||
77 | App::$Event->run(static::EVENT_CONTENT_READ, [ |
||
78 | 'model' => $model |
||
79 | ]); |
||
80 | |||
81 | // render view output |
||
82 | return $this->view->render('content/read', [ |
||
83 | 'model' => $model, |
||
84 | 'search' => $search, |
||
85 | 'trash' => $trash, |
||
86 | 'configs' => $this->getConfigs() |
||
87 | ]); |
||
90 |