/**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $producerKey = $input->getOption('producer');
     /** @var \OldSound\RabbitMqBundle\RabbitMq\Producer $producer */
     $producer = $this->getContainer()->get(sprintf('old_sound_rabbit_mq.weaving_the_web_amqp.%s_producer', $producerKey));
     if ($input->hasOption('routing_key') && !is_null($input->getOption('routing_key'))) {
         $routingKey = $input->getOption('routing_key');
     } else {
         $routingKey = '';
     }
     $tokens = $this->getTokens($input);
     $this->setUpLogger();
     $this->setupAccessor($tokens);
     /** @var \WTW\UserBundle\Repository\UserRepository $userRepository */
     $userRepository = $this->getContainer()->get('wtw_user.repository.user');
     /** @var \Doctrine\ORM\EntityManager $entityManager */
     $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
     /** @var \Symfony\Bundle\FrameworkBundle\Translation\Translator $translator */
     $translator = $this->getContainer()->get('translator');
     try {
         $friends = $this->accessor->showUserFriends($input->getOption('screen_name'));
     } catch (UnavailableResourceException $exception) {
         $outputMessage = $translator->trans('amqp.error.unavailable_host', ['{{ host }}' => $this->accessor->getApiHost()], 'messages');
         $output->writeln($outputMessage);
         return $exception->getCode();
     }
     $messageBody = $tokens;
     $invalidUsers = 0;
     foreach ($friends->ids as $friend) {
         $result = $userRepository->findOneBy(['twitterID' => $friend]);
         if (count($result) === 1) {
             $user = $result;
         } else {
             try {
                 $twitterUser = $this->accessor->showUser($friend);
                 if (isset($twitterUser->protected) && $twitterUser->protected) {
                     $protectedAccount = $translator->trans('logs.info.account_protected', ['{{ user }}' => $twitterUser->screen_name], 'logs');
                     throw new ProtectedAccountException($protectedAccount);
                 }
                 if (isset($twitterUser->screen_name)) {
                     $user = new User();
                     $user->setTwitterUsername($twitterUser->screen_name);
                     $user->setTwitterID($friend);
                     $user->setEnabled(false);
                     $user->setLocked(false);
                     $user->setEmail('@' . $twitterUser->screen_name);
                     $user->setStatus(0);
                     $entityManager->persist($user);
                     $entityManager->flush();
                     $publishedMessage = $translator->trans('amqp.info.user_persisted', ['{{ user }}' => $twitterUser->screen_name], 'messages');
                     $this->logger->info($publishedMessage);
                 } else {
                     $this->logger->info(serialize($twitterUser));
                 }
             } catch (SuspendedAccountException $exception) {
                 $suspendedAccount = $translator->trans('amqp.output.suspended_account', ['{{ user }}' => $friend], 'messages');
                 $this->sendMessage($suspendedAccount, 'info', $output, $exception);
                 $invalidUsers++;
                 // TODO Flag suspended accounts
                 // Skip suspended accounts
                 continue;
             } catch (ProtectedAccountException $exception) {
                 $protectedAccount = $translator->trans('amqp.output.protected_account', ['{{ user }}' => $friend], 'messages');
                 $this->sendMessage($protectedAccount, 'info', $output, $exception);
                 $invalidUsers++;
                 // TODO Flag protected accounts
                 // Skip protected accounts
                 continue;
             } catch (UnavailableResourceException $exception) {
                 $unavailableResource = $translator->trans('amqp.output.unavailable_resource', ['{{ user }}' => $friend], 'messages');
                 $this->sendMessage($unavailableResource, 'error', $output, $exception);
                 return $exception->getCode();
             }
         }
         if (isset($user)) {
             $messageBody['screen_name'] = $user->getTwitterUsername();
             $producer->setContentType('application/json');
             $producer->publish(serialize(json_encode($messageBody)), $routingKey);
             $publishedMessage = $translator->trans('amqp.info.message_published', ['{{ user }}' => $messageBody['screen_name']], 'messages');
             $this->logger->info($publishedMessage);
         }
     }
     $output->writeln($translator->trans('amqp.production.friendlist.success', ['{{ count }}' => count($friends->ids) - $invalidUsers], 'messages'));
 }
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     /**
      * @var \OldSound\RabbitMqBundle\RabbitMq\Producer $producer
      */
     $producer = $this->getContainer()->get('old_sound_rabbit_mq.weaving_the_web_amqp.twitter.user_status_producer');
     $tokens = $this->getTokens($input);
     $this->setUpLogger();
     $this->setupAccessor($tokens);
     $lists = $this->accessor->getUserLists($input->getOption('screen_name'));
     $messageBody = $tokens;
     /**
      * @var \WTW\UserBundle\Repository\UserRepository $userRepository
      */
     $userRepository = $this->getContainer()->get('wtw_user.repository.user');
     /**
      * @var \Doctrine\ORM\EntityManager $entityManager
      */
     $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
     if ($input->hasOption('list') && !is_null($input->getOption('list'))) {
         $listRestriction = $input->getOption('list');
     } else {
         $listRestriction = null;
     }
     foreach ($lists as $list) {
         if (is_null($listRestriction) || $list->name === $listRestriction) {
             $members = $this->accessor->getListMembers($list->id);
             foreach ($members->users as $friend) {
                 $result = $userRepository->findOneBy(['twitterID' => $friend->id]);
                 if (count($result) === 1) {
                     $user = $result;
                 } else {
                     /**
                      * TODO Handle errors with exception
                      */
                     $twitterUser = $this->accessor->showUser($friend->screen_name);
                     if (isset($twitterUser->screen_name)) {
                         $message = '[publishing new message produced for "' . $twitterUser->screen_name . '"]';
                         $this->logger->info($message);
                         $user = new User();
                         $user->setTwitterUsername($twitterUser->screen_name);
                         $user->setTwitterID($friend->id);
                         $user->setEnabled(false);
                         $user->setLocked(false);
                         $user->setEmail('@' . $twitterUser->screen_name);
                         $user->setStatus(0);
                         $entityManager->persist($user);
                         $entityManager->flush();
                     } elseif (isset($twitterUser->errors) && is_array($twitterUser->errors) && isset($twitterUser->errors[0])) {
                         $message = print_r($twitterUser->errors[0]->message, true);
                         $this->logger->error($message);
                         break;
                     }
                 }
                 $messageBody['screen_name'] = $user->getTwitterUsername();
                 $producer->setContentType('application/json');
                 $producer->publish(serialize(json_encode($messageBody)));
             }
             /**
              * @var \Symfony\Bundle\FrameworkBundle\Translation\Translator $translator
              */
             $translator = $this->getContainer()->get('translator');
             $output->writeln($translator->trans('amqp.production.list_members.success', ['{{ count }}' => count($members->users), '{{ list }}' => $list->name]));
         }
     }
 }