Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.59% covered (warning)
70.59%
12 / 17
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlanRepository
70.59% covered (warning)
70.59%
12 / 17
55.56% covered (warning)
55.56%
5 / 9
12.54
0.00% covered (danger)
0.00%
0 / 1
 getAllPaginated
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getAllActive
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 findById
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findByIdentifier
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findByStripeProductId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWithFeatures
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Repositories;
4
5use App\Http\Models\Plans;
6use App\Http\Repositories\interfaces\IPlanRepository;
7use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8use Illuminate\Support\Collection;
9
10/**
11 * Repository for plan data access operations.
12 *
13 * This repository handles all database queries related to plans,
14 * keeping data access logic separate from business logic.
15 */
16class PlanRepository implements IPlanRepository
17{
18    /**
19     * Get all plans with optional filtering and pagination.
20     *
21     * @param  string|null  $filter  Optional search filter for plan titles
22     * @param  int  $perPage  Number of items per page
23     * @return LengthAwarePaginator<Plans>
24     */
25    public function getAllPaginated(?string $filter = null, int $perPage = 15): LengthAwarePaginator
26    {
27        $query = Plans::query();
28
29        if ($filter) {
30            $query->where(function ($q) use ($filter) {
31                $q->where('title', 'like', "%{$filter}%")
32                    ->orWhere('identifier', 'like', "%{$filter}%");
33            });
34        }
35
36        return $query->orderBy('title')->paginate($perPage);
37    }
38
39    /**
40     * Get all active plans (where pricing_version is null).
41     *
42     * @return Collection<int, Plans> Collection of active plans
43     */
44    public function getAllActive(): Collection
45    {
46        return Plans::orderBy('title')
47            ->get();
48    }
49
50    /**
51     * Find a plan by its ID.
52     *
53     * @param  string  $id  The plan ID
54     * @return Plans|null The plan or null if not found
55     */
56    public function findById(string $id): ?Plans
57    {
58        return Plans::find($id);
59    }
60
61    /**
62     * Find a plan by its identifier.
63     *
64     * @param  string  $identifier  The plan identifier
65     * @return Plans|null The plan or null if not found
66     */
67    public function findByIdentifier(string $identifier): ?Plans
68    {
69        return Plans::where('identifier', $identifier)->first();
70    }
71
72    /**
73     * Find a plan by its Stripe product ID.
74     *
75     * @param  string  $productId  The Stripe product ID
76     * @return Plans|null The plan or null if not found
77     */
78    public function findByStripeProductId(string $productId): ?Plans
79    {
80        return Plans::where('stripe_product_id', $productId)->first();
81    }
82
83    /**
84     * Get a plan with its features and HubSpot config loaded.
85     *
86     * @param  string  $id  The plan ID
87     * @return Plans|null The plan with features or null if not found
88     */
89    public function getWithFeatures(string $id): ?Plans
90    {
91        return Plans::with(['planFeatures.feature', 'hubspotConfig'])->find($id);
92    }
93
94    /**
95     * Create a new plan.
96     *
97     * @param  array<string, mixed>  $data  The plan data
98     * @return Plans The created plan
99     */
100    public function create(array $data): Plans
101    {
102        return Plans::create($data);
103    }
104
105    /**
106     * Update an existing plan.
107     *
108     * @param  Plans  $plan  The plan to update
109     * @param  array<string, mixed>  $data  The update data
110     * @return Plans The updated plan
111     */
112    public function update(Plans $plan, array $data): Plans
113    {
114        $plan->update($data);
115
116        return $plan->fresh();
117    }
118
119    /**
120     * Delete a plan.
121     *
122     * @param  Plans  $plan  The plan to delete
123     * @return bool True if deleted successfully
124     */
125    public function delete(Plans $plan): bool
126    {
127        return $plan->delete();
128    }
129}