Passed
Push — master ( 77e2c5...fbde13 )
by Curtis
10:28 queued 01:36
created

Webhook::__invoke()   C

Complexity

Conditions 14
Paths 16

Size

Total Lines 56
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
cc 14
eloc 41
c 6
b 0
f 1
nc 16
nop 1
dl 0
loc 56
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Stripe;
4
use LaravelEnso\Roles\Models\Role;
5
use App\Http\Controllers\Controller;
6
use App\Models\User;
7
use Illuminate\Http\Request;
8
use Stripe;
9
use Illuminate\Support\Facades\Log;
10
11
class Webhook extends Controller
12
{
13
    /**
14
     * Handle the incoming request.
15
     *
16
     * @return \Illuminate\Http\Response
17
     */
18
    public function __invoke(Request $request)
19
    {
20
        Stripe\Stripe::setApiKey(\Config::get('services.stripe.secret'));
21
        $plans = Stripe\Plan::all();
0 ignored issues
show
Unused Code introduced by
The assignment to $plans is dead and can be removed.
Loading history...
22
        $data = request()->all();
23
24
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
25
            switch ($data['type']) {
26
                case "customer.subscription.deleted": 
27
                    $user = User::where('stripe_id', $data['data']['object']['customer'])->first();
28
                    if ($user) {
29
                        $user->role_id = 4;
30
                        $user->save();
31
                    }
32
                    break;
33
                
34
                case "customer.subscription.created":
35
                case "customer.subscription.updated":
36
                    $user = User::where('stripe_id', $data['data']['object']['customer'])->first();
37
                    if ($user) {
38
                        $plan_nickname = $data['data']['object']['plan']['nickname'];
39
                        $roles= Role::where('name', strtolower($plan_nickname))->first();
40
                        if ($roles) {
41
                            $user->role_id = $roles->id;
42
                            $user->save();
43
                        }
44
                    }
45
                    break;
46
                      
47
                case "invoice.payment_succeeded":
48
                    $user = User::where('stripe_id', $data['data']['object']['customer'])->first();
49
                    if ($user) {
50
                        $plan_nickname = $data['data']['object']['lines']['data'][0]['plan']['nickname'];
51
                        $roles= Role::where('name', strtolower($plan_nickname))->first();
52
                        if ($roles) {
53
                            $user->role_id = $roles->id;
54
                            $user->save();
55
                        }
56
                    }
57
                    break;
58
                case "invoice.payment_failed" :
59
                    $user = User::where('stripe_id', $data['data']['object']['customer'])->first();
60
                    if ($user) {
61
                        $roles= Role::where('name', strtolower("free"))->first();
62
                        if ($roles) {
63
                            $user->role_id = $roles->id;
64
                            $user->save();
65
                        }
66
                    }
67
                    break;
68
            }
69
        } else {
70
            echo 'User not found!';
71
        }
72
73
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
74
    }
75
}
76