original_string: protected function refreshUser(TokenInterface $token)
      {
          $user = $token->getUser();
          if (!$user instanceof UserInterface) {
              return $token;
          }
  
          $userNotFoundByProvider = false;
          $userDeauthenticated = false;
  
          foreach ($this->userProviders as $provider) {
              if (!$provider instanceof UserProviderInterface) {
                  throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "%s".', \get_class($provider), UserProviderInterface::class));
              }
  
              try {
                  $refreshedUser = $provider->refreshUser($user);
                  $newToken = clone $token;
                  $newToken->setUser($refreshedUser);
  
                  // tokens can be deauthenticated if the user has been changed.
                  if (!$newToken->isAuthenticated()) {
                      $userDeauthenticated = true;
  
                      if (null !== $this->logger) {
                          $this->logger->debug('Cannot refresh token because user has changed.', ['username' => $refreshedUser->getUsername(), 'provider' => \get_class($provider)]);
                      }
  
                      continue;
                  }
  
                  $token->setUser($refreshedUser);
  
                  if (null !== $this->logger) {
                      $context = ['provider' => \get_class($provider), 'username' => $refreshedUser->getUsername()];
  
                      if ($token instanceof SwitchUserToken) {
                          $context['impersonator_username'] = $token->getOriginalToken()->getUsername();
                      } else {
                          foreach ($token->getRoles(false) as $role) {
                              if ($role instanceof SwitchUserRole) {
                                  $context['impersonator_username'] = $role->getSource(false)->getUsername();
                                  break;
                              }
                          }
                      }
  
                      $this->logger->debug('User was reloaded from a user provider.', $context);
                  }
  
                  return $token;
              } catch (UnsupportedUserException $e) {
                  // let's try the next user provider
              } catch (UsernameNotFoundException $e) {
                  if (null !== $this->logger) {
                      $this->logger->warning('Username could not be found in the selected user provider.', ['username' => $e->getUsername(), 'provider' => \get_class($provider)]);
                  }
  
                  $userNotFoundByProvider = true;
              }
          }
  
          if ($userDeauthenticated) {
              if (null !== $this->logger) {
                  $this->logger->debug('Token was deauthenticated after trying to refresh it.');
              }
  
              if (null !== $this->dispatcher) {
                  $this->dispatcher->dispatch(new DeauthenticatedEvent($token, $newToken), DeauthenticatedEvent::class);
              }
  
              return null;
          }
  
          if ($userNotFoundByProvider) {
              return null;
          }
  
          throw new \RuntimeException(sprintf('There is no user provider for user "%s".', \get_class($user)));
      }
code: protected function refreshUser(TokenInterface $token)
      {
          $user = $token->getUser();
          if (!$user instanceof UserInterface) {
              return $token;
          }
  
          $userNotFoundByProvider = false;
          $userDeauthenticated = false;
  
          foreach ($this->userProviders as $provider) {
              if (!$provider instanceof UserProviderInterface) {
                  throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "%s".', \get_class($provider), UserProviderInterface::class));
              }
  
              try {
                  $refreshedUser = $provider->refreshUser($user);
                  $newToken = clone $token;
                  $newToken->setUser($refreshedUser);
  
                  // tokens can be deauthenticated if the user has been changed.
                  if (!$newToken->isAuthenticated()) {
                      $userDeauthenticated = true;
  
                      if (null !== $this->logger) {
                          $this->logger->debug('Cannot refresh token because user has changed.', ['username' => $refreshedUser->getUsername(), 'provider' => \get_class($provider)]);
                      }
  
                      continue;
                  }
  
                  $token->setUser($refreshedUser);
  
                  if (null !== $this->logger) {
                      $context = ['provider' => \get_class($provider), 'username' => $refreshedUser->getUsername()];
  
                      if ($token instanceof SwitchUserToken) {
                          $context['impersonator_username'] = $token->getOriginalToken()->getUsername();
                      } else {
                          foreach ($token->getRoles(false) as $role) {
                              if ($role instanceof SwitchUserRole) {
                                  $context['impersonator_username'] = $role->getSource(false)->getUsername();
                                  break;
                              }
                          }
                      }
  
                      $this->logger->debug('User was reloaded from a user provider.', $context);
                  }
  
                  return $token;
              } catch (UnsupportedUserException $e) {
                  // let's try the next user provider
              } catch (UsernameNotFoundException $e) {
                  if (null !== $this->logger) {
                      $this->logger->warning('Username could not be found in the selected user provider.', ['username' => $e->getUsername(), 'provider' => \get_class($provider)]);
                  }
  
                  $userNotFoundByProvider = true;
              }
          }
  
          if ($userDeauthenticated) {
              if (null !== $this->logger) {
                  $this->logger->debug('Token was deauthenticated after trying to refresh it.');
              }
  
              if (null !== $this->dispatcher) {
                  $this->dispatcher->dispatch(new DeauthenticatedEvent($token, $newToken), DeauthenticatedEvent::class);
              }
  
              return null;
          }
  
          if ($userNotFoundByProvider) {
              return null;
          }
  
          throw new \RuntimeException(sprintf('There is no user provider for user "%s".', \get_class($user)));
      }
