laravel-liberu /
genealogy
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Services; |
||
| 4 | |||
| 5 | use App\Models\User; |
||
| 6 | use Carbon\Carbon; |
||
| 7 | use LaravelEnso\Helpers\Services\Decimals; |
||
| 8 | |||
| 9 | class ProfileBuilder |
||
| 10 | { |
||
| 11 | private const LoginRating = 80; |
||
| 12 | |||
| 13 | private const ActionRating = 20; |
||
| 14 | |||
| 15 | public function __construct(private readonly User $user) |
||
| 16 | { |
||
| 17 | } |
||
| 18 | |||
| 19 | public function set(): void |
||
| 20 | { |
||
| 21 | $this->user->load([ |
||
| 22 | 'person:id,name,appellative,birthday,phone', |
||
| 23 | 'group:id,name', 'role:id,name', 'avatar:id,user_id', |
||
| 24 | ]); |
||
| 25 | |||
| 26 | $this->build(); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function build(): void |
||
| 30 | { |
||
| 31 | $this->user->loginCount = $this->user->logins()->count(); |
||
|
0 ignored issues
–
show
|
|||
| 32 | $this->user->actionLogCount = $this->user->actionLogs()->count(); |
||
|
0 ignored issues
–
show
|
|||
| 33 | $this->user->daysSinceMember = max(Carbon::parse($this->user->created_at)->diffInDays(), 1); |
||
|
0 ignored issues
–
show
|
|||
| 34 | $this->user->rating = $this->rating(); |
||
|
0 ignored issues
–
show
|
|||
| 35 | } |
||
| 36 | |||
| 37 | private function rating(): int |
||
| 38 | { |
||
| 39 | $loginRatio = Decimals::div($this->user->loginCount, $this->user->daysSinceMember); |
||
|
0 ignored issues
–
show
|
|||
| 40 | $loginRating = Decimals::mul(self::LoginRating, $loginRatio); |
||
| 41 | $actionRatio = Decimals::div($this->user->actionLogCount, $this->user->daysSinceMember); |
||
|
0 ignored issues
–
show
|
|||
| 42 | $actionRating = Decimals::mul(self::ActionRating, $actionRatio); |
||
| 43 | $total = Decimals::add($loginRating, $actionRating); |
||
| 44 | |||
| 45 | return (int) Decimals::div($total, 100); |
||
| 46 | } |
||
| 47 | } |
||
| 48 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.