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\Routing\Annotation\Route; |
||
31 | use Symfony\Contracts\Translation\TranslatorInterface; |
||
32 | |||
33 | final class LocaleController extends Controller |
||
34 | { |
||
35 | |||
36 | public function __construct( |
||
37 | private readonly LoggerInterface $logger, |
||
38 | InstitutionConfigurationOptionsService $configurationOptionsService, |
||
39 | private readonly TranslatorInterface $translator, |
||
40 | private readonly IdentityService $identityService, |
||
41 | ) { |
||
42 | parent::__construct($logger, $configurationOptionsService); |
||
43 | } |
||
44 | |||
45 | #[Route( |
||
46 | path: '/switch-locale', |
||
47 | name: 'ss_switch_locale', |
||
48 | requirements: ['return-url' => '.+'], |
||
49 | methods: ['POST'] |
||
50 | )] |
||
51 | public function switchLocale(Request $request): RedirectResponse |
||
52 | { |
||
53 | $returnUrl = $request->query->get('return-url'); |
||
54 | |||
55 | // Return URLs generated by us always include a path (ie. at least a forward slash) |
||
56 | // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878 |
||
57 | $domain = $request->getSchemeAndHttpHost() . '/'; |
||
58 | if (!str_starts_with($returnUrl, $domain)) { |
||
59 | $this->logger->error(sprintf( |
||
60 | 'Identity "%s" used illegal return-url for redirection after changing locale, aborting request', |
||
61 | $this->getIdentity()->id |
||
62 | )); |
||
63 | |||
64 | throw new BadRequestHttpException('Invalid return-url given'); |
||
65 | } |
||
66 | |||
67 | $this->logger->info('Switching locale...'); |
||
68 | |||
69 | $identity = $this->getIdentity(); |
||
70 | if (!$identity) { |
||
71 | throw new AccessDeniedHttpException('Cannot switch locales when not authenticated'); |
||
72 | } |
||
73 | |||
74 | $command = new SwitchLocaleCommand(); |
||
75 | $command->identityId = $identity->id; |
||
76 | |||
77 | $form = $this->createForm( |
||
78 | SwitchLocaleType::class, |
||
79 | $command, |
||
80 | ['route' => 'ss_switch_locale', 'route_parameters' => ['return_url' => $returnUrl]] |
||
81 | ); |
||
102 |