From 2ac54dc1b4e6c77cd2b0b9a2b8284aaaa9dffef6 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 11 Jan 2017 19:17:52 +0300 Subject: [PATCH] TagFactory is now receives TagRepository directly instead of EntityManager. --- .../Repository/Blogs/PostRepository.php | 6 +++++ .../Repository/Blogs/TagRepository.php | 5 ++++ .../Resources/config/services.yml | 7 +++++- .../Service/Factory/Blogs/TagFactory.php | 24 +++++++------------ 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php index 10c8209..eeae971 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/PostRepository.php @@ -4,9 +4,15 @@ namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; +use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; class PostRepository extends EntityRepository { + public function add(Post $entity) + { + $this->getEntityManager()->persist($entity); + } + public function getPostWithComments($postId) { /** @var QueryBuilder $qb */ diff --git a/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/TagRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/TagRepository.php index a264046..79dcc1a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/TagRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Repository/Blogs/TagRepository.php @@ -7,6 +7,11 @@ use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag; class TagRepository extends EntityRepository { + public function add(Tag $entity) + { + $this->getEntityManager()->persist($entity); + } + /** * @param $text * @return Tag|null diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index 98ff611..c6c0201 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -86,6 +86,11 @@ services: class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\CommentRepository factory: 'doctrine:getRepository' arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment'] + # Tag repository + app.point.tag_repository: + class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository + factory: 'doctrine:getRepository' + arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag'] # Factories @@ -105,7 +110,7 @@ services: # Tag factory app.point.tag_factory: class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\TagFactory - arguments: [ '@logger', '@doctrine.orm.entity_manager' ] + arguments: [ '@logger', '@app.point.tag_repository' ] # File factory app.point.file_factory: diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/TagFactory.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/TagFactory.php index 9ec6904..9337319 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/TagFactory.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Factory/Blogs/TagFactory.php @@ -2,35 +2,27 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs; -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository; use Psr\Log\LoggerInterface; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag; -use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; +use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository; class TagFactory { - /** - * @var EntityManagerInterface - */ - private $em; - /** * @var LoggerInterface */ - private $log; + private $logger; /** - * @var EntityRepository + * @var TagRepository */ private $tagRepository; - public function __construct(LoggerInterface $log, EntityManagerInterface $em) + public function __construct(LoggerInterface $logger, TagRepository $tagRepository) { - $this->log = $log; - $this->em = $em; - $this->tagRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Tag'); + $this->logger = $logger; + $this->tagRepository = $tagRepository; } /** @@ -47,7 +39,7 @@ class TagFactory $tag = $this->createFromString($string); $tags[] = $tag; } catch (\Exception $e) { - $this->log->error('Error while creating tag from DTO', ['tag' => $string, 'message' => $e->getMessage()]); + $this->logger->error('Error while creating tag from DTO', ['tag' => $string, 'message' => $e->getMessage()]); continue; } } @@ -60,7 +52,7 @@ class TagFactory if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) { // Creating new tag $tag = new Tag($text); - $this->em->persist($tag); + $this->tagRepository->add($tag); } return $tag;