code_tokens: protected function refreshUser ( TokenInterface $ token ) { $ user = $ token -> getUser ( ) ; if ( ! $ user instanceof UserInterface ) { return $ token ; } $ userNotFoundByProvider = false ; $ userDeauthenticated = false ; foreach ( $ this -> userProviders as $ provider ) { if ( ! $ provider instanceof UserProviderInterface ) { throw new \ InvalidArgumentException ( sprintf ( 'User provider "%s" must implement "%s".' , \ get_class ( $ provider ) , UserProviderInterface :: class ) ) ; } try { $ refreshedUser = $ provider -> refreshUser ( $ user ) ; $ newToken = clone $ token ; $ newToken -> setUser ( $ refreshedUser ) ; // tokens can be deauthenticated if the user has been changed. if ( ! $ newToken -> isAuthenticated ( ) ) { $ userDeauthenticated = true ; if ( null !== $ this -> logger ) { $ this -> logger -> debug ( 'Cannot refresh token because user has changed.' , [ 'username' => $ refreshedUser -> getUsername ( ) , 'provider' => \ get_class ( $ provider ) ] ) ; } continue ; } $ token -> setUser ( $ refreshedUser ) ; if ( null !== $ this -> logger ) { $ context = [ 'provider' => \ get_class ( $ provider ) , 'username' => $ refreshedUser -> getUsername ( ) ] ; if ( $ token instanceof SwitchUserToken ) { $ context [ 'impersonator_username' ] = $ token -> getOriginalToken ( ) -> getUsername ( ) ; } else { foreach ( $ token -> getRoles ( false ) as $ role ) { if ( $ role instanceof SwitchUserRole ) { $ context [ 'impersonator_username' ] = $ role -> getSource ( false ) -> getUsername ( ) ; break ; } } } $ this -> logger -> debug ( 'User was reloaded from a user provider.' , $ context ) ; } return $ token ; } catch ( UnsupportedUserException $ e ) { // let's try the next user provider } catch ( UsernameNotFoundException $ e ) { if ( null !== $ this -> logger ) { $ this -> logger -> warning ( 'Username could not be found in the selected user provider.' , [ 'username' => $ e -> getUsername ( ) , 'provider' => \ get_class ( $ provider ) ] ) ; } $ userNotFoundByProvider = true ; } } if ( $ userDeauthenticated ) { if ( null !== $ this -> logger ) { $ this -> logger -> debug ( 'Token was deauthenticated after trying to refresh it.' ) ; } if ( null !== $ this -> dispatcher ) { $ this -> dispatcher -> dispatch ( new DeauthenticatedEvent ( $ token , $ newToken ) , DeauthenticatedEvent :: class ) ; } return null ; } if ( $ userNotFoundByProvider ) { return null ; } throw new \ RuntimeException ( sprintf ( 'There is no user provider for user "%s".' , \ get_class ( $ user ) ) ) ; }
docstring: Refreshes the user by reloading it from the user provider.
  
  @return TokenInterface|null
  
  @throws \RuntimeException
docstring_tokens: Refreshes the user by reloading it from the user provider .