Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

RoleCalculator::calculateRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Coyote\Services\Guide;
4
5
use Coyote\Guide;
6
7
class RoleCalculator
8
{
9
    public function __construct(private Guide $guide)
10
    {
11
    }
12
13
    public function setRole(int $userId, string $role): void
14
    {
15
        $currentRole = $this->guide->roles()->forUser($userId)->firstOrNew();
16
17
        $currentRole->role = $role;
18
        $currentRole->user_id = $userId;
19
        $currentRole->save();
20
21
        $this->guide->role = $this->calculateRole();
22
    }
23
24
    private function calculateRole(): string
25
    {
26
        $roles = array_count_values($this->guide->roles()->get(['role'])->pluck('role')->toArray());
27
        arsort($roles);
28
29
        return key($roles);
0 ignored issues
show
Bug Best Practice introduced by
The expression return key($roles) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
30
    }
31
}
32