| Conditions | 16 |
| Paths | 32 |
| Total Lines | 95 |
| Code Lines | 59 |
| 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 |
||
| 40 | #[Route('/login_json', name: 'login_json', methods: ['POST'])] |
||
| 41 | public function loginJson(Request $request, EntityManager $entityManager, SettingsManager $settingsManager, TokenStorageInterface $tokenStorage, TranslatorInterface $translator): Response |
||
| 42 | { |
||
| 43 | if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { |
||
| 44 | return $this->json( |
||
| 45 | [ |
||
| 46 | 'error' => 'Invalid login request: check that the Content-Type header is "application/json".', |
||
| 47 | ], |
||
| 48 | 400 |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | $user = $this->userHelper->getCurrent(); |
||
| 53 | |||
| 54 | if (1 !== $user->getActive()) { |
||
| 55 | if (0 === $user->getActive()) { |
||
| 56 | $message = $translator->trans('Account not activated.'); |
||
| 57 | } else { |
||
| 58 | $message = $translator->trans('Invalid credentials. Please try again or contact support if you continue to experience issues.'); |
||
| 59 | } |
||
| 60 | |||
| 61 | $tokenStorage->setToken(null); |
||
| 62 | $request->getSession()->invalidate(); |
||
| 63 | |||
| 64 | return $this->json(['error' => $message], 401); |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($user->getMfaEnabled()) { |
||
| 68 | $totpCode = null; |
||
| 69 | $data = json_decode($request->getContent(), true); |
||
| 70 | if (isset($data['totp'])) { |
||
| 71 | $totpCode = $data['totp']; |
||
| 72 | } |
||
| 73 | |||
| 74 | if (null === $totpCode || !$this->isTOTPValid($user, $totpCode)) { |
||
| 75 | $tokenStorage->setToken(null); |
||
| 76 | $request->getSession()->invalidate(); |
||
| 77 | |||
| 78 | return $this->json([ |
||
| 79 | 'requires2FA' => true, |
||
| 80 | ], 200); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | if (null !== $user->getExpirationDate() && $user->getExpirationDate() <= new DateTime()) { |
||
| 85 | $message = $translator->trans('Your account has expired.'); |
||
| 86 | |||
| 87 | $tokenStorage->setToken(null); |
||
| 88 | $request->getSession()->invalidate(); |
||
| 89 | |||
| 90 | return $this->json(['error' => $message], 401); |
||
| 91 | } |
||
| 92 | |||
| 93 | $extraFieldValuesRepository = $this->entityManager->getRepository(ExtraFieldValues::class); |
||
| 94 | $legalTermsRepo = $this->entityManager->getRepository(Legal::class); |
||
| 95 | if ($user->hasRole('ROLE_STUDENT') |
||
| 96 | && 'true' === $this->settingsManager->getSetting('allow_terms_conditions') |
||
| 97 | && 'login' === $this->settingsManager->getSetting('load_term_conditions_section') |
||
| 98 | ) { |
||
| 99 | $termAndConditionStatus = false; |
||
| 100 | $extraValue = $extraFieldValuesRepository->findLegalAcceptByItemId($user->getId()); |
||
| 101 | if (!empty($extraValue['value'])) { |
||
| 102 | $result = $extraValue['value']; |
||
| 103 | $userConditions = explode(':', $result); |
||
| 104 | $version = $userConditions[0]; |
||
| 105 | $langId = (int) $userConditions[1]; |
||
| 106 | $realVersion = $legalTermsRepo->getLastVersion($langId); |
||
| 107 | $termAndConditionStatus = ($version >= $realVersion); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (false === $termAndConditionStatus) { |
||
| 111 | $tempTermAndCondition = ['user_id' => $user->getId()]; |
||
| 112 | |||
| 113 | $this->tokenStorage->setToken(null); |
||
| 114 | $request->getSession()->invalidate(); |
||
| 115 | |||
| 116 | $request->getSession()->start(); |
||
| 117 | $request->getSession()->set('term_and_condition', $tempTermAndCondition); |
||
| 118 | |||
| 119 | $responseData = [ |
||
| 120 | 'redirect' => '/main/auth/inscription.php', |
||
| 121 | 'load_terms' => true, |
||
| 122 | ]; |
||
| 123 | |||
| 124 | return new JsonResponse($responseData, Response::HTTP_OK); |
||
| 125 | } |
||
| 126 | $request->getSession()->remove('term_and_condition'); |
||
| 127 | } |
||
| 128 | |||
| 129 | $data = null; |
||
| 130 | if ($user) { |
||
| 131 | $data = $this->serializer->serialize($user, 'jsonld', ['groups' => ['user_json:read']]); |
||
| 132 | } |
||
| 133 | |||
| 134 | return new JsonResponse($data, Response::HTTP_OK, [], true); |
||
| 135 | } |
||
| 179 |