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