DB schema, entities and factories small refactoring.

This commit is contained in:
Alexey Skobkin 2017-11-06 05:36:32 +03:00
parent e970a55f24
commit 6c321a9fc6
6 changed files with 65 additions and 45 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Schema refactoring
*/
class Version20171106023155 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('ALTER INDEX subscriptions.author_idx RENAME TO idx_subscription_author');
$this->addSql('ALTER INDEX subscriptions.subscriber_idx RENAME TO idx_subscription_subscriber');
$this->addSql('ALTER INDEX subscriptions.date_idx RENAME TO idx_subscription_date');
$this->addSql('ALTER TABLE posts.posts_tags DROP CONSTRAINT FK_7870CC82BAD26311');
$this->addSql('ALTER TABLE posts.posts_tags ADD CONSTRAINT FK_7870CC82BAD26311 FOREIGN KEY (tag_id) REFERENCES posts.tags (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('DROP INDEX posts.idx_tag_text');
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('CREATE INDEX idx_tag_text ON posts.tags (text)');
$this->addSql('ALTER TABLE posts.posts_tags DROP CONSTRAINT fk_7870cc82bad26311');
$this->addSql('ALTER TABLE posts.posts_tags ADD CONSTRAINT fk_7870cc82bad26311 FOREIGN KEY (tag_id) REFERENCES posts.tags (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER INDEX subscriptions.idx_subscription_author RENAME TO author_idx');
$this->addSql('ALTER INDEX subscriptions.idx_subscription_date RENAME TO date_idx');
$this->addSql('ALTER INDEX subscriptions.idx_subscription_subscriber RENAME TO subscriber_idx');
}
}

View File

