Conditions | 9 |
Paths | 37 |
Total Lines | 36 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Tests | 24 |
CRAP Score | 9 |
Changes | 0 |
1 | <?php |
||
20 | 17 | public static function canUpdate(?User $currentUser, string $oldRole, string $newRole): bool |
|
21 | { |
||
22 | 17 | if ($newRole === $oldRole) { |
|
23 | 6 | return true; |
|
24 | } |
||
25 | |||
26 | 14 | $currentRole = $currentUser ? $currentUser->getRole() : User::ROLE_ANONYMOUS; |
|
27 | 14 | $orderedRoles = [ |
|
28 | 14 | User::ROLE_ANONYMOUS, |
|
29 | 14 | User::ROLE_STUDENT, |
|
30 | 14 | User::ROLE_JUNIOR, |
|
31 | 14 | User::ROLE_SENIOR, |
|
32 | 14 | User::ROLE_MAJOR, |
|
33 | 14 | User::ROLE_ADMINISTRATOR, |
|
34 | 14 | ]; |
|
35 | |||
36 | 14 | $newFound = false; |
|
37 | 14 | $oldFound = false; |
|
38 | 14 | foreach ($orderedRoles as $r) { |
|
39 | 14 | if ($r === $oldRole) { |
|
40 | 11 | $oldFound = true; |
|
41 | } |
||
42 | 14 | if ($r === $newRole) { |
|
43 | 10 | $newFound = true; |
|
44 | } |
||
45 | |||
46 | 14 | if ($r === $currentRole) { |
|
47 | 14 | break; |
|
48 | } |
||
49 | } |
||
50 | |||
51 | 14 | if (!$newFound || !$oldFound) { |
|
52 | 6 | return false; |
|
53 | } |
||
54 | |||
55 | 9 | return true; |
|
56 | } |
||
58 |