1 | <?php |
||||
2 | |||||
3 | namespace BristolSU\Module\AssignRoles\Support; |
||||
4 | |||||
5 | use BristolSU\ControlDB\Contracts\Models\Role; |
||||
6 | use BristolSU\ControlDB\Contracts\Models\User; |
||||
7 | use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserRole; |
||||
8 | use BristolSU\Support\Logic\Contracts\LogicRepository; |
||||
9 | use Illuminate\Support\Collection; |
||||
10 | |||||
11 | class LogicUserRoleRepository implements UserRole |
||||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
12 | { |
||||
13 | |||||
14 | /** |
||||
15 | * @var UserRole |
||||
16 | */ |
||||
17 | private $userRoleRepository; |
||||
18 | /** |
||||
19 | * @var \BristolSU\Support\Logic\Contracts\LogicTester |
||||
20 | */ |
||||
21 | private $logicTester; |
||||
22 | |||||
23 | 17 | public function __construct(UserRole $userRoleRepository, \BristolSU\Support\Logic\Contracts\LogicTester $logicTester) |
|||
24 | { |
||||
25 | 17 | $this->userRoleRepository = $userRoleRepository; |
|||
26 | 17 | $this->logicTester = $logicTester; |
|||
27 | 17 | } |
|||
28 | |||||
29 | /** |
||||
30 | * @inheritDoc |
||||
31 | */ |
||||
32 | 8 | public function getUsersThroughRole(Role $role): Collection |
|||
33 | { |
||||
34 | 8 | return $this->userRoleRepository->getUsersThroughRole($role); |
|||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * @inheritDoc |
||||
39 | */ |
||||
40 | 11 | public function getRolesThroughUser(User $user): Collection |
|||
41 | { |
||||
42 | 11 | return $this->filter($this->userRoleRepository->getRolesThroughUser($user)); |
|||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * @inheritDoc |
||||
47 | */ |
||||
48 | 2 | public function addUserToRole(User $user, Role $role): void |
|||
49 | { |
||||
50 | 2 | $this->userRoleRepository->addUserToRole($user, $role); |
|||
51 | 2 | } |
|||
52 | |||||
53 | /** |
||||
54 | * @inheritDoc |
||||
55 | */ |
||||
56 | 1 | public function removeUserFromRole(User $user, Role $role): void |
|||
57 | { |
||||
58 | 1 | $this->userRoleRepository->removeUserFromRole($user, $role); |
|||
59 | 1 | } |
|||
60 | |||||
61 | 11 | private function hasLogicGroup() |
|||
0 ignored issues
–
show
|
|||||
62 | { |
||||
63 | 11 | return $this->logicGroupId() !== null; |
|||
64 | } |
||||
65 | |||||
66 | 11 | private function logicGroupId() |
|||
0 ignored issues
–
show
|
|||||
67 | { |
||||
68 | 11 | $id = settings('logic_group', null); |
|||
69 | 11 | if($id === null) { |
|||
0 ignored issues
–
show
|
|||||
70 | 11 | return null; |
|||
71 | } |
||||
72 | return (int) $id; |
||||
73 | } |
||||
74 | |||||
75 | private function logicGroup() |
||||
0 ignored issues
–
show
|
|||||
76 | { |
||||
77 | return app(LogicRepository::class)->getById( |
||||
78 | $this->logicGroupId() |
||||
0 ignored issues
–
show
It seems like
$this->logicGroupId() can also be of type null ; however, parameter $id of BristolSU\Support\Logic\...icRepository::getById() does only seem to accept integer , 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
Loading history...
|
|||||
79 | ); |
||||
80 | } |
||||
81 | |||||
82 | private function isInLogicGroup(\BristolSU\ControlDB\Contracts\Models\Role $role) |
||||
0 ignored issues
–
show
|
|||||
83 | { |
||||
84 | return $this->logicTester->evaluate($this->logicGroup(), null, $role->group(), $role); |
||||
85 | } |
||||
86 | |||||
87 | 11 | private function filter(Collection $roles) |
|||
0 ignored issues
–
show
|
|||||
88 | { |
||||
89 | 11 | if($this->hasLogicGroup()) { |
|||
90 | return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) { |
||||
91 | return $this->isInLogicGroup($role); |
||||
92 | })->values(); |
||||
93 | } |
||||
94 | 11 | return $roles; |
|||
95 | } |
||||
96 | } |