Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.30% covered (success)
97.30%
36 / 37
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlanResource
97.30% covered (success)
97.30%
36 / 37
50.00% covered (danger)
50.00%
1 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 toArray
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 buildFeaturesMap
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
5.03
1<?php
2
3namespace App\Http\Resources\v2;
4
5use Illuminate\Http\Resources\Json\JsonResource;
6
7/**
8 * Resource for transforming Plans models for public API responses.
9 *
10 * This resource excludes legacy fields and returns plan data with
11 * features loaded from the plan_features relationship.
12 *
13 * @property string $_id The plan ID
14 * @property string $title The plan title
15 * @property string $identifier The plan identifier
16 * @property string|null $stripe_id The Stripe price ID
17 * @property string|null $stripe_product_id The Stripe product ID
18 * @property string|null $hubspot_name HubSpot plan name
19 * @property string $currency Plan currency
20 * @property string $interval Billing interval
21 * @property int|string $unit_amount Price amount
22 * @property \Carbon\Carbon|null $created_at When the plan was created
23 * @property \Carbon\Carbon|null $updated_at When the plan was last updated
24 */
25class PlanResource extends JsonResource
26{
27    /**
28     * Transform the resource into an array.
29     *
30     * Excludes legacy fields (features, flycuts_features, legacy_features)
31     * from the response. Use plan_features relationship for feature data.
32     *
33     * @return array<string, mixed>
34     */
35    public function toArray($request)
36    {
37        return [
38            'id' => (string) $this->_id,
39            'title' => $this->title,
40            'identifier' => $this->identifier,
41            'stripe_id' => $this->stripe_id,
42            'stripe_product_id' => $this->stripe_product_id,
43            'hubspot_name' => $this->hubspot_name,
44            'currency' => $this->currency,
45            'interval' => $this->interval,
46            'unit_amount' => $this->unit_amount,
47            'has_fly_learning' => (bool) $this->has_fly_learning,
48            'user_custom_prompts' => (int) ($this->user_custom_prompts ?? 0),
49            'user_persona_available' => (int) ($this->user_persona_available ?? 0),
50            'can_disable_flygrammar' => (bool) $this->can_disable_flygrammar,
51            'flycut_deployment' => (int) ($this->flycut_deployment ?? 0),
52            'flygrammar_actions' => (int) ($this->flygrammar_actions ?? 0),
53            'prompts_per_day' => (int) ($this->prompts_per_day ?? 0),
54            'regenerate_count' => (int) ($this->regenerate_count ?? 0),
55            'plan_features' => $this->whenLoaded('planFeatures', function () {
56                return PlanFeatureResource::collection($this->planFeatures);
57            }),
58            'features_map' => $this->when($this->relationLoaded('planFeatures'), function () {
59                return $this->buildFeaturesMap();
60            }),
61            'created_at' => $this->created_at?->timestamp,
62            'updated_at' => $this->updated_at?->timestamp,
63        ];
64    }
65
66    /**
67     * Build a map of feature keys to their values for easy access.
68     *
69     * @return array<string, mixed>
70     */
71    private function buildFeaturesMap(): array
72    {
73        if (! $this->planFeatures) {
74            return [];
75        }
76
77        $map = [];
78
79        foreach ($this->planFeatures as $planFeature) {
80            if ($planFeature->feature && $planFeature->is_enabled) {
81                $map[$planFeature->feature->key] = [
82                    'value' => $planFeature->value ?? $planFeature->feature->default_value,
83                    'description' => $planFeature->feature->description,
84                ];
85            }
86        }
87
88        return $map;
89    }
90}