ProfileBuilder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A build() 0 6 1
A rating() 0 9 1
A set() 0 8 1
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
Bug introduced by
The property loginCount does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
32
        $this->user->actionLogCount = $this->user->actionLogs()->count();
0 ignored issues
show
Bug introduced by
The property actionLogCount does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
33
        $this->user->daysSinceMember = max(Carbon::parse($this->user->created_at)->diffInDays(), 1);
0 ignored issues
show
Bug introduced by
The property daysSinceMember does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
34
        $this->user->rating = $this->rating();
0 ignored issues
show
Bug introduced by
The property rating does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
35
    }
36
37
    private function rating(): int
38
    {
39
        $loginRatio = Decimals::div($this->user->loginCount, $this->user->daysSinceMember);
0 ignored issues
show
Bug introduced by
The property loginCount does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property daysSinceMember does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
40
        $loginRating = Decimals::mul(self::LoginRating, $loginRatio);
41
        $actionRatio = Decimals::div($this->user->actionLogCount, $this->user->daysSinceMember);
0 ignored issues
show
Bug introduced by
The property actionLogCount does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
42
        $actionRating = Decimals::mul(self::ActionRating, $actionRatio);
43
        $total = Decimals::add($loginRating, $actionRating);
44
45
        return (int) Decimals::div($total, 100);
46
    }
47
}
48