Merge branch 'add-doctrine'

This commit is contained in:
Oliver Davies 2019-01-16 00:11:54 +00:00
commit b50d0de494
22 changed files with 2019 additions and 117 deletions

View file

@ -24,3 +24,10 @@ TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_ACCESS_TOKEN=
TWITTER_ACCESS_SECRET=
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
# Configure your db driver and server_version in config/packages/doctrine.yaml
DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name
###< doctrine/doctrine-bundle ###

View file

@ -11,6 +11,7 @@
"symfony/dotenv": "4.2.*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "4.2.*",
"symfony/orm-pack": "^1.0",
"symfony/yaml": "4.2.*",
"tightenco/collect": "^5.7"
},

1585
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -3,4 +3,7 @@
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
];

View file

@ -0,0 +1,29 @@
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App

View file

@ -0,0 +1,5 @@
doctrine_migrations:
dir_name: '%kernel.project_dir%/src/Migrations'
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
namespace: DoctrineMigrations

View file

@ -0,0 +1,32 @@
doctrine:
orm:
auto_generate_proxy_classes: false
metadata_cache_driver:
type: service
id: doctrine.system_cache_provider
query_cache_driver:
type: service
id: doctrine.system_cache_provider
result_cache_driver:
type: service
id: doctrine.result_cache_provider
services:
doctrine.result_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '@doctrine.result_cache_pool'
doctrine.system_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '@doctrine.system_cache_pool'
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system

View file

@ -0,0 +1,3 @@
controllers:
resource: ../../src/Controller/
type: annotation

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;

View file

@ -2,15 +2,15 @@
namespace App\Command;
use App\Model\Tweet;
use App\Entity\Tweet;
use App\Repository\TweetRepository;
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,25 +20,48 @@ class RetweetTweetsCommand extends Command
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();
$this->tweetFetcher = $tweetFetcher;
$this->retweeter = $retweeter;
$this->tweetRepository = $tweetRepository;
$this->entityManager = $entityManager;
}
protected function configure()
{
$this
->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)
{
$this->tweetFetcher->getTweets()->each(function (Tweet $tweet) {
$this->tweetRepository->getUntweetedTweets($input->getOption('number'))->each(function (Tweet $tweet) {
$this->retweeter->retweet($tweet);
$tweet->setRetweeted(time());
$this->entityManager->persist($tweet);
});
$this->entityManager->flush();
}
}

0
src/Entity/.gitignore vendored Normal file
View file

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

0
src/Migrations/.gitignore vendored Normal file
View file

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

0
src/Repository/.gitignore vendored Normal file
View file

View file

@ -0,0 +1,58 @@
<?php
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)
* @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();
}
public function getUntweetedTweets(int $limit): Collection
{
return collect(
$this->createQueryBuilder('t')
->where('t.retweeted is NULL')
->orderBy('t.created', 'asc')
->setMaxResults($limit)
->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,7 @@
namespace App\Service;
use App\Model\Tweet;
use App\Entity\Tweet;
class Retweeter
{

View file

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

View file

@ -2,9 +2,75 @@
"composer/installers": {
"version": "v1.6.0"
},
"doctrine/annotations": {
"version": "1.0",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.0",
"ref": "cb4152ebcadbe620ea2261da1a1c5a9b8cea7672"
}
},
"doctrine/cache": {
"version": "v1.8.0"
},
"doctrine/collections": {
"version": "v1.5.0"
},
"doctrine/common": {
"version": "v2.10.0"
},
"doctrine/dbal": {
"version": "v2.9.2"
},
"doctrine/doctrine-bundle": {
"version": "1.6",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.6",
"ref": "453e89b78ded666f351617baca5ae40d20622351"
}
},
"doctrine/doctrine-cache-bundle": {
"version": "1.3.5"
},
"doctrine/doctrine-migrations-bundle": {
"version": "1.2",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.2",
"ref": "c1431086fec31f17fbcfe6d6d7e92059458facc1"
}
},
"doctrine/event-manager": {
"version": "v1.0.0"
},
"doctrine/inflector": {
"version": "v1.3.0"
},
"doctrine/instantiator": {
"version": "1.1.0"
},
"doctrine/lexer": {
"version": "v1.0.1"
},
"doctrine/migrations": {
"version": "v1.8.1"
},
"doctrine/orm": {
"version": "v2.6.3"
},
"doctrine/persistence": {
"version": "v1.1.0"
},
"doctrine/reflection": {
"version": "v1.0.0"
},
"jdorn/sql-formatter": {
"version": "v1.2.17"
},
"josephlavin/tap": {
"version": "v1.0.0"
},
@ -14,6 +80,12 @@
"nikic/php-parser": {
"version": "v4.1.1"
},
"ocramius/package-versions": {
"version": "1.3.0"
},
"ocramius/proxy-manager": {
"version": "2.2.2"
},
"psr/cache": {
"version": "1.0.1"
},
@ -50,6 +122,9 @@
"symfony/dependency-injection": {
"version": "v4.2.1"
},
"symfony/doctrine-bridge": {
"version": "v4.2.2"
},
"symfony/dotenv": {
"version": "v4.2.1"
},
@ -95,6 +170,9 @@
"ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f"
}
},
"symfony/orm-pack": {
"version": "v1.0.5"
},
"symfony/polyfill-mbstring": {
"version": "v1.10.0"
},
@ -121,5 +199,11 @@
},
"tightenco/collect": {
"version": "v5.7.20"
},
"zendframework/zend-code": {
"version": "3.3.1"
},
"zendframework/zend-eventmanager": {
"version": "3.2.1"
}
}