Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
PlanHubspotConfigRepository
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 findByPlanId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
4 / 4
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
 upsert
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Http\Repositories;
4
5use App\Http\Models\PlanHubspotConfig;
6use App\Http\Repositories\interfaces\IPlanHubspotConfigRepository;
7
8/**
9 * Repository for plan HubSpot config data access operations.
10 *
11 * This repository handles all database queries related to plan HubSpot configs,
12 * keeping data access logic separate from business logic.
13 */
14class PlanHubspotConfigRepository implements IPlanHubspotConfigRepository
15{
16    /**
17     * Find a HubSpot config by plan ID.
18     *
19     * @param  string  $planId  The plan ID
20     * @return PlanHubspotConfig|null The config or null if not found
21     */
22    public function findByPlanId(string $planId): ?PlanHubspotConfig
23    {
24        return PlanHubspotConfig::where('plan_id', $planId)->first();
25    }
26
27    /**
28     * Create a new HubSpot config for a plan.
29     *
30     * @param  string  $planId  The plan ID
31     * @param  array{
32     *     name?: string|null,
33     *     last_product?: string|null,
34     *     cancel_subscription_date?: string|null,
35     *     payment_status?: string|null,
36     *     subscription_annual_recurring_revenue?: string|null,
37     *     subscription_churn_date?: string|null,
38     *     subscription_expiration_date?: string|null,
39     *     subscription_frequency?: string|null,
40     *     subscription_monthly_recurring_revenue?: string|null,
41     *     subscription_plan_type?: string|null,
42     *     subscription_start_date?: string|null,
43     *     subscription_status?: string|null,
44     *     subscription_status_updated_on?: string|null,
45     *     user_type?: string|null
46     * }  $data  The config data
47     * @return PlanHubspotConfig The created config
48     */
49    public function create(string $planId, array $data): PlanHubspotConfig
50    {
51        return PlanHubspotConfig::create([
52            'plan_id' => $planId,
53            ...$data,
54        ]);
55    }
56
57    /**
58     * Update an existing HubSpot config.
59     *
60     * @param  PlanHubspotConfig  $config  The config to update
61     * @param  array{
62     *     name?: string|null,
63     *     last_product?: string|null,
64     *     cancel_subscription_date?: string|null,
65     *     payment_status?: string|null,
66     *     subscription_annual_recurring_revenue?: string|null,
67     *     subscription_churn_date?: string|null,
68     *     subscription_expiration_date?: string|null,
69     *     subscription_frequency?: string|null,
70     *     subscription_monthly_recurring_revenue?: string|null,
71     *     subscription_plan_type?: string|null,
72     *     subscription_start_date?: string|null,
73     *     subscription_status?: string|null,
74     *     subscription_status_updated_on?: string|null,
75     *     user_type?: string|null
76     * }  $data  The update data
77     * @return PlanHubspotConfig The updated config
78     */
79    public function update(PlanHubspotConfig $config, array $data): PlanHubspotConfig
80    {
81        $config->update($data);
82
83        return $config->fresh();
84    }
85
86    /**
87     * Delete a HubSpot config.
88     *
89     * @param  PlanHubspotConfig  $config  The config to delete
90     * @return bool True if deleted successfully
91     */
92    public function delete(PlanHubspotConfig $config): bool
93    {
94        return $config->delete();
95    }
96
97    /**
98     * Create or update a HubSpot config for a plan.
99     *
100     * @param  string  $planId  The plan ID
101     * @param  array{
102     *     name?: string|null,
103     *     last_product?: string|null,
104     *     cancel_subscription_date?: string|null,
105     *     payment_status?: string|null,
106     *     subscription_annual_recurring_revenue?: string|null,
107     *     subscription_churn_date?: string|null,
108     *     subscription_expiration_date?: string|null,
109     *     subscription_frequency?: string|null,
110     *     subscription_monthly_recurring_revenue?: string|null,
111     *     subscription_plan_type?: string|null,
112     *     subscription_start_date?: string|null,
113     *     subscription_status?: string|null,
114     *     subscription_status_updated_on?: string|null,
115     *     user_type?: string|null
116     * }  $data  The config data
117     * @return PlanHubspotConfig The created or updated config
118     */
119    public function upsert(string $planId, array $data): PlanHubspotConfig
120    {
121        $existing = $this->findByPlanId($planId);
122
123        if ($existing) {
124            return $this->update($existing, $data);
125        }
126
127        return $this->create($planId, $data);
128    }
129}