Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CompanyInfo
96.77% covered (success)
96.77%
30 / 31
75.00% covered (warning)
75.00%
3 / 4
6
0.00% covered (danger)
0.00%
0 / 1
 toArray
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
1
 getActiveLicense
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIntervalByLabel
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 formatDate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Resources;
4
5use Illuminate\Http\Request;
6use Illuminate\Http\Resources\Json\JsonResource;
7use Illuminate\Support\Carbon;
8
9/** @mixin \App\Http\Models\Admin\Company */
10class CompanyInfo extends JsonResource
11{
12    /**
13     * Transform the resource into an array.
14     *
15     * @return array<string, mixed>
16     */
17    public function toArray(Request $request): array
18    {
19        $license = $this->getActiveLicense($this);
20
21        return [
22            'id' => $this->id,
23            'company_name' => $this->name,
24            'company_address_line1' => $this->address_line_1,
25            'company_address_line2' => $this->address_line_2,
26            'city' => $this->city,
27            'state' => $this->state,
28            'country' => $this->country,
29            'zip_code' => $this->zip,
30            'term_of_contract' => $this->getIntervalByLabel($license->term_of_contract),
31            'contract_start_date' => $this->formatDate($license->contract_start_date),
32            'contract_end_date' => $this->formatDate($license->contract_end_date),
33            'business_pro_enterprise_plus' => $license->business_pro_enterprise_plus,
34            'auto_renewal' => $license->auto_renew_license,
35            'total_licenses' => max(0, $license->total_licenses),
36            'starter' => max(0, $license->total_starter_license_count),
37            'growth' => max(0, $license->total_growth_license_count),
38            'sales_pro' => max(0, $license->total_sales_pro_license_count),
39            'sales_pro_teams_smb' => max(0, $license->total_sales_pro_teams_license_count),
40            'roleplay_addon_access' => $this->roleplay_addon_access,
41            'company_roleplay_addon_id' => $this->company_roleplay_addon_id,
42        ];
43    }
44
45    private function getActiveLicense($company)
46    {
47        return $company->licenses->first() ?? $company->licenses()->latest()->first();
48    }
49
50    private function getIntervalByLabel($label)
51    {
52        $intervalInMonths = 0;
53
54        // Match patterns for years and months
55        if (preg_match('/(\d+)\s*year(s?)/i', $label, $yearMatches)) {
56            $intervalInMonths += $yearMatches[1] * 12;
57        }
58
59        if (preg_match('/(\d+)\s*month(s?)/i', $label, $monthMatches)) {
60            $intervalInMonths += $monthMatches[1];
61        }
62
63        return $intervalInMonths;
64    }
65
66    private function formatDate($date)
67    {
68        return Carbon::parse($date)->format('m/d/y');
69    }
70}