| Conditions | 2 |
| Paths | 2 |
| Total Lines | 53 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 43 | public function onSwitchUser($event) |
||
| 44 | { |
||
| 45 | $app = $this->app; |
||
| 46 | |||
| 47 | $user = $app['security']->getToken()->getUser(); |
||
| 48 | $targetUser = $event->getTargetUser(); |
||
| 49 | |||
| 50 | if ($app['security']->isGranted('ROLE_PREVIOUS_ADMIN')) { |
||
| 51 | $targetUser = $app['orm.em'] |
||
| 52 | ->find( |
||
| 53 | 'Application\Entity\UserEntity', |
||
| 54 | $targetUser->getId() |
||
| 55 | ) |
||
| 56 | ; |
||
| 57 | $userActionEntity = new UserActionEntity(); |
||
| 58 | $userActionEntity |
||
| 59 | ->setUser($targetUser) |
||
| 60 | ->setKey('user.switch.back') |
||
| 61 | ->setType('event') |
||
| 62 | ->setMessage( |
||
| 63 | 'User has switched back to own user (from user with ID "'.$user->getId().'")!' |
||
| 64 | ) |
||
| 65 | ->setData(array( |
||
| 66 | 'user_id' => $targetUser->getId(), |
||
| 67 | 'from_user_id' => $user->getId(), |
||
| 68 | )) |
||
| 69 | ->setIp($app['request']->getClientIp()) |
||
| 70 | ->setUserAgent($app['request']->headers->get('User-Agent')) |
||
| 71 | ; |
||
| 72 | |||
| 73 | $app['orm.em']->persist($userActionEntity); |
||
| 74 | $app['orm.em']->flush(); |
||
| 75 | } else { |
||
| 76 | $userActionEntity = new UserActionEntity(); |
||
| 77 | $userActionEntity |
||
| 78 | ->setUser($user) |
||
| 79 | ->setKey('user.switch') |
||
| 80 | ->setType('event') |
||
| 81 | ->setMessage( |
||
| 82 | 'User has switched to user with ID "'.$targetUser->getId().'"!' |
||
| 83 | ) |
||
| 84 | ->setData(array( |
||
| 85 | 'user_id' => $user->getId(), |
||
| 86 | 'to_user_id' => $targetUser->getId(), |
||
| 87 | )) |
||
| 88 | ->setIp($app['request']->getClientIp()) |
||
| 89 | ->setUserAgent($app['request']->headers->get('User-Agent')) |
||
| 90 | ; |
||
| 91 | |||
| 92 | $app['orm.em']->persist($userActionEntity); |
||
| 93 | $app['orm.em']->flush(); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 105 |