/** * @param $tokenParameters * @throws \Exception */ protected function persistToken($tokenParameters) { if ($tokenParameters['screen_name'] !== $this->getUser()->getTwitterUserName()) { throw new \Exception('The token doesn\'t match ' . 'with the declared twitter username of current user'); } $tokens = $this->tokenRepository->findBy(['oauthToken' => $tokenParameters['oauth_token']]); if (count($tokens) === 0) { $token = $this->tokenRepository->makeToken($tokenParameters); /** @var \WTW\UserBundle\Entity\User $user */ $user = $this->getUser(); $phantomUser = $this->userManager->findUserBy(['twitter_username' => $tokenParameters['screen_name'], 'twitterID' => $tokenParameters['user_id']]); if (!is_null($phantomUser)) { $phantomUser->setTwitterID(null); $this->entityManager->persist($phantomUser); $this->entityManager->flush(); } $this->userManager->updateUserTwitterCredentials($user, $tokenParameters); $token->addUser($user); $this->entityManager->persist($token); $this->entityManager->flush(); $user->addToken($token); $this->entityManager->persist($user); $this->entityManager->flush(); $noticeMessage = 'successful_access_token_persistence'; } else { $noticeMessage = 'existing_access_token'; } $this->session->getFlashBag()->add('notice', $noticeMessage); }