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