Webhook::__invoke()   C
last analyzed

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
5
use App\Http\Controllers\Controller;
6
use App\Models\User;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Log;
9
use LaravelEnso\Roles\Models\Role;
10
use Stripe;
11
12
class Webhook extends Controller
13
{
14
    /**
15
     * Handle the incoming request.
16
     *
17
     * @return \Illuminate\Http\Response
18
     */
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) {
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...
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;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property id does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
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;
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...
75
    }
76
}
77