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\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 */

View file

@ -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

View file

@ -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:

View file

@ -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;