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

RoleCalculator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 23
rs 10
c 1
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A calculateRole() 0 6 1
A setRole() 0 9 1
A __construct() 0 2 1
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