Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CompanyResource | |
0.00% |
0 / 60 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
| toArray | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 | |||
| findClosestDate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getLicenseCount | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| processGroups | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| getAllUsers | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 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\Support\Str; |
| 6 | use Illuminate\Http\Request; |
| 7 | use Illuminate\Support\Carbon; |
| 8 | use Illuminate\Http\Resources\Json\JsonResource; |
| 9 | |
| 10 | class CompanyResource 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 | $lastModifiedDate = $this->findClosestDate($license?->updated_at, $this->updated_at); |
| 21 | |
| 22 | $statusDate = match (true) { |
| 23 | !is_null($this->deleted_at) => $this->deleted_at->toFormattedDateString(), |
| 24 | !is_null($this->created_at) => $this->created_at->toFormattedDateString(), |
| 25 | default => null, |
| 26 | }; |
| 27 | |
| 28 | $licenseCount = $this->getLicenseCount($license); |
| 29 | |
| 30 | return [ |
| 31 | 'id' => $this->id, |
| 32 | 'name' => $this->name, |
| 33 | 'licenses' => $licenseCount, |
| 34 | 'contract_term' => $license?->term_of_contract, |
| 35 | 'contract_start_date' => $license ? $this->formatDate($license->contract_start_date) : null, |
| 36 | 'contract_end_date' => $license ? $this->formatDate($license->contract_end_date) : null, |
| 37 | 'auto_renewal' => $license?->auto_renew_license === true ? "Yes" : "No", |
| 38 | 'payment_options' => $license?->auto_renew_license === true ? "Auto-Pay" : "Invoice", |
| 39 | 'account_status' => $license?->status ?? 'Inactive', |
| 40 | 'last_modified_on' => $this->formatDate($lastModifiedDate), |
| 41 | 'status_date' => $statusDate, |
| 42 | 'created_at' => $this->created_at, |
| 43 | 'slug' => Str::slug($this->name), |
| 44 | 'groups' => $this->processGroups($this->groupsAndSubgroups) |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | private function findClosestDate($date1, $date2) { |
| 49 | $now = now(); |
| 50 | |
| 51 | if ($date1 === null) { |
| 52 | return $date2; |
| 53 | } |
| 54 | |
| 55 | $diff1 = $now->diffInSeconds($date1); |
| 56 | $diff2 = $now->diffInSeconds($date2); |
| 57 | |
| 58 | return $diff1 < $diff2 ? $date1 : $date2; |
| 59 | } |
| 60 | |
| 61 | private function getLicenseCount($license) { |
| 62 | if (is_null($license)) { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | $licenseCounts = array_filter($license->toArray(), function ($key) { |
| 67 | return str_ends_with($key, '_license_count'); |
| 68 | }, ARRAY_FILTER_USE_KEY); |
| 69 | |
| 70 | return array_sum($licenseCounts); |
| 71 | } |
| 72 | |
| 73 | private function processGroups($groups) |
| 74 | { |
| 75 | if(empty($groups)){ |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | return $groups->map(function($group) { |
| 80 | $allGroupUsers = $this->getAllUsers($group); |
| 81 | |
| 82 | return [ |
| 83 | 'name' => $group->name, |
| 84 | 'users' => $allGroupUsers->count(), |
| 85 | 'subgroups' => filled($group->subgroups) ? $group->subgroups->map(function($subgroup) { |
| 86 | return [ |
| 87 | 'name' => $subgroup->name, |
| 88 | 'users' => $subgroup->users()->count(), |
| 89 | ]; |
| 90 | }) : null |
| 91 | ]; |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | private function getAllUsers($group) |
| 96 | { |
| 97 | $users = $group->users; |
| 98 | $users = $users->merge($group->groupInvitedUsers); |
| 99 | $users = $users->merge($group->subgroupInvitedUsers); |
| 100 | |
| 101 | if(filled($group->subgroups)){ |
| 102 | foreach ($group->subgroups as $subgroup) { |
| 103 | $users = $users->merge($this->getAllUsers($subgroup)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return $users; |
| 108 | } |
| 109 | |
| 110 | private function getActiveLicense($company) |
| 111 | { |
| 112 | return $company->licenses->first() ?? $company->licenses()->latest()->first(); |
| 113 | } |
| 114 | |
| 115 | private function formatDate($date) |
| 116 | { |
| 117 | return Carbon::parse($date)->format('m/d/y'); |
| 118 | } |
| 119 | } |