AccountFactory is now receives AccountRepository directly instead of EntityManager.

This commit is contained in:
Alexey Skobkin 2017-01-11 19:31:12 +03:00
parent 20878ab123
commit 592d9592c6
3 changed files with 18 additions and 14 deletions

View file

@ -3,9 +3,15 @@
namespace Skobkin\Bundle\PointToolsBundle\Repository\Telegram; namespace Skobkin\Bundle\PointToolsBundle\Repository\Telegram;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
class AccountRepository extends EntityRepository class AccountRepository extends EntityRepository
{ {
public function add(Account $entity)
{
$this->getEntityManager()->persist($entity);
}
/** /**
* @todo remove if not used * @todo remove if not used
* *
@ -14,7 +20,7 @@ class AccountRepository extends EntityRepository
* @param int|null $limit * @param int|null $limit
* @param int|null $offset * @param int|null $offset
* *
* @return array * @return Account[]
*/ */
public function findLinkedAccountsBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array public function findLinkedAccountsBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array
{ {

View file

@ -96,6 +96,11 @@ services:
class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository
factory: 'doctrine:getRepository' factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\File'] arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Blogs\File']
# Telegram Account repository
app.point.telegram_account_repository:
class: Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository
factory: 'doctrine:getRepository'
arguments: ['Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account']
# Factories # Factories
@ -137,7 +142,7 @@ services:
# Telegram accounts factory # Telegram accounts factory
app.telegram.telegram_account_factory: app.telegram.telegram_account_factory:
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory
arguments: ['@doctrine.orm.entity_manager'] arguments: ['@app.point.telegram_account_repository']
# Custom Markdown parser # Custom Markdown parser

View file

@ -2,35 +2,28 @@
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram; namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account; use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
use Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository;
use unreal4u\TelegramAPI\Telegram\Types\Message; use unreal4u\TelegramAPI\Telegram\Types\Message;
class AccountFactory class AccountFactory
{ {
/** /**
* @var EntityManagerInterface * @var AccountRepository
*/
private $em;
/**
* @var EntityRepository
*/ */
private $accountRepo; private $accountRepo;
public function __construct(EntityManagerInterface $em) public function __construct(AccountRepository $accountRepository)
{ {
$this->em = $em; $this->accountRepo = $accountRepository;
$this->accountRepo = $em->getRepository('SkobkinPointToolsBundle:Telegram\Account');
} }
public function findOrCreateFromMessage(Message $message): Account public function findOrCreateFromMessage(Message $message): Account
{ {
if (null === $account = $this->accountRepo->findOneBy(['id' => $message->from->id])) { if (null === $account = $this->accountRepo->findOneBy(['id' => $message->from->id])) {
$account = new Account($message->from->id); $account = new Account($message->from->id);
$this->em->persist($account); $this->accountRepo->add($account);
} }
// Setting/updating account data // Setting/updating account data