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