Subscribe::__invoke()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 6
eloc 26
c 2
b 0
f 1
nc 10
nop 1
dl 0
loc 36
rs 8.8817
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();
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
            $subscription = $user->newSubscription('default', $plan_id)
31
                                 ->trialDays(14);
32
            if ($couponId = $request->input('coupon_id')) {
33
                $subscription->withCoupon($couponId);
34
            }
35
            $subscription->create($paymentMethod);
36
            $user->notify(new SubscribeSuccessfully($plan_id));
37
        } elseif ($user->subscribed('default')) {
38
            $subscription = $user->subscription();
39
            if ($subscription->stripe_status == 'canceled') {
40
                $user->newSubscription('default', $plan_id)
41
                    ->trialDays(14)
42
                    ->create();
43
                $user->notify(new SubscribeSuccessfully($plan_id));
44
            } else {
45
                $user->subscription('default')->swap($plan_id);
46
            }
47
        } else {
48
            $user->subscription('default')->swap($plan_id);
49
        }
50
51
        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...
52
    }
53
}
54