Completed
Push — master ( fbde13...e39b89 )
by Curtis
53s queued 16s
created

Subscribe::__invoke()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 25
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 40
rs 8.8977
1
<?php
2
3
namespace App\Http\Controllers\Stripe;
4
5
use App\Http\Controllers\Controller;
6
use App\Notifications\SubscribeSuccessfully;
7
use Illuminate\Http\Request;
8
9
class Subscribe extends Controller
10
{
11
    /**
12
     * Handle the incoming request.
13
     *
14
     * @return \Illuminate\Http\Response
15
     */
16
    public function __invoke(Request $request)
17
    {
18
        $user = auth()->user();
19
        \Stripe\Stripe::setApiKey(config('cashier.secret'));
20
21
        try {
22
            $user->createAsStripeCustomer();
0 ignored issues
show
Bug introduced by
The method createAsStripeCustomer() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
            $user->/** @scrutinizer ignore-call */ 
23
                   createAsStripeCustomer();
Loading history...
23
        } catch(\Exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
24
        }
25
26
        $plan_id = $request->input('plan_id');
27
28
        if ($request->has('payment_method')) {
29
            $paymentMethod = $request->payment_method;
30
31
            $subscription = $user->newSubscription('default', $plan_id)
0 ignored issues
show
Bug introduced by
The method newSubscription() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            $subscription = $user->/** @scrutinizer ignore-call */ newSubscription('default', $plan_id)
Loading history...
32
                                 ->trialDays(14);
33
34
            if ($couponId = $request->input('coupon_id')) {
35
                $subscription->withCoupon($couponId);
36
            }
37
38
            $subscription->create($paymentMethod);
39
40
            $user->notify(new SubscribeSuccessfully($plan_id));
0 ignored issues
show
Bug introduced by
The method notify() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            $user->/** @scrutinizer ignore-call */ 
41
                   notify(new SubscribeSuccessfully($plan_id));
Loading history...
41
        } else {
42
            if ($user->subscribed('default')) {
0 ignored issues
show
Bug introduced by
The method subscribed() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            if ($user->/** @scrutinizer ignore-call */ subscribed('default')) {
Loading history...
43
                $subscription = $user->subscription();
0 ignored issues
show
Bug introduced by
The method subscription() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
                /** @scrutinizer ignore-call */ 
44
                $subscription = $user->subscription();
Loading history...
44
                if ($subscription->stripe_status == 'canceled') {
45
                    $user->newSubscription('default', $plan_id)->create();
46
                    $user->notify(new SubscribeSuccessfully($plan_id));
47
                } else {
48
                    $user->subscription('default')->swap($plan_id);
49
                }
50
            } else {
51
                $user->subscription('default')->swap($plan_id);
52
            }
53
        }
54
55
        return ['success' => true];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('success' => true) returns the type array<string,true> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
56
    }
57
}
58