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