From 3d481f744f255d55fd5b73a19c5ef70f9125405a Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Sun, 15 Jan 2017 20:33:43 +0300 Subject: [PATCH] Post and PostFactory refactored. --- .../DataFixtures/ORM/LoadPostData.php | 10 +--- .../PointToolsBundle/Entity/Blogs/Post.php | 26 ++------- .../Service/Factory/Blogs/PostFactory.php | 54 ++++++++++++------- 3 files changed, 40 insertions(+), 50 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadPostData.php b/src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadPostData.php index be0e135..4313e9b 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadPostData.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DataFixtures/ORM/LoadPostData.php @@ -15,21 +15,15 @@ class LoadPostData extends AbstractFixture implements OrderedFixtureInterface /** @var User $testUser */ $testUser = $this->getReference('test_user_99999'); - $longPost = (new Post('longpost')) - ->setAuthor($testUser) - ->setCreatedAt(new \DateTime()) + $longPost = (new Post('longpost', $testUser, new \DateTime(), Post::TYPE_POST)) ->setText('Test post with many comments') ->setPrivate(false) - ->setType(Post::TYPE_POST) ->setDeleted(false) ; - $shortPost = (new Post('shortpost')) - ->setAuthor($testUser) - ->setCreatedAt(new \DateTime()) + $shortPost = (new Post('shortpost', $testUser, new \DateTime(), Post::TYPE_POST)) ->setText('Test short post') ->setPrivate(false) - ->setType(Post::TYPE_POST) ->setDeleted(false) ; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php index ec4a827..78af4cb 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/Blogs/Post.php @@ -103,9 +103,12 @@ class Post private $comments; - public function __construct(string $id) + public function __construct(string $id, User $author, \DateTime $createdAt, string $type) { $this->id = $id; + $this->author = $author; + $this->createdAt = $createdAt; + $this->type = $type; $this->files = new ArrayCollection(); $this->postTags = new ArrayCollection(); @@ -137,13 +140,6 @@ class Post return $this->text; } - public function setCreatedAt(\DateTime $createdAt): self - { - $this->createdAt = $createdAt; - - return $this; - } - public function getCreatedAt(): \DateTime { return $this->createdAt; @@ -154,13 +150,6 @@ class Post return $this->updatedAt; } - public function setType(string $type): self - { - $this->type = $type; - - return $this; - } - public function getType(): string { return $this->type; @@ -171,13 +160,6 @@ class Post return $this->author; } - public function setAuthor(User $author): self - { - $this->author = $author; - - return $this; - } - public function addFile(File $files): self { $this->files[] = $files; diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php index 547de83..8ecbc67 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/PostFactory.php @@ -5,9 +5,11 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; 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\Entity\Blogs\Post; 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\Repository\Blogs\PostRepository; use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException; @@ -68,6 +70,8 @@ class PostFactory extends AbstractFactory /** * Creates posts and return status of new insertions * + * @todo refactor + * * @throws InvalidResponseException */ public function createFromPageDTO(PostsPage $page): bool @@ -82,7 +86,7 @@ class PostFactory extends AbstractFactory $hasNew = true; } - $post = $this->createFromDTO($postData); + $post = $this->findOrCreateFromDTOWithTagsAndFiles($postData); $posts[] = $post; } catch (\Exception $e) { $this->logger->error('Error while processing post DTO', [ @@ -109,43 +113,32 @@ class PostFactory extends AbstractFactory * * @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'); } + $postData = $metaPost->getPost(); + try { - $user = $this->userFactory->findOrCreateFromDTO($postData->getPost()->getAuthor()); + $author = $this->userFactory->findOrCreateFromDTO($metaPost->getPost()->getAuthor()); } catch (\Exception $e) { $this->logger->error('Error while creating user from DTO'); throw $e; } - if (null === ($post = $this->postRepository->find($postData->getPost()->getId()))) { - // 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()) - ; + $post = $this->findOrCreateFromDto($postData, $author); try { - $this->updatePostTags($post, $postData->getPost()->getTags() ?: []); + $this->updatePostTags($post, $postData->getTags() ?: []); } catch (\Exception $e) { $this->logger->error('Error while updating post tags'); throw $e; } try { - $this->updatePostFiles($post, $postData->getPost()->getFiles() ?: []); + $this->updatePostFiles($post, $postData->getFiles() ?: []); } catch (\Exception $e) { $this->logger->error('Error while updating post files'); throw $e; @@ -154,6 +147,27 @@ class PostFactory extends AbstractFactory 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 string[] $tagsStrings