Replace arrays with Tweet model

This commit is contained in:
Oliver Davies 2019-01-10 23:51:40 +00:00
parent 482df07a6d
commit c18fcd0e2e
5 changed files with 34 additions and 15 deletions

View file

@ -2,6 +2,7 @@
namespace App\Command; namespace App\Command;
use App\Model\Tweet;
use App\Service\TweetFetcher; use App\Service\TweetFetcher;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
@ -39,12 +40,12 @@ class FetchTweetsCommand extends Command
$io->table( $io->table(
['Tweet', 'Author', 'Created', 'ID'], ['Tweet', 'Author', 'Created', 'ID'],
$this->tweetFetcher->getTweets()->map(function (array $tweet) { $this->tweetFetcher->getTweets()->map(function (Tweet $tweet) {
return [ return [
$tweet['text'], $tweet->getText(),
$tweet['author'], $tweet->getAuthor(),
$tweet['created'], $tweet->getCreated(),
$tweet['id'], $tweet->getId(),
]; ];
})->all() })->all()
); );

View file

@ -2,6 +2,7 @@
namespace App\Command; namespace App\Command;
use App\Model\Tweet;
use App\Service\Retweeter; use App\Service\Retweeter;
use App\Service\TweetFetcher; use App\Service\TweetFetcher;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -36,7 +37,7 @@ class RetweetTweetsCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$this->tweetFetcher->getTweets()->each(function (array $tweet) { $this->tweetFetcher->getTweets()->each(function (Tweet $tweet) {
$this->retweeter->retweet($tweet); $this->retweeter->retweet($tweet);
}); });
} }

View file

@ -24,6 +24,21 @@ class Tweet
$this->author = $author; $this->author = $author;
} }
public function getText(): string
{
return $this->text;
}
public function getCreated(): string
{
return $this->created;
}
public function getAuthor(): string
{
return $this->author;
}
public function setId(int $id): void public function setId(int $id): void
{ {
$this->id = $id; $this->id = $id;

View file

@ -2,6 +2,7 @@
namespace App\Service; namespace App\Service;
use App\Model\Tweet;
use Codebird\Codebird; use Codebird\Codebird;
class Retweeter class Retweeter
@ -26,10 +27,10 @@ class Retweeter
$this->codebird = $codebird; $this->codebird = $codebird;
} }
public function retweet(array $tweet): void public function retweet(Tweet $tweet): void
{ {
$this->codebird->statuses_retweet_ID([ $this->codebird->statuses_retweet_ID([
'id' => $tweet['id'], 'id' => $tweet->getId(),
]); ]);
} }
} }

View file

@ -2,6 +2,7 @@
namespace App\Service; namespace App\Service;
use App\Model\Tweet;
use Codebird\Codebird; use Codebird\Codebird;
use Tightenco\Collect\Support\Collection; use Tightenco\Collect\Support\Collection;
@ -56,13 +57,13 @@ class TweetFetcher
} }
return collect($response->get('statuses')) return collect($response->get('statuses'))
->map(function (\stdClass $tweet) { ->map(function (\stdClass $status) {
return [ return tap(new Tweet(), function (Tweet $tweet) use ($status) {
'id' => $tweet->id, $tweet->setId($status->id);
'created' => strtotime($tweet->created_at), $tweet->setText($status->text);
'text' => $tweet->text, $tweet->setCreated(strtotime($status->created_at));
'author' => $tweet->user->screen_name, $tweet->setAuthor($status->user->screen_name);
]; });
})->reverse(); })->reverse();
} }