Conditions | 10 |
Paths | 7 |
Total Lines | 59 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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 |
||
31 | public function read() |
||
32 | { |
||
33 | // get raw path without controller-action |
||
34 | $rawPath = $this->request->getPathWithoutControllerAction(); |
||
35 | $arrayPath = explode('/', $rawPath); |
||
36 | // get category and content item path as string |
||
37 | $contentPath = array_pop($arrayPath); |
||
38 | $categoryPath = implode('/', $arrayPath); |
||
39 | |||
40 | // try to find category object by string pathway |
||
41 | $categoryRecord = ContentCategory::getByPath($categoryPath); |
||
42 | |||
43 | // if no categories are available for this path - throw exception |
||
44 | if ($categoryRecord === null || $categoryRecord->count() < 1) { |
||
45 | throw new NotFoundException(__('Page not found')); |
||
46 | } |
||
47 | |||
48 | // try to find content entity record |
||
49 | $contentRecord = ContentEntity::where('path', '=', $contentPath) |
||
50 | ->where('category_id', '=', $categoryRecord->id); |
||
51 | $trash = false; |
||
52 | |||
53 | // if no entity is founded for this path lets try to find on trashed |
||
54 | if ($contentRecord === null || $contentRecord->count() !== 1) { |
||
55 | // check if user can access to content list on admin panel |
||
56 | if (!App::$User->isAuth() || !App::$User->identity()->role->can('Admin/Content/Index')) { |
||
57 | throw new NotFoundException(); |
||
58 | } |
||
59 | // lets try to find in trashed |
||
60 | $contentRecord->withTrashed(); |
||
61 | // no way, lets throw exception |
||
62 | if ($contentRecord->count() !== 1) { |
||
63 | throw new NotFoundException(); |
||
64 | } |
||
65 | // set trashed marker for this item |
||
66 | $trash = true; |
||
67 | } |
||
68 | |||
69 | // lets init entity model for content transfer to view |
||
70 | $model = new EntityContentRead($categoryRecord, $contentRecord->first()); |
||
|
|||
71 | $search = null; |
||
72 | // check if similar search is enabled for item category |
||
73 | if ((int)$model->getCategory()->getProperty('showSimilar') === 1 && $trash === false) { |
||
74 | $search = new EntityContentSearch($model->title, $model->id, $model->getCategory()->id); |
||
75 | } |
||
76 | |||
77 | // define read event |
||
78 | App::$Event->run(static::EVENT_CONTENT_READ, [ |
||
79 | 'model' => $model |
||
80 | ]); |
||
81 | |||
82 | // render view output |
||
83 | return $this->view->render('read', [ |
||
84 | 'model' => $model, |
||
85 | 'search' => $search, |
||
86 | 'trash' => $trash, |
||
87 | 'configs' => $this->getConfigs() |
||
88 | ]); |
||
89 | } |
||
90 | } |
||
91 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: