Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
58.33% |
14 / 24 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| PlanHubspotConfigService | |
58.33% |
14 / 24 |
|
62.50% |
5 / 8 |
14.86 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getByPlanId | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| delete | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| upsert | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getPropertyMappings | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| invalidateCache | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Services; |
| 4 | |
| 5 | use App\Http\Models\PlanHubspotConfig; |
| 6 | use App\Http\Repositories\interfaces\IPlanHubspotConfigRepository; |
| 7 | use Illuminate\Support\Facades\Cache; |
| 8 | |
| 9 | /** |
| 10 | * Service for plan HubSpot config business logic. |
| 11 | * |
| 12 | * This service handles all business logic related to plan HubSpot configurations, |
| 13 | * coordinating with the repository for data access. |
| 14 | */ |
| 15 | class PlanHubspotConfigService |
| 16 | { |
| 17 | /** |
| 18 | * Cache key prefix for HubSpot configs. |
| 19 | */ |
| 20 | private const CACHE_KEY_PREFIX = 'plan_hubspot_config:'; |
| 21 | |
| 22 | /** |
| 23 | * Cache TTL in seconds (1 hour). |
| 24 | */ |
| 25 | private const CACHE_TTL = 3600; |
| 26 | |
| 27 | public function __construct( |
| 28 | private IPlanHubspotConfigRepository $hubspotConfigRepository |
| 29 | ) {} |
| 30 | |
| 31 | /** |
| 32 | * Get HubSpot config for a plan. |
| 33 | * |
| 34 | * @param string $planId The plan ID |
| 35 | * @return PlanHubspotConfig|null The config or null if not found |
| 36 | */ |
| 37 | public function getByPlanId(string $planId): ?PlanHubspotConfig |
| 38 | { |
| 39 | return Cache::remember( |
| 40 | self::CACHE_KEY_PREFIX.$planId, |
| 41 | self::CACHE_TTL, |
| 42 | fn () => $this->hubspotConfigRepository->findByPlanId($planId) |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Create a new HubSpot config for a plan. |
| 48 | * |
| 49 | * @param string $planId The plan ID |
| 50 | * @param array{ |
| 51 | * name?: string|null, |
| 52 | * last_product?: string|null, |
| 53 | * cancel_subscription_date?: string|null, |
| 54 | * payment_status?: string|null, |
| 55 | * subscription_annual_recurring_revenue?: string|null, |
| 56 | * subscription_churn_date?: string|null, |
| 57 | * subscription_expiration_date?: string|null, |
| 58 | * subscription_frequency?: string|null, |
| 59 | * subscription_monthly_recurring_revenue?: string|null, |
| 60 | * subscription_plan_type?: string|null, |
| 61 | * subscription_start_date?: string|null, |
| 62 | * subscription_status?: string|null, |
| 63 | * subscription_status_updated_on?: string|null, |
| 64 | * user_type?: string|null |
| 65 | * } $data The config data |
| 66 | * @return PlanHubspotConfig The created config |
| 67 | */ |
| 68 | public function create(string $planId, array $data): PlanHubspotConfig |
| 69 | { |
| 70 | $config = $this->hubspotConfigRepository->create($planId, $data); |
| 71 | |
| 72 | $this->invalidateCache($planId); |
| 73 | |
| 74 | return $config; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Update an existing HubSpot config. |
| 79 | * |
| 80 | * @param PlanHubspotConfig $config The config to update |
| 81 | * @param array{ |
| 82 | * name?: string|null, |
| 83 | * last_product?: string|null, |
| 84 | * cancel_subscription_date?: string|null, |
| 85 | * payment_status?: string|null, |
| 86 | * subscription_annual_recurring_revenue?: string|null, |
| 87 | * subscription_churn_date?: string|null, |
| 88 | * subscription_expiration_date?: string|null, |
| 89 | * subscription_frequency?: string|null, |
| 90 | * subscription_monthly_recurring_revenue?: string|null, |
| 91 | * subscription_plan_type?: string|null, |
| 92 | * subscription_start_date?: string|null, |
| 93 | * subscription_status?: string|null, |
| 94 | * subscription_status_updated_on?: string|null, |
| 95 | * user_type?: string|null |
| 96 | * } $data The update data |
| 97 | * @return PlanHubspotConfig The updated config |
| 98 | */ |
| 99 | public function update(PlanHubspotConfig $config, array $data): PlanHubspotConfig |
| 100 | { |
| 101 | $updated = $this->hubspotConfigRepository->update($config, $data); |
| 102 | |
| 103 | $this->invalidateCache($config->plan_id); |
| 104 | |
| 105 | return $updated; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Delete a HubSpot config. |
| 110 | * |
| 111 | * @param PlanHubspotConfig $config The config to delete |
| 112 | * @return bool True if deleted successfully |
| 113 | */ |
| 114 | public function delete(PlanHubspotConfig $config): bool |
| 115 | { |
| 116 | $planId = $config->plan_id; |
| 117 | $result = $this->hubspotConfigRepository->delete($config); |
| 118 | |
| 119 | $this->invalidateCache($planId); |
| 120 | |
| 121 | return $result; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Create or update a HubSpot config for a plan. |
| 126 | * |
| 127 | * @param string $planId The plan ID |
| 128 | * @param array{ |
| 129 | * name?: string|null, |
| 130 | * last_product?: string|null, |
| 131 | * cancel_subscription_date?: string|null, |
| 132 | * payment_status?: string|null, |
| 133 | * subscription_annual_recurring_revenue?: string|null, |
| 134 | * subscription_churn_date?: string|null, |
| 135 | * subscription_expiration_date?: string|null, |
| 136 | * subscription_frequency?: string|null, |
| 137 | * subscription_monthly_recurring_revenue?: string|null, |
| 138 | * subscription_plan_type?: string|null, |
| 139 | * subscription_start_date?: string|null, |
| 140 | * subscription_status?: string|null, |
| 141 | * subscription_status_updated_on?: string|null, |
| 142 | * user_type?: string|null |
| 143 | * } $data The config data |
| 144 | * @return PlanHubspotConfig The created or updated config |
| 145 | */ |
| 146 | public function upsert(string $planId, array $data): PlanHubspotConfig |
| 147 | { |
| 148 | $config = $this->hubspotConfigRepository->upsert($planId, $data); |
| 149 | |
| 150 | $this->invalidateCache($planId); |
| 151 | |
| 152 | return $config; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the HubSpot property mappings for a plan. |
| 157 | * |
| 158 | * Returns all non-null HubSpot property names for use in CRM sync operations. |
| 159 | * |
| 160 | * @param string $planId The plan ID |
| 161 | * @return array<string, string> Property mappings (only non-null values) |
| 162 | */ |
| 163 | public function getPropertyMappings(string $planId): array |
| 164 | { |
| 165 | $config = $this->getByPlanId($planId); |
| 166 | |
| 167 | if (! $config) { |
| 168 | return []; |
| 169 | } |
| 170 | |
| 171 | return $config->getPropertyMappings(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Invalidate the cache for a plan's HubSpot config. |
| 176 | * |
| 177 | * @param string $planId The plan ID |
| 178 | */ |
| 179 | public function invalidateCache(string $planId): void |
| 180 | { |
| 181 | Cache::forget(self::CACHE_KEY_PREFIX.$planId); |
| 182 | } |
| 183 | } |