| Conditions | 11 |
| Paths | 8 |
| Total Lines | 80 |
| Code Lines | 44 |
| 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 |
||
| 49 | public function save(Request $request, $microblog) |
||
| 50 | { |
||
| 51 | $this->validate($request, [ |
||
| 52 | 'text' => 'required|string|max:10000|throttle:' . $microblog->id |
||
| 53 | ]); |
||
| 54 | |||
| 55 | $data = $request->only(['text']); |
||
| 56 | |||
| 57 | if (!$microblog->exists) { |
||
| 58 | $user = $this->auth; |
||
| 59 | $data['user_id'] = $user->id; |
||
| 60 | } else { |
||
| 61 | $this->authorize('update', $microblog); |
||
| 62 | |||
| 63 | $user = $this->user->find($microblog->user_id, ['id', 'name', 'is_blocked', 'is_active', 'photo']); |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($request->has('thumbnail') || count($microblog->media) > 0) { |
||
| 67 | /** @var \Coyote\Services\Media\MediaInterface $media */ |
||
| 68 | foreach ($microblog->media as $media) { |
||
| 69 | if (!in_array($media->getFilename(), $request->get('thumbnail', []))) { |
||
| 70 | $media->delete(); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $microblog->media = $request->get('thumbnail'); |
||
| 75 | } |
||
| 76 | |||
| 77 | $microblog->fill($data); |
||
| 78 | |||
| 79 | $this->transaction(function () use (&$microblog, $user) { |
||
| 80 | $microblog->save(); |
||
| 81 | $object = (new Stream_Microblog())->map($microblog); |
||
| 82 | |||
| 83 | if ($microblog->wasRecentlyCreated) { |
||
| 84 | // increase reputation points |
||
| 85 | app('reputation.microblog.create')->map($microblog)->save(); |
||
| 86 | |||
| 87 | // put this to activity stream |
||
| 88 | stream(Stream_Create::class, $object); |
||
| 89 | |||
| 90 | $helper = new LoginHelper(); |
||
| 91 | // get id of users that were mentioned in the text |
||
| 92 | $usersId = $helper->grab($microblog->html); |
||
| 93 | |||
| 94 | if (!empty($usersId)) { |
||
| 95 | app('notification.microblog.login')->with([ |
||
| 96 | 'users_id' => $usersId, |
||
| 97 | 'sender_id' => $user->id, |
||
| 98 | 'sender_name' => $user->name, |
||
| 99 | 'subject' => excerpt($microblog->html), |
||
| 100 | 'text' => $microblog->html, |
||
| 101 | 'url' => UrlBuilder::microblog($microblog) |
||
| 102 | ])->notify(); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($this->auth->allow_subscribe) { |
||
| 106 | // enable subscribe button |
||
| 107 | $microblog->subscribe_on = true; |
||
| 108 | $microblog->subscribers()->create(['user_id' => $user->id]); |
||
| 109 | } |
||
| 110 | } else { |
||
| 111 | stream(Stream_Update::class, $object); |
||
| 112 | } |
||
| 113 | |||
| 114 | $helper = new HashHelper(); |
||
| 115 | $microblog->setTags($helper->grab($microblog->html)); |
||
| 116 | |||
| 117 | event(new MicroblogWasSaved($microblog)); |
||
| 118 | }); |
||
| 119 | |||
| 120 | // do przekazania do widoku... |
||
| 121 | foreach (['name', 'is_blocked', 'is_active', 'photo'] as $key) { |
||
| 122 | $microblog->{$key} = $user->{$key}; |
||
| 123 | } |
||
| 124 | |||
| 125 | // passing html version of the entry... |
||
| 126 | $microblog->text = $microblog->html; |
||
| 127 | |||
| 128 | return view(!$microblog->wasRecentlyCreated ? 'microblog.partials.text' : 'microblog.partials.microblog')->with('microblog', $microblog); |
||
| 129 | } |
||
| 217 |