GetCurrentSubscription::__invoke()   B
last analyzed

Complexity

Conditions 6
Paths 13

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 29
c 1
b 0
f 0
nc 13
nop 1
dl 0
loc 44
rs 8.8337
1
<?php
2
3
namespace App\Http\Controllers\Stripe;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
8
class GetCurrentSubscription extends Controller
9
{
10
    /**
11
     * Handle the incoming request.
12
     *
13
     * @return array
14
     */
15
    public function __invoke(Request $request)
16
    {
17
        $user = auth()->user();
18
        $data = [];
19
        $data['has_payment_method'] = $user->hasDefaultPaymentMethod();
20
        if ($user->subscribed('default')) {
21
            $subscription = $user->subscription();
22
23
            $data['subscribed'] = $subscription->stripe_status != 'canceled';
24
25
            $data['plan_id'] = $subscription->stripe_price;
26
            $data['trial_end'] = null;
27
28
            try {
29
                \Stripe\Stripe::setApiKey(\Config::get('services.stripe.secret'));
30
                $stripeSub = \Stripe\Subscription::retrieve($subscription->stripe_id);
31
32
                $data['trial_end'] = date('F j, Y', $stripeSub->trial_end);
33
34
                $finalPrice = $price = $stripeSub->plan->amount;
35
                $discountAmount = 0;
36
37
                try {
38
                    $coupon = $stripeSub->discount->coupon;
39
                    // check the type of coupon - it can be 'fixed_amount' or 'percent_off'
40
                    if ($coupon->amount_off) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $coupon->amount_off of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
41
                        $discountAmount = $coupon->amount_off;
42
                        $finalPrice = $price - $discountAmount;
43
                    } elseif ($coupon->percent_off) {
44
                        $discountAmount = ($price * $coupon->percent_off / 100);
45
                        $finalPrice = $price - $discountAmount;
46
                    }
47
                } catch (\Exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Unused Code introduced by
catch (\Exception) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
                }
49
50
                $data['final_price'] = $finalPrice;
51
                $data['discount_amount'] = $discountAmount;
52
            } catch (Exception) {
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Stripe\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
53
            }
54
        } else {
55
            $data['subscribed'] = false;
56
        }
57
58
        return $data;
59
    }
60
}
61