| Conditions | 11 |
| Paths | 10 |
| Total Lines | 105 |
| Code Lines | 79 |
| 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 namespace BB\Http\Controllers; |
||
| 132 | public function store($userId) |
||
| 133 | { |
||
| 134 | $user = User::findWithPermission($userId); |
||
| 135 | |||
| 136 | if ( ! \Auth::user()->hasRole('admin') && ! \Auth::user()->hasRole('finance')) { |
||
| 137 | throw new \BB\Exceptions\AuthenticationException; |
||
| 138 | } |
||
| 139 | |||
| 140 | \Log::debug('Manual payment endpoint getting hit. account/{id}/payment. paymentController@store '.json_encode(\Input::all())); |
||
| 141 | |||
| 142 | $reason = \Input::get('reason'); |
||
| 143 | |||
| 144 | if ($reason == 'subscription') { |
||
| 145 | $payment = new Payment([ |
||
| 146 | 'reason' => $reason, |
||
| 147 | 'source' => \Input::get('source'), |
||
| 148 | 'source_id' => '', |
||
| 149 | 'amount' => $user->monthly_subscription, |
||
| 150 | 'amount_minus_fee' => $user->monthly_subscription, |
||
| 151 | 'status' => 'paid' |
||
| 152 | ]); |
||
| 153 | $user->payments()->save($payment); |
||
| 154 | |||
| 155 | $user->extendMembership(\Input::get('source'), \Carbon\Carbon::now()->addMonth()); |
||
| 156 | |||
| 157 | } elseif ($reason == 'induction') { |
||
| 158 | if (\Input::get('source') == 'manual') { |
||
| 159 | $ref = \Input::get('induction_key'); |
||
| 160 | ($item = $this->equipmentRepository->findBySlug($ref)) || App::abort(404); |
||
| 161 | $payment = new Payment([ |
||
| 162 | 'reason' => $reason, |
||
| 163 | 'source' => 'manual', |
||
| 164 | 'source_id' => '', |
||
| 165 | 'amount' => $item->cost, |
||
| 166 | 'amount_minus_fee' => $item->cost, |
||
| 167 | 'status' => 'paid' |
||
| 168 | ]); |
||
| 169 | $payment = $user->payments()->save($payment); |
||
| 170 | Induction::create([ |
||
| 171 | 'user_id' => $user->id, |
||
| 172 | 'key' => $ref, |
||
| 173 | 'paid' => true, |
||
| 174 | 'payment_id' => $payment->id |
||
| 175 | ]); |
||
| 176 | } else { |
||
| 177 | throw new \BB\Exceptions\NotImplementedException(); |
||
| 178 | } |
||
| 179 | } elseif ($reason == 'door-key') { |
||
| 180 | $payment = new Payment([ |
||
| 181 | 'reason' => $reason, |
||
| 182 | 'source' => \Input::get('source'), |
||
| 183 | 'source_id' => '', |
||
| 184 | 'amount' => 10, |
||
| 185 | 'amount_minus_fee' => 10, |
||
| 186 | 'status' => 'paid' |
||
| 187 | ]); |
||
| 188 | $user->payments()->save($payment); |
||
| 189 | |||
| 190 | $user->key_deposit_payment_id = $payment->id; |
||
| 191 | $user->save(); |
||
| 192 | |||
| 193 | } elseif ($reason == 'storage-box') { |
||
| 194 | $payment = new Payment([ |
||
| 195 | 'reason' => $reason, |
||
| 196 | 'source' => \Input::get('source'), |
||
| 197 | 'source_id' => '', |
||
| 198 | 'amount' => 5, |
||
| 199 | 'amount_minus_fee' => 5, |
||
| 200 | 'status' => 'paid' |
||
| 201 | ]); |
||
| 202 | $user->payments()->save($payment); |
||
| 203 | |||
| 204 | $user->storage_box_payment_id = $payment->id; |
||
| 205 | $user->save(); |
||
| 206 | } elseif ($reason == 'balance') { |
||
| 207 | $amount = \Input::get('amount') * 1; //convert the users amount into a number |
||
| 208 | if ( ! is_numeric($amount)) { |
||
| 209 | $exceptionErrors = new \Illuminate\Support\MessageBag(['amount' => 'Invalid amount']); |
||
| 210 | throw new \BB\Exceptions\FormValidationException('Not a valid amount', $exceptionErrors); |
||
| 211 | } |
||
| 212 | $payment = new Payment([ |
||
| 213 | 'reason' => 'balance', |
||
| 214 | 'source' => \Input::get('source'), |
||
| 215 | 'source_id' => '', |
||
| 216 | 'amount' => $amount, |
||
| 217 | 'amount_minus_fee' => $amount, |
||
| 218 | 'status' => 'paid' |
||
| 219 | ]); |
||
| 220 | $user->payments()->save($payment); |
||
| 221 | |||
| 222 | $memberCreditService = \App::make('\BB\Services\Credit'); |
||
| 223 | $memberCreditService->setUserId($user->id); |
||
| 224 | $memberCreditService->recalculate(); |
||
| 225 | |||
| 226 | //This needs to be improved |
||
| 227 | \Notification::success('Payment recorded'); |
||
| 228 | |||
| 229 | return \Redirect::route('account.bbcredit.index', $user->id); |
||
| 230 | } else { |
||
| 231 | throw new \BB\Exceptions\NotImplementedException(); |
||
| 232 | } |
||
| 233 | \Notification::success('Payment recorded'); |
||
| 234 | |||
| 235 | return \Redirect::route('account.show', [$user->id]); |
||
| 236 | } |
||
| 237 | |||
| 385 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.