Compare commits

..

No commits in common. "master" and "add-doctrine" have entirely different histories.

8 changed files with 420 additions and 631 deletions

View file

@ -2,13 +2,13 @@
"type": "project", "type": "project",
"license": "proprietary", "license": "proprietary",
"require": { "require": {
"php": "~7.1", "php": "^7.1.3",
"ext-ctype": "*", "ext-ctype": "*",
"ext-iconv": "*", "ext-iconv": "*",
"josephlavin/tap": "^1.0", "josephlavin/tap": "^1.0",
"jublonet/codebird-php": "^3.1", "jublonet/codebird-php": "^3.1",
"symfony/console": "4.2.*", "symfony/console": "4.2.*",
"symfony/dotenv": "4.3.*", "symfony/dotenv": "4.2.*",
"symfony/flex": "^1.1", "symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*", "symfony/framework-bundle": "4.2.*",
"symfony/orm-pack": "^1.0", "symfony/orm-pack": "^1.0",
@ -41,7 +41,8 @@
}, },
"scripts": { "scripts": {
"auto-scripts": { "auto-scripts": {
"cache:clear": "symfony-cmd" "cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
}, },
"post-install-cmd": [ "post-install-cmd": [
"@auto-scripts" "@auto-scripts"

921
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,10 +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\InputInterface; use Symfony\Component\Console\Input\InputInterface;
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
{ {
@ -18,34 +18,32 @@ 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()
{ {
$this $this
->setDescription('Fetch and store any tweets to be retweeted') ->setDescription('Add a short description for your 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()
);
} }
} }

View file

@ -2,15 +2,15 @@
namespace App\Command; namespace App\Command;
use App\Entity\Tweet; use App\Model\Tweet;
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\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; 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 RetweetTweetsCommand extends Command class RetweetTweetsCommand extends Command
{ {
@ -20,48 +20,25 @@ 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('Retweet one or more stored tweets') ->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->tweetRepository->getUntweetedTweets($input->getOption('number'))->each(function (Tweet $tweet) { $this->tweetFetcher->getTweets()->each(function (Tweet $tweet) {
$this->retweeter->retweet($tweet); $this->retweeter->retweet($tweet);
$tweet->setRetweeted(time());
$this->entityManager->persist($tweet);
}); });
$this->entityManager->flush();
} }
} }

View file

@ -5,7 +5,6 @@ namespace App\Repository;
use App\Entity\Tweet; use App\Entity\Tweet;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface; use Symfony\Bridge\Doctrine\RegistryInterface;
use Tightenco\Collect\Support\Collection;
/** /**
* @method Tweet|null find($id, $lockMode = null, $lockVersion = null) * @method Tweet|null find($id, $lockMode = null, $lockVersion = null)
@ -30,19 +29,22 @@ class TweetRepository extends ServiceEntityRepository
return collect($result)->first(); return collect($result)->first();
} }
// /**
// * @return Tweet[] Returns an array of Tweet objects
public function getUntweetedTweets(int $limit): Collection // */
/*
public function findByExampleField($value)
{ {
return collect( return $this->createQueryBuilder('t')
$this->createQueryBuilder('t') ->andWhere('t.exampleField = :val')
->where('t.retweeted is NULL') ->setParameter('val', $value)
->orderBy('t.created', 'asc') ->orderBy('t.id', 'ASC')
->setMaxResults($limit) ->setMaxResults(10)
->getQuery() ->getQuery()
->getResult() ->getResult()
); ;
} }
*/
/* /*
public function findOneBySomeField($value): ?Tweet public function findOneBySomeField($value): ?Tweet

View file

@ -2,7 +2,7 @@
namespace App\Service; namespace App\Service;
use App\Entity\Tweet; use App\Model\Tweet;
class Retweeter class Retweeter
{ {

View file

@ -4,7 +4,6 @@ namespace App\Service;
use App\Entity\Tweet; use App\Entity\Tweet;
use App\Repository\TweetRepository; use App\Repository\TweetRepository;
use Doctrine\ORM\EntityManagerInterface;
use Tightenco\Collect\Support\Collection; use Tightenco\Collect\Support\Collection;
class TweetFetcher class TweetFetcher
@ -31,36 +30,30 @@ class TweetFetcher
]; ];
/** /**
* @var \Doctrine\ORM\EntityManagerInterface * @var \App\Repository\TweetRepository
*/
private $entityManager;
/**
* @var \App\Service\TweetRepository
*/ */
private $tweetRepository; private $tweetRepository;
public function __construct(Codebird $codebird, EntityManagerInterface $entityManager, TweetRepository $tweetRepository) public function __construct(Codebird $codebird, TweetRepository $tweetRepository)
{ {
$this->codebird = $codebird; $this->codebird = $codebird;
$this->entityManager = $entityManager;
$this->tweetRepository = $tweetRepository; $this->tweetRepository = $tweetRepository;
} }
public function getTweets(): Collection public function getTweets(): Collection
{ {
$params = ['q' => collect($this->params()->all())->implode(' AND ')]; $latestTweet = $this->tweetRepository->findNewestTweet();
if ($newestTweet = $this->tweetRepository->findNewestTweet()) {
$params['since_id'] = $newestTweet->getId();
}
$response = collect($this->codebird->get()->search_tweets($params)); $response = collect($this->codebird->get()->search_tweets([
'q' => collect($this->params()->all())->implode(' AND '),
'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);
@ -71,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

View file

@ -188,9 +188,6 @@
"ref": "5374e24d508ba8fd6ba9eb15170255fdb778316a" "ref": "5374e24d508ba8fd6ba9eb15170255fdb778316a"
} }
}, },
"symfony/stopwatch": {
"version": "v4.2.2"
},
"symfony/var-dumper": { "symfony/var-dumper": {
"version": "v4.2.2" "version": "v4.2.2"
}, },