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