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