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",
"license": "proprietary",
"require": {
"php": "~7.1",
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"josephlavin/tap": "^1.0",
"jublonet/codebird-php": "^3.1",
"symfony/console": "4.2.*",
"symfony/dotenv": "4.3.*",
"symfony/dotenv": "4.2.*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*",
"symfony/orm-pack": "^1.0",
@ -41,7 +41,8 @@
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd"
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@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\Service\TweetFetcher;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class FetchTweetsCommand extends Command
{
@ -18,34 +18,32 @@ class FetchTweetsCommand extends Command
*/
private $tweetFetcher;
public function __construct(TweetFetcher $tweetFetcher)
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
private $entityManager;
public function __construct(TweetFetcher $tweetFetcher, EntityManagerInterface $entityManager)
{
parent::__construct();
$this->tweetFetcher = $tweetFetcher;
$this->entityManager = $entityManager;
}
protected function configure()
{
$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)
{
$io = new SymfonyStyle($input, $output);
$this->tweetFetcher->getTweets()->each(function (Tweet $tweet) {
$this->entityManager->persist($tweet);
});
$io->table(
['Tweet', 'Author', 'Created', 'ID'],
$this->tweetFetcher->getTweets()->map(function (Tweet $tweet) {
return [
$tweet->getText(),
$tweet->getAuthor(),
$tweet->getCreated(),
$tweet->getId(),
];
})->all()
);
$this->entityManager->flush();
}
}

View file

@ -2,15 +2,15 @@
namespace App\Command;
use App\Entity\Tweet;
use App\Repository\TweetRepository;
use App\Model\Tweet;
use App\Service\Retweeter;
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 RetweetTweetsCommand extends Command
{
@ -20,48 +20,25 @@ class RetweetTweetsCommand extends Command
private $retweeter;
/**
* @var \App\Repository\TweetRepository
*/
private $tweetRepository;
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
private $entityManager;
public function __construct(
TweetFetcher $tweetFetcher,
TweetRepository $tweetRepository,
Retweeter $retweeter,
EntityManagerInterface $entityManager
) {
public function __construct(TweetFetcher $tweetFetcher, Retweeter $retweeter)
{
parent::__construct();
$this->tweetFetcher = $tweetFetcher;
$this->retweeter = $retweeter;
$this->tweetRepository = $tweetRepository;
$this->entityManager = $entityManager;
}
protected function configure()
{
$this
->setDescription('Retweet one or more stored tweets')
->addOption('number', null, InputOption::VALUE_OPTIONAL, 'Specify how many tweets to retweet.', 1)
->setDescription('Add a short description for your command')
;
}
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);
$tweet->setRetweeted(time());
$this->entityManager->persist($tweet);
});
$this->entityManager->flush();
}
}

View file

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

View file

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

View file

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

View file

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