Add Tweet Entity

This commit is contained in:
Oliver Davies 2019-01-11 21:15:43 +00:00
parent 847efa466a
commit 1f3eada5bb
8 changed files with 273 additions and 62 deletions

View file

@ -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;

112
src/Entity/Tweet.php Normal file
View file

@ -0,0 +1,112 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TweetRepository")
*/
class Tweet
{
/**
* @ORM\Id()
* @ORM\Column(type="string")
*/
private $id;
/**
* @ORM\Column(type="integer")
*/
// private $tweet_id;
/**
* @ORM\Column(type="string", length=255)
*/
private $text;
/**
* @ORM\Column(type="string", length=255)
*/
private $author;
/**
* @ORM\Column(type="string", length=255)
*/
private $created;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $retweeted;
public function getId(): ?int
{
return $this->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;
}
}

View file

@ -0,0 +1,28 @@
<?php declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190111201746 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() 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('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');
}
}

View file

@ -0,0 +1,28 @@
<?php declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190111202343 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() 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 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');
}
}

View file

@ -0,0 +1,28 @@
<?php declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190111204428 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() 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 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');
}
}

View file

@ -1,58 +0,0 @@
<?php
namespace App\Model;
class Tweet
{
/** @var string */
private $text;
/** @var string */
private $created;
/** @var string */
private $author;
/** @var int */
private $id;
public function setText(string $text): void
{
$this->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;
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace App\Repository;
use App\Entity\Tweet;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* @method Tweet|null find($id, $lockMode = null, $lockVersion = null)
* @method Tweet|null findOneBy(array $criteria, array $orderBy = null)
* @method Tweet[] findAll()
* @method Tweet[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TweetRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Tweet::class);
}
public function findNewestTweet(): ?Tweet
{
$result = $this->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()
;
}
*/
}

View file

@ -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