| Conditions | 6 |
| Paths | 5 |
| Total Lines | 54 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 44 | #[Route( |
||
| 45 | path: '/switch-locale', |
||
| 46 | name: 'ss_switch_locale', |
||
| 47 | requirements: ['return-url' => '.+'], |
||
| 48 | methods: ['POST'] |
||
| 49 | )] |
||
| 50 | public function switchLocale(Request $request): RedirectResponse |
||
| 51 | { |
||
| 52 | $returnUrl = $request->query->get('return-url'); |
||
| 53 | |||
| 54 | // Return URLs generated by us always include a path (ie. at least a forward slash) |
||
| 55 | // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878 |
||
| 56 | $domain = $request->getSchemeAndHttpHost() . '/'; |
||
| 57 | if (!str_starts_with($returnUrl, $domain)) { |
||
| 58 | $this->logger->error(sprintf( |
||
| 59 | 'Identity "%s" used illegal return-url for redirection after changing locale, aborting request', |
||
| 60 | $this->getUser()->getIdentity()->id |
||
| 61 | )); |
||
| 62 | |||
| 63 | throw new BadRequestHttpException('Invalid return-url given'); |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->logger->info('Switching locale...'); |
||
| 67 | |||
| 68 | $identity = $this->getUser()->getIdentity(); |
||
| 69 | if (!$identity) { |
||
| 70 | throw new AccessDeniedHttpException('Cannot switch locales when not authenticated'); |
||
| 71 | } |
||
| 72 | |||
| 73 | $command = new SwitchLocaleCommand(); |
||
| 74 | $command->identityId = $identity->id; |
||
| 75 | |||
| 76 | $form = $this->createForm( |
||
| 77 | SwitchLocaleType::class, |
||
| 78 | $command, |
||
| 79 | ['route' => 'ss_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]] |
||
| 80 | ); |
||
| 81 | $form->handleRequest($request); |
||
| 82 | |||
| 83 | if (!$form->isSubmitted() || !$form->isValid()) { |
||
| 84 | $this->addFlash('error', $this->translator->trans('ss.flash.invalid_switch_locale_form')); |
||
| 85 | $this->logger->error('The switch locale form unexpectedly contained invalid data'); |
||
| 86 | return $this->redirect($returnUrl); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (!$this->identityService->switchLocale($command)) { |
||
| 90 | $this->addFlash('error', $this->translator->trans('ss.flash.error_while_switching_locale')); |
||
| 91 | $this->logger->error('An error occurred while switching locales'); |
||
| 92 | return $this->redirect($returnUrl); |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->logger->info('Successfully switched locale'); |
||
| 96 | |||
| 97 | return $this->redirect($returnUrl); |
||
| 98 | } |
||
| 100 |