Passed
Push — master ( 51edae...d29a9b )
by Curtis
11:52 queued 05:54
created

ProfileBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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();
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...
35
        $this->user->person->gender = $this->user->person->gender();
0 ignored issues
show
Bug introduced by
The property gender does not seem to exist on App\Person. 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...
36
        $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...
37
        $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...
38
        $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...
39
    }
40
41
    private function rating(): int
42
    {
43
        $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...
44
        $loginRating = Decimals::mul(self::LoginRating, $loginRatio);
45
        $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...
46
        $actionRating = Decimals::mul(self::ActionRating, $actionRatio);
47
        $total = Decimals::add($loginRating, $actionRating);
48
49
        return (int) Decimals::div($total, 100);
50
    }
51
}
52