Post and PostFactory refactored.
This commit is contained in:
parent
1d88ccb6a3
commit
3d481f744f
|
@ -15,21 +15,15 @@ class LoadPostData extends AbstractFixture implements OrderedFixtureInterface
|
||||||
/** @var User $testUser */
|
/** @var User $testUser */
|
||||||
$testUser = $this->getReference('test_user_99999');
|
$testUser = $this->getReference('test_user_99999');
|
||||||
|
|
||||||
$longPost = (new Post('longpost'))
|
$longPost = (new Post('longpost', $testUser, new \DateTime(), Post::TYPE_POST))
|
||||||
->setAuthor($testUser)
|
|
||||||
->setCreatedAt(new \DateTime())
|
|
||||||
->setText('Test post with many comments')
|
->setText('Test post with many comments')
|
||||||
->setPrivate(false)
|
->setPrivate(false)
|
||||||
->setType(Post::TYPE_POST)
|
|
||||||
->setDeleted(false)
|
->setDeleted(false)
|
||||||
;
|
;
|
||||||
|
|
||||||
$shortPost = (new Post('shortpost'))
|
$shortPost = (new Post('shortpost', $testUser, new \DateTime(), Post::TYPE_POST))
|
||||||
->setAuthor($testUser)
|
|
||||||
->setCreatedAt(new \DateTime())
|
|
||||||
->setText('Test short post')
|
->setText('Test short post')
|
||||||
->setPrivate(false)
|
->setPrivate(false)
|
||||||
->setType(Post::TYPE_POST)
|
|
||||||
->setDeleted(false)
|
->setDeleted(false)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
|
@ -103,9 +103,12 @@ class Post
|
||||||
private $comments;
|
private $comments;
|
||||||
|
|
||||||
|
|
||||||
public function __construct(string $id)
|
public function __construct(string $id, User $author, \DateTime $createdAt, string $type)
|
||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
|
$this->author = $author;
|
||||||
|
$this->createdAt = $createdAt;
|
||||||
|
$this->type = $type;
|
||||||
|
|
||||||
$this->files = new ArrayCollection();
|
$this->files = new ArrayCollection();
|
||||||
$this->postTags = new ArrayCollection();
|
$this->postTags = new ArrayCollection();
|
||||||
|
@ -137,13 +140,6 @@ class Post
|
||||||
return $this->text;
|
return $this->text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setCreatedAt(\DateTime $createdAt): self
|
|
||||||
{
|
|
||||||
$this->createdAt = $createdAt;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCreatedAt(): \DateTime
|
public function getCreatedAt(): \DateTime
|
||||||
{
|
{
|
||||||
return $this->createdAt;
|
return $this->createdAt;
|
||||||
|
@ -154,13 +150,6 @@ class Post
|
||||||
return $this->updatedAt;
|
return $this->updatedAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setType(string $type): self
|
|
||||||
{
|
|
||||||
$this->type = $type;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getType(): string
|
public function getType(): string
|
||||||
{
|
{
|
||||||
return $this->type;
|
return $this->type;
|
||||||
|
@ -171,13 +160,6 @@ class Post
|
||||||
return $this->author;
|
return $this->author;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setAuthor(User $author): self
|
|
||||||
{
|
|
||||||
$this->author = $author;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addFile(File $files): self
|
public function addFile(File $files): self
|
||||||
{
|
{
|
||||||
$this->files[] = $files;
|
$this->files[] = $files;
|
||||||
|
|
|
@ -5,9 +5,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Skobkin\Bundle\PointToolsBundle\DTO\Api\MetaPost;
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\MetaPost;
|
||||||
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Post as PostDTO;
|
||||||
use Skobkin\Bundle\PointToolsBundle\DTO\Api\PostsPage;
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\PostsPage;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
|
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\PostTag;
|
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\PostTag;
|
||||||
|
use Skobkin\Bundle\PointToolsBundle\Entity\User;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidDataException;
|
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidDataException;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
|
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
|
||||||
use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException;
|
use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException;
|
||||||
|
@ -68,6 +70,8 @@ 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
|
||||||
|
@ -82,7 +86,7 @@ class PostFactory extends AbstractFactory
|
||||||
$hasNew = true;
|
$hasNew = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$post = $this->createFromDTO($postData);
|
$post = $this->findOrCreateFromDTOWithTagsAndFiles($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', [
|
||||||
|
@ -109,43 +113,32 @@ class PostFactory extends AbstractFactory
|
||||||
*
|
*
|
||||||
* @throws InvalidDataException
|
* @throws InvalidDataException
|
||||||
*/
|
*/
|
||||||
private function createFromDTO(MetaPost $postData): Post
|
public function findOrCreateFromDTOWithTagsAndFiles(MetaPost $metaPost): Post
|
||||||
{
|
{
|
||||||
if (!$postData->isValid()) {
|
if (!$metaPost->isValid()) {
|
||||||
throw new InvalidDataException('Invalid post data');
|
throw new InvalidDataException('Invalid post data');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$postData = $metaPost->getPost();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$user = $this->userFactory->findOrCreateFromDTO($postData->getPost()->getAuthor());
|
$author = $this->userFactory->findOrCreateFromDTO($metaPost->getPost()->getAuthor());
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->logger->error('Error while creating user from DTO');
|
$this->logger->error('Error while creating user from DTO');
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === ($post = $this->postRepository->find($postData->getPost()->getId()))) {
|
$post = $this->findOrCreateFromDto($postData, $author);
|
||||||
// Creating new post
|
|
||||||
$post = new Post($postData->getPost()->getId());
|
|
||||||
$this->postRepository->add($post);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Updating data
|
|
||||||
$post
|
|
||||||
->setAuthor($user)
|
|
||||||
->setCreatedAt((new \DateTime($postData->getPost()->getCreated())) ?: null)
|
|
||||||
->setType($postData->getPost()->getType() ?: Post::TYPE_POST)
|
|
||||||
->setText($postData->getPost()->getText())
|
|
||||||
->setPrivate($postData->getPost()->getPrivate())
|
|
||||||
;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->updatePostTags($post, $postData->getPost()->getTags() ?: []);
|
$this->updatePostTags($post, $postData->getTags() ?: []);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->logger->error('Error while updating post tags');
|
$this->logger->error('Error while updating post tags');
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->updatePostFiles($post, $postData->getPost()->getFiles() ?: []);
|
$this->updatePostFiles($post, $postData->getFiles() ?: []);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->logger->error('Error while updating post files');
|
$this->logger->error('Error while updating post files');
|
||||||
throw $e;
|
throw $e;
|
||||||
|
@ -154,6 +147,27 @@ class PostFactory extends AbstractFactory
|
||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function findOrCreateFromDto(PostDTO $postData, User $author): Post
|
||||||
|
{
|
||||||
|
if (null === ($post = $this->postRepository->find($postData->getId()))) {
|
||||||
|
// Creating new post
|
||||||
|
$post = new Post(
|
||||||
|
$postData->getId(),
|
||||||
|
$author,
|
||||||
|
new \DateTime($postData->getCreated()),
|
||||||
|
$postData->getType() ?: Post::TYPE_POST
|
||||||
|
);
|
||||||
|
$this->postRepository->add($post);
|
||||||
|
}
|
||||||
|
|
||||||
|
$post
|
||||||
|
->setText($postData->getText())
|
||||||
|
->setPrivate($postData->getPrivate())
|
||||||
|
;
|
||||||
|
|
||||||
|
return $post;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Post $post
|
* @param Post $post
|
||||||
* @param string[] $tagsStrings
|
* @param string[] $tagsStrings
|
||||||
|
|
Loading…
Reference in a new issue