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