| Conditions | 14 |
| Paths | 16 |
| Total Lines | 56 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 19 | public function __invoke(Request $request) |
||
| 20 | { |
||
| 21 | Stripe\Stripe::setApiKey(\Config::get('services.stripe.secret')); |
||
| 22 | Stripe\Plan::all(); |
||
| 23 | $data = request()->all(); |
||
| 24 | |||
| 25 | if ($data) { |
||
|
|
|||
| 26 | switch ($data['type']) { |
||
| 27 | case 'customer.subscription.deleted': |
||
| 28 | $user = User::where('stripe_id', $data['data']['object']['customer'])->first(); |
||
| 29 | if ($user) { |
||
| 30 | $user->role_id = 4; |
||
| 31 | $user->save(); |
||
| 32 | } |
||
| 33 | break; |
||
| 34 | |||
| 35 | case 'customer.subscription.created': |
||
| 36 | case 'customer.subscription.updated': |
||
| 37 | $user = User::where('stripe_id', $data['data']['object']['customer'])->first(); |
||
| 38 | if ($user) { |
||
| 39 | $plan_nickname = $data['data']['object']['plan']['nickname']; |
||
| 40 | $roles = Role::where('name', strtolower((string) $plan_nickname))->first(); |
||
| 41 | if ($roles) { |
||
| 42 | $user->role_id = $roles->id; |
||
| 43 | $user->save(); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | break; |
||
| 47 | |||
| 48 | case 'invoice.payment_succeeded': |
||
| 49 | $user = User::where('stripe_id', $data['data']['object']['customer'])->first(); |
||
| 50 | if ($user) { |
||
| 51 | $plan_nickname = $data['data']['object']['lines']['data'][0]['plan']['nickname']; |
||
| 52 | $roles = Role::where('name', strtolower((string) $plan_nickname))->first(); |
||
| 53 | if ($roles) { |
||
| 54 | $user->role_id = $roles->id; |
||
| 55 | $user->save(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | break; |
||
| 59 | case 'invoice.payment_failed': |
||
| 60 | $user = User::where('stripe_id', $data['data']['object']['customer'])->first(); |
||
| 61 | if ($user) { |
||
| 62 | $roles = Role::where('name', strtolower('free'))->first(); |
||
| 63 | if ($roles) { |
||
| 64 | $user->role_id = $roles->id; |
||
| 65 | $user->save(); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | break; |
||
| 69 | } |
||
| 70 | } else { |
||
| 71 | echo 'User not found!'; |
||
| 72 | } |
||
| 73 | |||
| 74 | return true; |
||
| 75 | } |
||
| 77 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.