| Conditions | 15 |
| Paths | 12 |
| Total Lines | 108 |
| Code Lines | 59 |
| 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 |
||
| 27 | public function index($post, Request $request) |
||
| 28 | { |
||
| 29 | // it must be like that. only if reason has been chosen, we need to validate it. |
||
| 30 | if ($request->get('reason')) { |
||
| 31 | $this->validate($request, ['reason' => 'int|exists:forum_reasons,id']); |
||
| 32 | } |
||
| 33 | |||
| 34 | // Step 1. Get post category |
||
| 35 | $forum = &$post->forum; |
||
| 36 | |||
| 37 | // Step 2. Does user really have permission to delete this post? |
||
| 38 | $this->authorize('delete', [$post, $forum]); |
||
| 39 | |||
| 40 | // Step 3. Maybe user does not have an access to this category? |
||
| 41 | $forum->userCanAccess($this->userId) || abort(401, 'Unauthorized'); |
||
| 42 | $topic = &$post->topic; |
||
| 43 | |||
| 44 | // Step 4. Only moderators can delete this post if topic (or forum) was locked |
||
| 45 | if ($this->getGateFactory()->denies('delete', $forum)) { |
||
| 46 | if ($topic->is_locked || $forum->is_locked || $post->id < $topic->last_post_id || $post->deleted_at) { |
||
| 47 | abort(401, 'Unauthorized'); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | $url = $this->transaction(function () use ($post, $topic, $forum, $request) { |
||
| 52 | $url = UrlBuilder::topic($topic); |
||
| 53 | |||
| 54 | $notification = [ |
||
| 55 | 'sender_id' => $this->userId, |
||
| 56 | 'sender_name' => $this->auth->name, |
||
| 57 | 'subject' => str_limit($topic->subject, 84) |
||
| 58 | ]; |
||
| 59 | |||
| 60 | $reason = null; |
||
| 61 | |||
| 62 | if ($request->get('reason')) { |
||
| 63 | $reason = Reason::find($request->get('reason')); |
||
| 64 | |||
| 65 | $notification = array_merge($notification, [ |
||
| 66 | 'excerpt' => $reason->name, |
||
| 67 | 'reasonName' => $reason->name, |
||
| 68 | 'reasonText' => $reason->description |
||
| 69 | ]); |
||
| 70 | } |
||
| 71 | |||
| 72 | // if this is the first post in topic... we must delete whole thread |
||
| 73 | if ($post->id === $topic->first_post_id) { |
||
| 74 | $redirect = redirect()->route('forum.category', [$forum->slug]); |
||
| 75 | |||
| 76 | $subscribersId = $topic->subscribers()->pluck('user_id'); |
||
| 77 | if ($post->user_id !== null) { |
||
|
|
|||
| 78 | $subscribersId[] = $post->user_id; |
||
| 79 | } |
||
| 80 | |||
| 81 | $topic->delete(); |
||
| 82 | // delete topic's flag |
||
| 83 | $this->getFlagFactory()->deleteBy('topic_id', $topic->id, $this->userId); |
||
| 84 | |||
| 85 | if ($subscribersId) { |
||
| 86 | app('notification.topic.delete') |
||
| 87 | ->with($notification) |
||
| 88 | ->setUsersId($subscribersId->toArray()) |
||
| 89 | ->notify(); |
||
| 90 | } |
||
| 91 | |||
| 92 | // fire the event. it can be used to delete row from "pages" table or from search index |
||
| 93 | event(new TopicWasDeleted($topic)); |
||
| 94 | |||
| 95 | $object = (new Stream_Topic())->map($topic); |
||
| 96 | $target = (new Stream_Forum())->map($forum); |
||
| 97 | } else { |
||
| 98 | $subscribersId = $post->subscribers()->pluck('user_id'); |
||
| 99 | |||
| 100 | if ($post->user_id !== null) { |
||
| 101 | $subscribersId[] = $post->user_id; |
||
| 102 | } |
||
| 103 | |||
| 104 | $post->delete(); |
||
| 105 | // delete post's flags |
||
| 106 | $this->getFlagFactory()->deleteBy('post_id', $post->id, $this->userId); |
||
| 107 | |||
| 108 | if ($subscribersId) { |
||
| 109 | app('notification.post.delete') |
||
| 110 | ->with($notification) |
||
| 111 | ->setUrl($url) |
||
| 112 | ->setUsersId($subscribersId->toArray()) |
||
| 113 | ->notify(); |
||
| 114 | } |
||
| 115 | |||
| 116 | $url .= '?p=' . $post->id . '#id' . $post->id; |
||
| 117 | |||
| 118 | $redirect = back(); |
||
| 119 | // fire the event. delete from search index |
||
| 120 | event(new PostWasDeleted($post)); |
||
| 121 | |||
| 122 | $object = (new Stream_Post(['url' => $url]))->map($post); |
||
| 123 | $target = (new Stream_Topic())->map($topic); |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!empty($reason)) { |
||
| 127 | $object->reasonName = $reason->name; |
||
| 128 | } |
||
| 129 | |||
| 130 | stream(Stream_Delete::class, $object, $target); |
||
| 131 | return $redirect->with('success', 'Post został usunięty.'); |
||
| 132 | }); |
||
| 133 | |||
| 134 | return $url; |
||
| 135 | } |
||
| 137 |