From c199a9fd78a496fd3ac09abb08a57658c430d104 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 15 Jan 2019 23:37:55 +0000 Subject: [PATCH] Revert "Tidy imports" This reverts commit 2079aaef0e21ce9ad6864e0e99db8ffc5e877a06. --- src/Command/FetchTweetsCommand.php | 28 ++++++++++++++++------------ src/Service/TweetFetcher.php | 20 +++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/Command/FetchTweetsCommand.php b/src/Command/FetchTweetsCommand.php index 7d57432..e846647 100644 --- a/src/Command/FetchTweetsCommand.php +++ b/src/Command/FetchTweetsCommand.php @@ -4,10 +4,12 @@ namespace App\Command; use App\Entity\Tweet; use App\Service\TweetFetcher; -use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; class FetchTweetsCommand extends Command { @@ -18,17 +20,11 @@ class FetchTweetsCommand extends Command */ private $tweetFetcher; - /** - * @var \Doctrine\ORM\EntityManagerInterface - */ - private $entityManager; - - public function __construct(TweetFetcher $tweetFetcher, EntityManagerInterface $entityManager) + public function __construct(TweetFetcher $tweetFetcher) { parent::__construct(); $this->tweetFetcher = $tweetFetcher; - $this->entityManager = $entityManager; } protected function configure() @@ -40,10 +36,18 @@ class FetchTweetsCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { - $this->tweetFetcher->getTweets()->each(function (Tweet $tweet) { - $this->entityManager->persist($tweet); - }); + $io = new SymfonyStyle($input, $output); - $this->entityManager->flush(); + $io->table( + ['Tweet', 'Author', 'Created', 'ID'], + $this->tweetFetcher->getTweets()->map(function (Tweet $tweet) { + return [ + $tweet->getText(), + $tweet->getAuthor(), + $tweet->getCreated(), + $tweet->getId(), + ]; + })->all() + ); } } diff --git a/src/Service/TweetFetcher.php b/src/Service/TweetFetcher.php index 0838853..78fa9af 100644 --- a/src/Service/TweetFetcher.php +++ b/src/Service/TweetFetcher.php @@ -3,7 +3,7 @@ namespace App\Service; use App\Entity\Tweet; -use App\Repository\TweetRepository; +use Doctrine\ORM\EntityManagerInterface; use Tightenco\Collect\Support\Collection; class TweetFetcher @@ -30,30 +30,28 @@ class TweetFetcher ]; /** - * @var \App\Repository\TweetRepository + * @var \Doctrine\ORM\EntityManagerInterface */ - private $tweetRepository; + private $entityManager; - public function __construct(Codebird $codebird, TweetRepository $tweetRepository) + public function __construct(Codebird $codebird, EntityManagerInterface $entityManager) { $this->codebird = $codebird; - $this->tweetRepository = $tweetRepository; + $this->entityManager = $entityManager; } public function getTweets(): Collection { - $latestTweet = $this->tweetRepository->findNewestTweet(); - $response = collect($this->codebird->get()->search_tweets([ 'q' => collect($this->params()->all())->implode(' AND '), - 'since_id' => $latestTweet ? $latestTweet->getId() : null, + // 'since_id' => $this->lastTweetId, ])); if ($response->get('httpstatus') != 200) { dump($response); } - return collect($response->get('statuses')) + $tweets = collect($response->get('statuses')) ->map(function (\stdClass $status) { return tap(new Tweet(), function (Tweet $tweet) use ($status) { $tweet->setId($status->id); @@ -64,6 +62,10 @@ class TweetFetcher $this->entityManager->persist($tweet); }); })->reverse(); + + $this->entityManager->flush(); + + return $tweets; } private function params(): Collection