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 Controller |
||
36 | { |
||
37 | |||
38 | public function __construct( |
||
39 | private readonly LoggerInterface $logger, |
||
40 | InstitutionConfigurationOptionsService $configurationOptionsService, |
||
41 | private readonly TranslatorInterface $translator, |
||
42 | private readonly IdentityService $identityService, |
||
43 | ) { |
||
44 | parent::__construct($logger, $configurationOptionsService); |
||
45 | } |
||
46 | |||
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, |
||
104 |