| Conditions | 11 |
| Paths | 7 |
| Total Lines | 39 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 61 | protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
||
| 62 | { |
||
| 63 | if (!$subject instanceof Organization) { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | // los administradores globales siempre tienen permiso |
||
| 68 | if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) { |
||
| 69 | return true; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** @var User $user */ |
||
| 73 | $user = $token->getUser(); |
||
| 74 | |||
| 75 | if (!$user instanceof User) { |
||
| 76 | // si el usuario no ha entrado, denegar |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Si es administrador de la organización, permitir siempre |
||
| 81 | if ($user->getManagedOrganizations()->contains($subject)) { |
||
| 82 | return true; |
||
| 83 | } |
||
| 84 | |||
| 85 | // Si es permiso de acceso, comprobar que pertenece actualmente a la organización |
||
| 86 | if ($attribute === self::ACCESS) { |
||
| 87 | |||
| 88 | $date = new \DateTime(); |
||
| 89 | /** @var Membership $membership */ |
||
| 90 | foreach ($user->getMemberships() as $membership) { |
||
| 91 | if ($membership->getOrganization() == $subject && $membership->getValidFrom() <= $date && ($membership->getValidUntil() === null || $membership->getValidUntil() >= $date)) { |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // denegamos en cualquier otro caso |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | } |
||
| 101 |