@ -6,7 +6,7 @@ use Doctrine\ORM\Mapping as ORM;
/** /**
* @ORM\Table(name="posts_tags", schema="posts") * @ORM\Table(name="posts_tags", schema="posts")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostTagRepository") * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostTagRepository", readOnly=true)
*/ */
class PostTag class PostTag
{ {
@ -22,7 +22,7 @@ class PostTag
/** /**
* @var Post * @var Post
* *
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post", inversedBy="postTags") * @ORM\ManyToOne(targetEntity="Post", inversedBy="postTags")
* @ORM\JoinColumn(name="post_id", onDelete="CASCADE") * @ORM\JoinColumn(name="post_id", onDelete="CASCADE")
*/ */
private $post; private $post;
@ -30,10 +30,8 @@ class PostTag
/** /**
* @var Tag * @var Tag
* *
* @todo fix SET NULL * @ORM\ManyToOne(targetEntity="Tag", fetch="EAGER")
* * @ORM\JoinColumn(name="tag_id")
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag", fetch="EAGER")
* @ORM\JoinColumn(name="tag_id", onDelete="SET NULL")
*/ */
private $tag; private $tag;
@ -45,9 +43,11 @@ class PostTag
private $text; private $text;
public function __construct(Tag $tag) public function __construct(Post $post, Tag $tag, string $text)
{ {
$this->post = $post;
$this->tag = $tag; $this->tag = $tag;
$this->text = $text;
} }
public function getId(): int public function getId(): int
@ -55,13 +55,6 @@ class PostTag
return $this->id; return $this->id;
} }
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
public function getText(): string public function getText(): string
{ {
return $this->text; return $this->text;
@ -69,22 +62,7 @@ class PostTag
public function getOriginalTagText(): string public function getOriginalTagText(): string
{ {
return $this->tag ? $this->tag->getText() : ''; return $this->tag->getText();
}
/**
* Set post
*
* @todo move to constructor
*
* @param Post $post
* @return PostTag
*/
public function setPost(Post $post = null): self
{
$this->post = $post;
return $this;
} }
public function getPost(): Post public function getPost(): Post

View File

@ -5,9 +5,7 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity\Blogs;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
/** /**
* @ORM\Table(name="tags", schema="posts", indexes={ * @ORM\Table(name="tags", schema="posts")
* @ORM\Index(name="idx_tag_text", columns={"text"})
* })
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository", readOnly=true) * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository", readOnly=true)
*/ */
class Tag class Tag

View File

@ -6,9 +6,9 @@ use Doctrine\ORM\Mapping as ORM;
/** /**
* @ORM\Table(name="log", schema="subscriptions", indexes={ * @ORM\Table(name="log", schema="subscriptions", indexes={
* @ORM\Index(name="author_idx", columns={"author_id"}), * @ORM\Index(name="idx_subscription_author", columns={"author_id"}),
* @ORM\Index(name="subscriber_idx", columns={"subscriber_id"}), * @ORM\Index(name="idx_subscription_subscriber", columns={"subscriber_id"}),
* @ORM\Index(name="date_idx", columns={"date"}) * @ORM\Index(name="idx_subscription_date", columns={"date"})
* }) * })
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository", readOnly=true) * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository", readOnly=true)
*/ */

View File

@ -52,8 +52,6 @@ class PostFactory extends AbstractFactory
/** /**
* Creates posts and return status of new insertions * Creates posts and return status of new insertions
* *
* @todo refactor
*
* @throws InvalidResponseException * @throws InvalidResponseException
*/ */
public function createFromPageDTO(PostsPage $page): bool public function createFromPageDTO(PostsPage $page): bool
@ -68,7 +66,7 @@ class PostFactory extends AbstractFactory
$hasNew = true; $hasNew = true;
} }
$post = $this->findOrCreateFromDTOWithTagsAndFiles($postData); $post = $this->findOrCreateFromDtoWithContent($postData);
$posts[] = $post; $posts[] = $post;
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error while processing post DTO', [ $this->logger->error('Error while processing post DTO', [
@ -82,6 +80,7 @@ class PostFactory extends AbstractFactory
} }
foreach ($posts as $post) { foreach ($posts as $post) {
// @todo probably refactor?
if ($this->em->getUnitOfWork()->isScheduledForInsert($post)) { if ($this->em->getUnitOfWork()->isScheduledForInsert($post)) {
$hasNew = true; $hasNew = true;
} }
@ -91,11 +90,13 @@ class PostFactory extends AbstractFactory
} }
/** /**
* @todo Implement full post processing with comments * Create full post with tags, files and comments
*
* @todo Implement comments
* *
* @throws InvalidDataException * @throws InvalidDataException
*/ */
public function findOrCreateFromDTOWithTagsAndFiles(MetaPost $metaPost): Post public function findOrCreateFromDtoWithContent(MetaPost $metaPost): Post
{ {
if (!$metaPost->isValid()) { if (!$metaPost->isValid()) {
throw new InvalidDataException('Invalid post data'); throw new InvalidDataException('Invalid post data');
@ -179,9 +180,7 @@ class PostFactory extends AbstractFactory
// Adding missing tags // Adding missing tags
foreach ($tags as $tag) { foreach ($tags as $tag) {
if (!array_key_exists(mb_strtolower($tag->getText()), $oldTagsHash)) { if (!array_key_exists(mb_strtolower($tag->getText()), $oldTagsHash)) {
$tmpPostTag = (new PostTag($tag)) $tmpPostTag = new PostTag($post, $tag, $tagStringsHash[mb_strtolower($tag->getText())]);
->setText($tagStringsHash[mb_strtolower($tag->getText())])
;
$post->addPostTag($tmpPostTag); $post->addPostTag($tmpPostTag);
} }
} }

View File

@ -16,6 +16,7 @@ class TagFactory extends AbstractFactory
public function __construct(LoggerInterface $logger, TagRepository $tagRepository) public function __construct(LoggerInterface $logger, TagRepository $tagRepository)
{ {
parent::__construct($logger); parent::__construct($logger);
$this->tagRepository = $tagRepository; $this->tagRepository = $tagRepository;
} }
@ -41,7 +42,7 @@ class TagFactory extends AbstractFactory
return $tags; return $tags;
} }
public function createFromString(string $text): Tag private function createFromString(string $text): Tag
{ {
if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) { if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) {
// Creating new tag // Creating new tag