| Conditions | 6 |
| Paths | 5 |
| Total Lines | 52 |
| 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 | public function switchLocaleAction(Request $request) |
||
| 32 | { |
||
| 33 | $returnUrl = $request->query->get('return-url'); |
||
| 34 | |||
| 35 | // Return URLs generated by us always include a path (ie. at least a forward slash) |
||
| 36 | // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878 |
||
| 37 | $domain = $request->getSchemeAndHttpHost() . '/'; |
||
| 38 | if (strpos($returnUrl, $domain) !== 0) { |
||
| 39 | $this->get('logger')->error(sprintf( |
||
| 40 | 'Identity "%s" used illegal return-url for redirection after changing locale, aborting request', |
||
| 41 | $this->getIdentity()->id |
||
| 42 | )); |
||
| 43 | |||
| 44 | throw new BadRequestHttpException('Invalid return-url given'); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** @var LoggerInterface $logger */ |
||
| 48 | $logger = $this->get('logger'); |
||
| 49 | $logger->info('Switching locale...'); |
||
| 50 | |||
| 51 | $identity = $this->getIdentity(); |
||
| 52 | if (!$identity) { |
||
| 53 | throw new AccessDeniedHttpException('Cannot switch locales when not authenticated'); |
||
| 54 | } |
||
| 55 | |||
| 56 | $command = new SwitchLocaleCommand(); |
||
| 57 | $command->identityId = $identity->id; |
||
| 58 | |||
| 59 | $form = $this->createForm( |
||
| 60 | SwitchLocaleType::class, |
||
| 61 | $command, |
||
| 62 | ['route' => 'ra_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]] |
||
| 63 | ); |
||
| 64 | $form->handleRequest($request); |
||
| 65 | |||
| 66 | if ($form->isSubmitted() && !$form->isValid()) { |
||
| 67 | $this->addFlash('error', $this->get('translator')->trans('ra.flash.invalid_switch_locale_form')); |
||
| 68 | $logger->error('The switch locale form unexpectedly contained invalid data'); |
||
| 69 | return $this->redirect($returnUrl); |
||
| 70 | } |
||
| 71 | |||
| 72 | $service = $this->get('ra.service.identity'); |
||
| 73 | if (!$service->switchLocale($command)) { |
||
| 74 | $this->addFlash('error', $this->get('translator')->trans('ra.flash.error_while_switching_locale')); |
||
| 75 | $logger->error('An error occurred while switching locales'); |
||
| 76 | return $this->redirect($returnUrl); |
||
| 77 | } |
||
| 78 | |||
| 79 | $logger->info('Successfully switched locale'); |
||
| 80 | |||
| 81 | return $this->redirect($returnUrl); |
||
| 82 | } |
||
| 83 | |||
| 92 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.