1 | <?php |
||||
2 | |||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
3 | namespace BristolSU\Module\AssignRoles\Rules; |
||||
4 | |||||
5 | use BristolSU\ControlDB\Contracts\Repositories\Role; |
||||
6 | use BristolSU\Module\AssignRoles\Support\PositionSettingRetrieval; |
||||
7 | use BristolSU\Support\Authentication\Contracts\Authentication; |
||||
8 | use Illuminate\Contracts\Validation\Rule; |
||||
9 | |||||
10 | /** |
||||
11 | * Does the role given have space to assign? |
||||
12 | * |
||||
13 | * This is not the case if the role should only have a single user, and it already has a user |
||||
14 | * |
||||
15 | * @package BristolSU\Module\AssignRoles\Rules |
||||
16 | */ |
||||
0 ignored issues
–
show
|
|||||
17 | class RoleHasSpaceToAssign implements Rule |
||||
18 | { |
||||
19 | |||||
20 | /** |
||||
0 ignored issues
–
show
|
|||||
21 | * @var PositionSettingRetrieval |
||||
22 | */ |
||||
23 | private $positionSettingRetrieval; |
||||
0 ignored issues
–
show
|
|||||
24 | /** |
||||
0 ignored issues
–
show
|
|||||
25 | * @var Authentication |
||||
26 | */ |
||||
27 | private $authentication; |
||||
0 ignored issues
–
show
|
|||||
28 | /** |
||||
0 ignored issues
–
show
|
|||||
29 | * @var Role |
||||
30 | */ |
||||
31 | private $roleRepository; |
||||
0 ignored issues
–
show
|
|||||
32 | |||||
33 | 7 | public function __construct(Authentication $authentication, PositionSettingRetrieval $positionSettingRetrieval, Role $roleRepository) |
|||
0 ignored issues
–
show
|
|||||
34 | { |
||||
35 | 7 | $this->positionSettingRetrieval = $positionSettingRetrieval; |
|||
36 | 7 | $this->authentication = $authentication; |
|||
37 | 7 | $this->roleRepository = $roleRepository; |
|||
38 | 7 | } |
|||
39 | |||||
40 | /** |
||||
0 ignored issues
–
show
|
|||||
41 | * @inheritDoc |
||||
42 | */ |
||||
0 ignored issues
–
show
|
|||||
43 | 7 | public function passes($attribute, $value) |
|||
44 | { |
||||
45 | try { |
||||
46 | 7 | $settings = $this->positionSettingRetrieval->getSettings($this->group()); |
|||
0 ignored issues
–
show
It seems like
$this->group() can also be of type null ; however, parameter $group of BristolSU\Module\AssignR...etrieval::getSettings() does only seem to accept BristolSU\ControlDB\Contracts\Models\Group , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
47 | } catch (\Exception $e) { |
||||
48 | return false; |
||||
49 | } |
||||
50 | 7 | if(in_array($value, $settings['only_one_user'])) { |
|||
0 ignored issues
–
show
|
|||||
51 | 2 | return $this->roleRepository->getById($value)->users()->count() === 0; |
|||
52 | } |
||||
53 | 5 | return true; |
|||
54 | } |
||||
55 | |||||
56 | 7 | protected function group() |
|||
0 ignored issues
–
show
|
|||||
57 | { |
||||
58 | 7 | return $this->authentication->getGroup(); |
|||
59 | } |
||||
60 | |||||
61 | /** |
||||
0 ignored issues
–
show
|
|||||
62 | * @inheritDoc |
||||
63 | */ |
||||
0 ignored issues
–
show
|
|||||
64 | 1 | public function message() |
|||
65 | { |
||||
66 | 1 | return 'A user is already assigned to this role'; |
|||
67 | } |
||||
68 | } |