PostFactory minor fixes.

This commit is contained in:
Alexey Skobkin 2017-01-15 17:51:40 +03:00
parent 7c5f1f1ee8
commit 6e7280938d

View file

@ -10,7 +10,6 @@ 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\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\Factory\Blog\InvalidPostDataException;
use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException; use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory; use Skobkin\Bundle\PointToolsBundle\Service\Factory\AbstractFactory;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory; use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory;
@ -69,10 +68,6 @@ class PostFactory extends AbstractFactory
/** /**
* Creates posts and return status of new insertions * Creates posts and return status of new insertions
* *
* @param PostsPage $page
*
* @return bool
*
* @throws InvalidResponseException * @throws InvalidResponseException
*/ */
public function createFromPageDTO(PostsPage $page): bool public function createFromPageDTO(PostsPage $page): bool
@ -110,25 +105,16 @@ class PostFactory extends AbstractFactory
} }
/** /**
* @todo Implement full post with comments processing * @todo Implement full post processing with comments
*
* @param MetaPost $postData
*
* @return Post
* *
* @throws InvalidDataException * @throws InvalidDataException
*/ */
private function createFromDTO(MetaPost $postData): Post private function createFromDTO(MetaPost $postData): Post
{ {
if (!$this->validateMetaPost($postData)) { if (!$postData->isValid()) {
throw new InvalidDataException('Invalid post data'); throw new InvalidDataException('Invalid post data');
} }
if (!$postData->getPost()->getAuthor()->getId()) {
$this->logger->error('Post author does not contain id', ['post_id' => $postData->getPost()->getId()]);
throw new InvalidPostDataException('Post author does not contain id', $postData->getPost());
}
try { try {
$user = $this->userFactory->findOrCreateFromDTO($postData->getPost()->getAuthor()); $user = $this->userFactory->findOrCreateFromDTO($postData->getPost()->getAuthor());
} catch (\Exception $e) { } catch (\Exception $e) {
@ -146,8 +132,7 @@ class PostFactory extends AbstractFactory
$post $post
->setAuthor($user) ->setAuthor($user)
->setCreatedAt((new \DateTime($postData->getPost()->getCreated())) ?: null) ->setCreatedAt((new \DateTime($postData->getPost()->getCreated())) ?: null)
// @fixme Need bugfix for point API (type is not showing now) ->setType($postData->getPost()->getType() ?: Post::TYPE_POST)
//->setType($postData->getPost()->getType())
->setText($postData->getPost()->getText()) ->setText($postData->getPost()->getText())
->setPrivate($postData->getPost()->getPrivate()) ->setPrivate($postData->getPost()->getPrivate())
; ;
@ -235,19 +220,4 @@ class PostFactory extends AbstractFactory
} }
} }
} }
private function validateMetaPost(MetaPost $post): bool
{
if (!$post->getPost()->getId()) {
$this->logger->error('Post DTO contains no id');
return false;
}
if (null === $post->getPost()->getAuthor()->getId()) {
$this->logger->error('Post DTO contains no valid User DTO.', ['post_id' => $post->getPost()->getId()]);
return false;
}
return true;
}
} }