TagFactory is now receives TagRepository directly instead of EntityManager.

This commit is contained in:
Alexey Skobkin 2017-01-11 19:17:52 +03:00
parent 09cc3741e7
commit 2ac54dc1b4
4 changed files with 25 additions and 17 deletions

View file

@ -4,9 +4,15 @@ namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
class PostRepository extends EntityRepository class PostRepository extends EntityRepository
{ {
public function add(Post $entity)
{
$this->getEntityManager()->persist($entity);
}
public function getPostWithComments($postId) public function getPostWithComments($postId)
{ {
/** @var QueryBuilder $qb */ /** @var QueryBuilder $qb */

View file

@ -7,6 +7,11 @@ use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag;
class TagRepository extends EntityRepository class TagRepository extends EntityRepository
{ {
public function add(Tag $entity)
{
$this->getEntityManager()->persist($entity);
}
/** /**
* @param $text * @param $text
* @return Tag|null * @return Tag|null

View file

@ -86,6 +86,11 @@ services:
class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\CommentRepository class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\CommentRepository
factory: 'doctrine:getRepository' factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment'] 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 # Factories
@ -105,7 +110,7 @@ services:
# Tag factory # Tag factory
app.point.tag_factory: app.point.tag_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\TagFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\TagFactory
arguments: [ '@logger', '@doctrine.orm.entity_manager' ] arguments: [ '@logger', '@app.point.tag_repository' ]
# File factory # File factory
app.point.file_factory: app.point.file_factory:

View file

@ -2,35 +2,27 @@
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs; namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag;
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; use Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository;
class TagFactory class TagFactory
{ {
/**
* @var EntityManagerInterface
*/
private $em;
/** /**
* @var LoggerInterface * @var LoggerInterface
*/ */
private $log; private $logger;
/** /**
* @var EntityRepository * @var TagRepository
*/ */
private $tagRepository; private $tagRepository;
public function __construct(LoggerInterface $log, EntityManagerInterface $em) public function __construct(LoggerInterface $logger, TagRepository $tagRepository)
{ {
$this->log = $log; $this->logger = $logger;
$this->em = $em; $this->tagRepository = $tagRepository;
$this->tagRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Tag');
} }
/** /**
@ -47,7 +39,7 @@ class TagFactory
$tag = $this->createFromString($string); $tag = $this->createFromString($string);
$tags[] = $tag; $tags[] = $tag;
} catch (\Exception $e) { } 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; continue;
} }
} }
@ -60,7 +52,7 @@ class TagFactory
if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) { if (null === ($tag = $this->tagRepository->findOneByLowerText($text))) {
// Creating new tag // Creating new tag
$tag = new Tag($text); $tag = new Tag($text);
$this->em->persist($tag); $this->tagRepository->add($tag);
} }
return $tag; return $tag;