PostFactory is now receives PostRepository directly. Still depends on EntityManager because of UoW usage.

This commit is contained in:
Alexey Skobkin 2017-01-11 19:27:08 +03:00
parent 0dc01bef2b
commit 20878ab123
2 changed files with 24 additions and 16 deletions

View file

@ -128,6 +128,7 @@ services:
arguments: arguments:
- '@logger' - '@logger'
- '@doctrine.orm.entity_manager' - '@doctrine.orm.entity_manager'
- '@app.point.post_repository'
- '@app.point.user_factory' - '@app.point.user_factory'
- '@app.point.file_factory' - '@app.point.file_factory'
- '@app.point.comment_factory' - '@app.point.comment_factory'

View file

@ -3,13 +3,13 @@
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs; namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\MetaPost; use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\MetaPost;
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\PostsPage; use Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\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\Exception\Factory\Blog\InvalidDataException; use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidDataException;
use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostRepository;
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidPostDataException; use Skobkin\Bundle\PointToolsBundle\Exception\Factory\Blog\InvalidPostDataException;
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
@ -20,7 +20,7 @@ class PostFactory
/** /**
* @var LoggerInterface * @var LoggerInterface
*/ */
private $log; private $logger;
/** /**
* @var EntityManagerInterface * @var EntityManagerInterface
@ -28,7 +28,7 @@ class PostFactory
private $em; private $em;
/** /**
* @var EntityRepository * @var PostRepository
*/ */
private $postRepository; private $postRepository;
@ -53,15 +53,22 @@ class PostFactory
private $tagFactory; private $tagFactory;
public function __construct(LoggerInterface $log, EntityManagerInterface $em, UserFactory $userFactory, FileFactory $fileFactory, CommentFactory $commentFactory, TagFactory $tagFactory) public function __construct(
{ LoggerInterface $logger,
$this->log = $log; EntityManagerInterface $em,
PostRepository $postRepository,
UserFactory $userFactory,
FileFactory $fileFactory,
CommentFactory $commentFactory,
TagFactory $tagFactory
) {
$this->logger = $logger;
$this->em = $em;
$this->postRepository = $postRepository;
$this->userFactory = $userFactory; $this->userFactory = $userFactory;
$this->fileFactory = $fileFactory; $this->fileFactory = $fileFactory;
$this->commentFactory = $commentFactory; $this->commentFactory = $commentFactory;
$this->tagFactory = $tagFactory; $this->tagFactory = $tagFactory;
$this->em = $em;
$this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post');
} }
/** /**
@ -89,7 +96,7 @@ class PostFactory
$post = $this->createFromDTO($postData); $post = $this->createFromDTO($postData);
$posts[] = $post; $posts[] = $post;
} catch (\Exception $e) { } catch (\Exception $e) {
$this->log->error('Error while processing post DTO', [ $this->logger->error('Error while processing post DTO', [
'post_id' => $postData->getPost()->getId(), 'post_id' => $postData->getPost()->getId(),
'exception' => get_class($e), 'exception' => get_class($e),
'message' => $e->getMessage(), 'message' => $e->getMessage(),
@ -125,21 +132,21 @@ class PostFactory
} }
if (!$postData->getPost()->getAuthor()->getId()) { if (!$postData->getPost()->getAuthor()->getId()) {
$this->log->error('Post author does not contain id', ['post_id' => $postData->getPost()->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()); throw new InvalidPostDataException('Post author does not contain id', $postData->getPost());
} }
try { try {
$user = $this->userFactory->createFromDTO($postData->getPost()->getAuthor()); $user = $this->userFactory->createFromDTO($postData->getPost()->getAuthor());
} catch (\Exception $e) { } catch (\Exception $e) {
$this->log->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()))) { if (null === ($post = $this->postRepository->find($postData->getPost()->getId()))) {
// Creating new post // Creating new post
$post = new Post($postData->getPost()->getId()); $post = new Post($postData->getPost()->getId());
$this->em->persist($post); $this->postRepository->add($post);
} }
// Updating data // Updating data
@ -155,14 +162,14 @@ class PostFactory
try { try {
$this->updatePostTags($post, $postData->getPost()->getTags() ?: []); $this->updatePostTags($post, $postData->getPost()->getTags() ?: []);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->log->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->getPost()->getFiles() ?: []);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->log->error('Error while updating post files'); $this->logger->error('Error while updating post files');
throw $e; throw $e;
} }
@ -239,12 +246,12 @@ class PostFactory
private function validateMetaPost(MetaPost $post) private function validateMetaPost(MetaPost $post)
{ {
if (!$post->getPost()->getId()) { if (!$post->getPost()->getId()) {
$this->log->error('Post DTO contains no id'); $this->logger->error('Post DTO contains no id');
return false; return false;
} }
if (null === $post->getPost()->getAuthor()->getId()) { if (null === $post->getPost()->getAuthor()->getId()) {
$this->log->error('Post DTO contains no valid User DTO.', ['post_id' => $post->getPost()->getId()]); $this->logger->error('Post DTO contains no valid User DTO.', ['post_id' => $post->getPost()->getId()]);
return false; return false;
} }