Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| PlanController | |
100.00% |
8 / 8 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| current | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v2; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Http\Models\Plans; |
| 7 | use App\Http\Requests\v2\Plan\UpdatePlanRequest; |
| 8 | use App\Http\Resources\v2\CurrentSubscriptionResource; |
| 9 | use App\Http\Resources\v2\PlanResource; |
| 10 | use App\Http\Services\CurrentSubscriptionService; |
| 11 | use Illuminate\Http\JsonResponse; |
| 12 | use Illuminate\Http\Request; |
| 13 | |
| 14 | /** |
| 15 | * Controller for plan-related operations. |
| 16 | * |
| 17 | * Handles retrieval and management of subscription plans. |
| 18 | */ |
| 19 | class PlanController extends Controller |
| 20 | { |
| 21 | public function __construct( |
| 22 | private CurrentSubscriptionService $currentSubscriptionService |
| 23 | ) {} |
| 24 | |
| 25 | /** |
| 26 | * Get all available plans. |
| 27 | * |
| 28 | * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection |
| 29 | */ |
| 30 | public function index() |
| 31 | { |
| 32 | $plans = Plans::where('pricing_version', null)->get(); |
| 33 | |
| 34 | return PlanResource::collection($plans); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get the current user's subscription with full details. |
| 39 | * |
| 40 | * Returns the user's current subscription plan, statistics, referral info, |
| 41 | * trial status, and user details. Data is cached for performance. |
| 42 | * |
| 43 | * |
| 44 | * @response 200 { |
| 45 | * "result": { |
| 46 | * "title": "Sales Pro", |
| 47 | * "identifier": "sales-pro-monthly", |
| 48 | * "features": {...}, |
| 49 | * "currency": "usd", |
| 50 | * "interval": "month", |
| 51 | * "unit_amount": 9900, |
| 52 | * "flycuts_features": [], |
| 53 | * "statistics": {...}, |
| 54 | * "referrals": null, |
| 55 | * "referral_key": "abc123", |
| 56 | * "on_trial": false, |
| 57 | * "plan_status": "active", |
| 58 | * "ends_at": null, |
| 59 | * "used_trial": false, |
| 60 | * "already_used_pro_trial": true, |
| 61 | * "user_details": {...} |
| 62 | * } |
| 63 | * } |
| 64 | */ |
| 65 | public function current(Request $request): CurrentSubscriptionResource |
| 66 | { |
| 67 | $user = $request->user(); |
| 68 | $subscriptionData = $this->currentSubscriptionService->getCurrentSubscription($user); |
| 69 | |
| 70 | return new CurrentSubscriptionResource($subscriptionData); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Update a plan. |
| 75 | */ |
| 76 | public function update(UpdatePlanRequest $request, Plans $plan): JsonResponse |
| 77 | { |
| 78 | $plan->update($request->validated()); |
| 79 | |
| 80 | return response()->json(['data' => $plan]); |
| 81 | } |
| 82 | } |