Tidy imports
This commit is contained in:
parent
1f3eada5bb
commit
2079aaef0e
|
@ -4,12 +4,10 @@ namespace App\Command;
|
||||||
|
|
||||||
use App\Entity\Tweet;
|
use App\Entity\Tweet;
|
||||||
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\InputArgument;
|
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
||||||
|
|
||||||
class FetchTweetsCommand extends Command
|
class FetchTweetsCommand extends Command
|
||||||
{
|
{
|
||||||
|
@ -20,11 +18,17 @@ class FetchTweetsCommand extends Command
|
||||||
*/
|
*/
|
||||||
private $tweetFetcher;
|
private $tweetFetcher;
|
||||||
|
|
||||||
public function __construct(TweetFetcher $tweetFetcher)
|
/**
|
||||||
|
* @var \Doctrine\ORM\EntityManagerInterface
|
||||||
|
*/
|
||||||
|
private $entityManager;
|
||||||
|
|
||||||
|
public function __construct(TweetFetcher $tweetFetcher, EntityManagerInterface $entityManager)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->tweetFetcher = $tweetFetcher;
|
$this->tweetFetcher = $tweetFetcher;
|
||||||
|
$this->entityManager = $entityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
|
@ -36,18 +40,10 @@ class FetchTweetsCommand extends Command
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$this->tweetFetcher->getTweets()->each(function (Tweet $tweet) {
|
||||||
|
$this->entityManager->persist($tweet);
|
||||||
|
});
|
||||||
|
|
||||||
$io->table(
|
$this->entityManager->flush();
|
||||||
['Tweet', 'Author', 'Created', 'ID'],
|
|
||||||
$this->tweetFetcher->getTweets()->map(function (Tweet $tweet) {
|
|
||||||
return [
|
|
||||||
$tweet->getText(),
|
|
||||||
$tweet->getAuthor(),
|
|
||||||
$tweet->getCreated(),
|
|
||||||
$tweet->getId(),
|
|
||||||
];
|
|
||||||
})->all()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
use App\Entity\Tweet;
|
use App\Entity\Tweet;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use App\Repository\TweetRepository;
|
||||||
use Tightenco\Collect\Support\Collection;
|
use Tightenco\Collect\Support\Collection;
|
||||||
|
|
||||||
class TweetFetcher
|
class TweetFetcher
|
||||||
|
@ -30,28 +30,30 @@ class TweetFetcher
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Doctrine\ORM\EntityManagerInterface
|
* @var \App\Repository\TweetRepository
|
||||||
*/
|
*/
|
||||||
private $entityManager;
|
private $tweetRepository;
|
||||||
|
|
||||||
public function __construct(Codebird $codebird, EntityManagerInterface $entityManager)
|
public function __construct(Codebird $codebird, TweetRepository $tweetRepository)
|
||||||
{
|
{
|
||||||
$this->codebird = $codebird;
|
$this->codebird = $codebird;
|
||||||
$this->entityManager = $entityManager;
|
$this->tweetRepository = $tweetRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTweets(): Collection
|
public function getTweets(): Collection
|
||||||
{
|
{
|
||||||
|
$latestTweet = $this->tweetRepository->findNewestTweet();
|
||||||
|
|
||||||
$response = collect($this->codebird->get()->search_tweets([
|
$response = collect($this->codebird->get()->search_tweets([
|
||||||
'q' => collect($this->params()->all())->implode(' AND '),
|
'q' => collect($this->params()->all())->implode(' AND '),
|
||||||
// 'since_id' => $this->lastTweetId,
|
'since_id' => $latestTweet ? $latestTweet->getId() : null,
|
||||||
]));
|
]));
|
||||||
|
|
||||||
if ($response->get('httpstatus') != 200) {
|
if ($response->get('httpstatus') != 200) {
|
||||||
dump($response);
|
dump($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
$tweets = collect($response->get('statuses'))
|
return collect($response->get('statuses'))
|
||||||
->map(function (\stdClass $status) {
|
->map(function (\stdClass $status) {
|
||||||
return tap(new Tweet(), function (Tweet $tweet) use ($status) {
|
return tap(new Tweet(), function (Tweet $tweet) use ($status) {
|
||||||
$tweet->setId($status->id);
|
$tweet->setId($status->id);
|
||||||
|
@ -62,10 +64,6 @@ class TweetFetcher
|
||||||
$this->entityManager->persist($tweet);
|
$this->entityManager->persist($tweet);
|
||||||
});
|
});
|
||||||
})->reverse();
|
})->reverse();
|
||||||
|
|
||||||
$this->entityManager->flush();
|
|
||||||
|
|
||||||
return $tweets;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function params(): Collection
|
private function params(): Collection
|
||||||
|
|
Loading…
Reference in a new issue