| Conditions | 7 |
| Paths | 12 |
| Total Lines | 54 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 36 | public function onKernelRequest(RequestEvent $event): void |
||
| 37 | { |
||
| 38 | if (null !== $this->security->getUser()) { |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | $request = $event->getRequest(); |
||
| 43 | $userIp = $request->getClientIp(); |
||
| 44 | |||
| 45 | $anonymousUserId = $this->getOrCreateAnonymousUserId($userIp); |
||
| 46 | if (null !== $anonymousUserId) { |
||
| 47 | $trackLoginRepository = $this->entityManager->getRepository(TrackELogin::class); |
||
| 48 | |||
| 49 | // Check if a login record already exists for this user and IP |
||
| 50 | $existingLogin = $trackLoginRepository->findOneBy(['userIp' => $userIp, 'user' => $anonymousUserId]); |
||
| 51 | if (!$existingLogin) { |
||
| 52 | // Record the access if it does not exist |
||
| 53 | $trackLogin = new TrackELogin(); |
||
| 54 | $trackLogin->setUserIp($userIp) |
||
| 55 | ->setLoginDate(new DateTime()) |
||
| 56 | ->setUser($this->entityManager->getReference(User::class, $anonymousUserId)) |
||
| 57 | ; |
||
| 58 | |||
| 59 | $this->entityManager->persist($trackLogin); |
||
| 60 | $this->entityManager->flush(); |
||
| 61 | } |
||
| 62 | |||
| 63 | $userRepository = $this->entityManager->getRepository(User::class); |
||
| 64 | $user = $userRepository->find($anonymousUserId); |
||
| 65 | |||
| 66 | if ($user) { |
||
| 67 | // Store user information in the session |
||
| 68 | $userInfo = [ |
||
| 69 | 'user_id' => $user->getId(), |
||
| 70 | 'username' => $user->getUsername(), |
||
| 71 | 'firstname' => $user->getFirstname(), |
||
| 72 | 'lastname' => $user->getLastname(), |
||
| 73 | 'firstName' => $user->getFirstname(), |
||
| 74 | 'lastName' => $user->getLastname(), |
||
| 75 | 'email' => $user->getEmail(), |
||
| 76 | 'official_code' => $user->getOfficialCode(), |
||
| 77 | 'picture_uri' => $user->getPictureUri(), |
||
| 78 | 'status' => $user->getStatus(), |
||
| 79 | 'active' => $user->getActive(), |
||
| 80 | 'auth_source' => $user->getAuthSource(), |
||
| 81 | 'theme' => $user->getTheme(), |
||
| 82 | 'language' => $user->getLocale(), |
||
| 83 | 'registration_date' => $user->getRegistrationDate()->format('Y-m-d H:i:s'), |
||
| 84 | 'expiration_date' => $user->getExpirationDate() ? $user->getExpirationDate()->format('Y-m-d H:i:s') : null, |
||
| 85 | 'last_login' => $user->getLastLogin() ? $user->getLastLogin()->format('Y-m-d H:i:s') : null, |
||
| 86 | 'is_anonymous' => true, |
||
| 87 | ]; |
||
| 88 | |||
| 89 | $this->session->set('_user', $userInfo); |
||
| 90 | } |
||
| 151 |