| Conditions | 18 |
| Paths | 300 |
| Total Lines | 126 |
| Code Lines | 63 |
| 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 |
||
| 36 | public function index(Request $request, $forum, $topic) |
||
| 37 | { |
||
| 38 | // get the topic (and forum) mark time value from middleware |
||
| 39 | // @see \Coyote\Http\Middleware\ScrollToPost |
||
| 40 | $markTime = $request->attributes->get('mark_time'); |
||
| 41 | |||
| 42 | $this->gate = $this->getGateFactory(); |
||
| 43 | |||
| 44 | // current page... |
||
| 45 | $page = (int) $request->get('page'); |
||
| 46 | // number of answers |
||
| 47 | $replies = $topic->replies; |
||
| 48 | // number of posts per one page |
||
| 49 | $perPage = $this->postsPerPage($request); |
||
| 50 | |||
| 51 | // user with forum-update ability WILL see every post |
||
| 52 | if ($this->gate->allows('delete', $forum)) { |
||
| 53 | $this->post->pushCriteria(new WithTrashed()); |
||
| 54 | // user is able to see real number of posts in this topic |
||
| 55 | $replies = $topic->replies_real; |
||
| 56 | } |
||
| 57 | |||
| 58 | // user wants to show certain post. we need to calculate page number based on post id. |
||
| 59 | if ($request->has('p')) { |
||
| 60 | $page = $this->post->getPage(min(2147483647, (int) $request->get('p')), $topic->id, $perPage); |
||
| 61 | } |
||
| 62 | |||
| 63 | // build "more like this" block. it's important to send elasticsearch query before |
||
| 64 | // send SQL query to database because search() method exists only in Model and not Builder class. |
||
| 65 | $mlt = $this->getCacheFactory()->remember('mlt-post:' . $topic->id, 60 * 24, function () use ($topic) { |
||
| 66 | $this->forum->pushCriteria(new OnlyThoseWithAccess()); |
||
| 67 | |||
| 68 | $builder = new MoreLikeThisBuilder($topic, $this->forum->pluck('id')); |
||
| 69 | |||
| 70 | // search related topics |
||
| 71 | $mlt = $this->topic->search($builder); |
||
|
|
|||
| 72 | |||
| 73 | // it's important to reset criteria for the further queries |
||
| 74 | $this->forum->resetCriteria(); |
||
| 75 | return $mlt; |
||
| 76 | }); |
||
| 77 | |||
| 78 | $this->post->pushCriteria(new ObtainSubscribers($this->userId)); |
||
| 79 | |||
| 80 | // magic happens here. get posts for given topic (including first post for every page) |
||
| 81 | /* @var \Illuminate\Support\Collection $posts */ |
||
| 82 | $posts = $this->post->takeForTopic($topic->id, $topic->first_post_id, $page, $perPage); |
||
| 83 | $paginate = new LengthAwarePaginator($posts, $replies, $perPage, $page, ['path' => ' ']); |
||
| 84 | |||
| 85 | start_measure('Parsing...'); |
||
| 86 | $parser = $this->getParsers(); |
||
| 87 | |||
| 88 | /** @var \Coyote\Post $post */ |
||
| 89 | foreach ($posts as &$post) { |
||
| 90 | // parse post or get it from cache |
||
| 91 | $post->text = $parser['post']->parse($post->text); |
||
| 92 | |||
| 93 | if ((auth()->guest() || (auth()->check() && $this->auth->allow_sig)) && $post->sig) { |
||
| 94 | $post->sig = $parser['sig']->parse($post->sig); |
||
| 95 | } |
||
| 96 | |||
| 97 | foreach ($post->comments as &$comment) { |
||
| 98 | $comment->text = $parser['comment']->setUserId($comment->user_id)->parse($comment->text); |
||
| 99 | } |
||
| 100 | |||
| 101 | $post->setRelation('topic', $topic); |
||
| 102 | $post->setRelation('forum', $forum); |
||
| 103 | } |
||
| 104 | |||
| 105 | stop_measure('Parsing...'); |
||
| 106 | |||
| 107 | $postsId = $posts->pluck('id')->toArray(); |
||
| 108 | $dateTimeString = $posts->last()->created_at->toDateTimeString(); |
||
| 109 | |||
| 110 | if ($markTime[Topic::class] < $dateTimeString && $markTime[Forum::class] < $dateTimeString) { |
||
| 111 | // mark topic as read. the date MUST be data of last post on this page |
||
| 112 | $topic->markAsRead($dateTimeString, $this->guestId); |
||
| 113 | $isUnread = true; |
||
| 114 | |||
| 115 | if ($markTime[Forum::class] < $dateTimeString) { |
||
| 116 | $isUnread = $this->topic->isUnread( |
||
| 117 | $forum->id, |
||
| 118 | $markTime[Forum::class], |
||
| 119 | $this->guestId |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!$isUnread) { |
||
| 124 | $this->forum->markAsRead($forum->id, $this->guestId); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // create forum list for current user (according to user's privileges) |
||
| 129 | $this->pushForumCriteria(); |
||
| 130 | |||
| 131 | $treeBuilder = new TreeBuilder(); |
||
| 132 | $forumList = $treeBuilder->listBySlug($this->forum->list()); |
||
| 133 | |||
| 134 | $this->breadcrumb->push($topic->subject, route('forum.topic', [$forum->slug, $topic->id, $topic->slug])); |
||
| 135 | |||
| 136 | $flags = $activities = []; |
||
| 137 | |||
| 138 | if ($this->gate->allows('delete', $forum) || $this->gate->allows('move', $forum)) { |
||
| 139 | $reasonList = Reason::pluck('name', 'id')->toArray(); |
||
| 140 | |||
| 141 | if ($this->gate->allows('delete', $forum)) { |
||
| 142 | $flags = $this->getFlags($postsId); |
||
| 143 | $activities = $this->getActivities($postsId); |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->forum->skipCriteria(true); |
||
| 147 | $adminForumList = $treeBuilder->listBySlug($this->forum->list()); |
||
| 148 | } |
||
| 149 | |||
| 150 | // informacje o powodzie zablokowania watku, przeniesienia itp |
||
| 151 | $warnings = $this->getWarnings($topic); |
||
| 152 | |||
| 153 | $form = $this->getForm($forum, $topic); |
||
| 154 | |||
| 155 | return $this->view( |
||
| 156 | 'forum.topic', |
||
| 157 | compact('posts', 'forum', 'topic', 'paginate', 'forumList', 'adminForumList', 'reasonList', 'form', 'mlt', 'flags', 'warnings', 'activities') |
||
| 158 | )->with([ |
||
| 159 | 'markTime' => $markTime[Topic::class] ? $markTime[Topic::class] : $markTime[Forum::class], |
||
| 160 | 'subscribers' => $this->userId ? $topic->subscribers()->pluck('topic_id', 'user_id') : [], |
||
| 161 | 'author_id' => $posts[0]->user_id |
||
| 162 | ]); |
||
| 301 |