/**
  * Authenticates the specified user in session.
  *
  * @param int $viewerId
  * @param \MetaPlayer\Model\SocialNetwork|null $socialNetwork
  */
 public function authenticate($viewerId, SocialNetwork $socialNetwork = null)
 {
     if ($socialNetwork == null) {
         $socialNetwork = SocialNetwork::$VK;
     }
     $user = $this->getUser();
     if ($user != null) {
         $user->setSocialNetwork($socialNetwork);
         if ($user->getSocialId() != $viewerId) {
             $currentUserId = $user->getSocialId();
             $this->logger->warn("User {$currentUserId} replaced with {$viewerId}.");
         }
     } else {
         $user = $this->userRepository->findOneBySocialId($viewerId, $socialNetwork);
     }
     if ($user == null) {
         $user = new User($viewerId, $socialNetwork);
         $this->userRepository->persistAndFlush($user);
     }
     $this->httpSession->setAttribute(self::USER_ID, $user->getId());
     $this->httpSession->setAttribute(self::SOCIAL_NETWORK, (string) $socialNetwork);
 }