Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CompanyExportResource | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
toArray | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
findClosestDate | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getActiveLicense | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatDate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Http\Resources; |
4 | |
5 | use Illuminate\Http\Request; |
6 | use Illuminate\Support\Carbon; |
7 | use Illuminate\Http\Resources\Json\JsonResource; |
8 | |
9 | class CompanyExportResource 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 | $lastModifiedDate = $this->findClosestDate($license->updated_at, $this->updated_at); |
20 | $invited = $this->invitedUsers; |
21 | $usersCount = $this->users()->whereNotIn('email', $invited->pluck("email"))->count() + $invited->count(); |
22 | |
23 | return [ |
24 | 'name' => $this->name, |
25 | 'users' => $usersCount, |
26 | 'contract_term' => $license->term_of_contract, |
27 | 'contract_start_date' => $this->formatDate($license->contract_start_date), |
28 | 'contract_end_date' => $this->formatDate($license->contract_end_date), |
29 | 'auto_renewal' => $license->auto_renew_license === true ? "Yes" : "No", |
30 | 'payment_options' => $license->auto_renew_license === true ? "Auto-Pay" : "Invoice", |
31 | 'account_status' => $license->status, |
32 | 'last_modified_on' => $this->formatDate($lastModifiedDate), |
33 | 'status_date' => $this->formatDate($license->updated_at), |
34 | 'created_at' => $this->created_at, |
35 | ]; |
36 | } |
37 | |
38 | private function findClosestDate($date1, $date2) { |
39 | $now = now(); |
40 | |
41 | $diff1 = $now->diffInSeconds($date1); |
42 | $diff2 = $now->diffInSeconds($date2); |
43 | |
44 | return $diff1 < $diff2 ? $date1 : $date2; |
45 | } |
46 | |
47 | private function getActiveLicense($company) |
48 | { |
49 | return $company->licenses->first() ?? $company->licenses()->latest()->first(); |
50 | } |
51 | |
52 | private function formatDate($date) |
53 | { |
54 | return Carbon::parse($date)->format('m/d/y'); |
55 | } |
56 | } |