Fetch untweeted tweets, store retweet time

This commit is contained in:
Oliver Davies 2019-01-16 00:11:21 +00:00
parent 2c5e1cf756
commit 5624f86e38

View file

@ -6,6 +6,7 @@ use App\Entity\Tweet;
use App\Repository\TweetRepository; use App\Repository\TweetRepository;
use App\Service\Retweeter; use App\Service\Retweeter;
use App\Service\TweetFetcher; use App\Service\TweetFetcher;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
@ -19,25 +20,48 @@ class RetweetTweetsCommand extends Command
private $retweeter; private $retweeter;
public function __construct(TweetFetcher $tweetFetcher, Retweeter $retweeter) /**
{ * @var \App\Repository\TweetRepository
*/
private $tweetRepository;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
private $entityManager;
public function __construct(
TweetFetcher $tweetFetcher,
TweetRepository $tweetRepository,
Retweeter $retweeter,
EntityManagerInterface $entityManager
) {
parent::__construct(); parent::__construct();
$this->tweetFetcher = $tweetFetcher; $this->tweetFetcher = $tweetFetcher;
$this->retweeter = $retweeter; $this->retweeter = $retweeter;
$this->tweetRepository = $tweetRepository;
$this->entityManager = $entityManager;
} }
protected function configure() protected function configure()
{ {
$this $this
->setDescription('Add a short description for your command') ->setDescription('Add a short description for your command')
->addOption('number', null, InputOption::VALUE_OPTIONAL, 'Specify how many tweets to retweet.', 1)
; ;
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$this->tweetFetcher->getTweets()->each(function (Tweet $tweet) { $this->tweetRepository->getUntweetedTweets($input->getOption('number'))->each(function (Tweet $tweet) {
$this->retweeter->retweet($tweet); $this->retweeter->retweet($tweet);
$tweet->setRetweeted(time());
$this->entityManager->persist($tweet);
}); });
$this->entityManager->flush();
} }
} }