diff --git a/src/Command/FetchTweetsCommand.php b/src/Command/FetchTweetsCommand.php index 581705c..e846647 100644 --- a/src/Command/FetchTweetsCommand.php +++ b/src/Command/FetchTweetsCommand.php @@ -2,7 +2,7 @@ namespace App\Command; -use App\Model\Tweet; +use App\Entity\Tweet; use App\Service\TweetFetcher; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; diff --git a/src/Entity/Tweet.php b/src/Entity/Tweet.php new file mode 100644 index 0000000..9d82e0c --- /dev/null +++ b/src/Entity/Tweet.php @@ -0,0 +1,112 @@ +id; + } + +// public function getTweetId(): ?int +// { +// return $this->tweet_id; +// } + +// public function setTweetId(int $tweet_id): self +// { +// $this->tweet_id = $tweet_id; +// +// return $this; +// } + + public function getText(): ?string + { + return $this->text; + } + + public function setText(string $text): self + { + $this->text = $text; + + return $this; + } + + public function getAuthor(): ?string + { + return $this->author; + } + + public function setAuthor(string $author): self + { + $this->author = $author; + + return $this; + } + + public function getCreated(): ?string + { + return $this->created; + } + + public function setCreated(string $created): self + { + $this->created = $created; + + return $this; + } + + public function setId(int $id) + { + $this->id = $id; + } + + public function getRetweeted(): ?string + { + return $this->retweeted; + } + + public function setRetweeted(?string $retweeted): self + { + $this->retweeted = $retweeted; + + return $this; + } +} diff --git a/src/Migrations/Version20190111201746.php b/src/Migrations/Version20190111201746.php new file mode 100644 index 0000000..554b0c5 --- /dev/null +++ b/src/Migrations/Version20190111201746.php @@ -0,0 +1,28 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE tweet (id INT AUTO_INCREMENT NOT NULL, tweet_id INT NOT NULL, text VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, created VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('DROP TABLE tweet'); + } +} diff --git a/src/Migrations/Version20190111202343.php b/src/Migrations/Version20190111202343.php new file mode 100644 index 0000000..dff5e8a --- /dev/null +++ b/src/Migrations/Version20190111202343.php @@ -0,0 +1,28 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE tweet DROP tweet_id, CHANGE id id VARCHAR(255) NOT NULL'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE tweet ADD tweet_id INT NOT NULL, CHANGE id id INT AUTO_INCREMENT NOT NULL'); + } +} diff --git a/src/Migrations/Version20190111204428.php b/src/Migrations/Version20190111204428.php new file mode 100644 index 0000000..3b77d9f --- /dev/null +++ b/src/Migrations/Version20190111204428.php @@ -0,0 +1,28 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE tweet ADD retweeted VARCHAR(255) DEFAULT NULL'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE tweet DROP retweeted'); + } +} diff --git a/src/Model/Tweet.php b/src/Model/Tweet.php deleted file mode 100644 index f556258..0000000 --- a/src/Model/Tweet.php +++ /dev/null @@ -1,58 +0,0 @@ -text = $text; - } - - public function setCreated(string $created): void - { - $this->created = $created; - } - - public function setAuthor(string $author): void - { - $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 - { - $this->id = $id; - } - - public function getId(): int - { - return (int) $this->id; - } -} diff --git a/src/Repository/TweetRepository.php b/src/Repository/TweetRepository.php new file mode 100644 index 0000000..5f15114 --- /dev/null +++ b/src/Repository/TweetRepository.php @@ -0,0 +1,60 @@ +createQueryBuilder('t') + ->orderBy('t.created', 'desc') + ->setMaxResults(1) + ->getQuery() + ->getResult(); + + return collect($result)->first(); + } + // /** + // * @return Tweet[] Returns an array of Tweet objects + // */ + /* + public function findByExampleField($value) + { + return $this->createQueryBuilder('t') + ->andWhere('t.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('t.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Tweet + { + return $this->createQueryBuilder('t') + ->andWhere('t.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/src/Service/TweetFetcher.php b/src/Service/TweetFetcher.php index 46b4138..78fa9af 100644 --- a/src/Service/TweetFetcher.php +++ b/src/Service/TweetFetcher.php @@ -2,7 +2,8 @@ namespace App\Service; -use App\Model\Tweet; +use App\Entity\Tweet; +use Doctrine\ORM\EntityManagerInterface; use Tightenco\Collect\Support\Collection; class TweetFetcher @@ -28,9 +29,15 @@ class TweetFetcher 'drupalmeetup', ]; - public function __construct(Codebird $codebird) + /** + * @var \Doctrine\ORM\EntityManagerInterface + */ + private $entityManager; + + public function __construct(Codebird $codebird, EntityManagerInterface $entityManager) { $this->codebird = $codebird; + $this->entityManager = $entityManager; } public function getTweets(): Collection @@ -44,15 +51,21 @@ class TweetFetcher 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); $tweet->setText($status->text); $tweet->setCreated(strtotime($status->created_at)); $tweet->setAuthor($status->user->screen_name); + + $this->entityManager->persist($tweet); }); })->reverse(); + + $this->entityManager->flush(); + + return $tweets; } private function params(): Collection