| Conditions | 6 |
| Paths | 5 |
| Total Lines | 51 |
| Code Lines | 30 |
| 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 |
||
| 30 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
||
| 31 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
||
| 32 | use Symfony\Component\Routing\Attribute\Route; |
||
| 33 | use Symfony\Contracts\Translation\TranslatorInterface; |
||
| 34 | |||
| 35 | final class LocaleController extends AbstractController |
||
| 36 | { |
||
| 37 | public function __construct( |
||
| 38 | private readonly LoggerInterface $logger, |
||
| 39 | private readonly TranslatorInterface $translator, |
||
| 40 | private readonly IdentityService $identityService, |
||
| 41 | ) { |
||
| 42 | } |
||
| 43 | |||
| 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); |
||
| 100 |