AccountFactory is now receives AccountRepository directly instead of EntityManager.
This commit is contained in:
parent
20878ab123
commit
592d9592c6
|
@ -3,9 +3,15 @@
|
|||
namespace Skobkin\Bundle\PointToolsBundle\Repository\Telegram;
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
|
||||
|
||||
class AccountRepository extends EntityRepository
|
||||
{
|
||||
public function add(Account $entity)
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo remove if not used
|
||||
*
|
||||
|
@ -14,7 +20,7 @@ class AccountRepository extends EntityRepository
|
|||
* @param int|null $limit
|
||||
* @param int|null $offset
|
||||
*
|
||||
* @return array
|
||||
* @return Account[]
|
||||
*/
|
||||
public function findLinkedAccountsBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array
|
||||
{
|
||||
|
|
|
@ -96,6 +96,11 @@ services:
|
|||
class: Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository
|
||||
factory: 'doctrine:getRepository'
|
||||
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
|
||||
|
@ -137,7 +142,7 @@ services:
|
|||
# Telegram accounts factory
|
||||
app.telegram.telegram_account_factory:
|
||||
class: Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory
|
||||
arguments: ['@doctrine.orm.entity_manager']
|
||||
arguments: ['@app.point.telegram_account_repository']
|
||||
|
||||
|
||||
# Custom Markdown parser
|
||||
|
|
|
@ -2,35 +2,28 @@
|
|||
|
||||
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\Repository\Telegram\AccountRepository;
|
||||
use unreal4u\TelegramAPI\Telegram\Types\Message;
|
||||
|
||||
class AccountFactory
|
||||
{
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var EntityRepository
|
||||
* @var AccountRepository
|
||||
*/
|
||||
private $accountRepo;
|
||||
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
public function __construct(AccountRepository $accountRepository)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->accountRepo = $em->getRepository('SkobkinPointToolsBundle:Telegram\Account');
|
||||
$this->accountRepo = $accountRepository;
|
||||
}
|
||||
|
||||
public function findOrCreateFromMessage(Message $message): Account
|
||||
{
|
||||
if (null === $account = $this->accountRepo->findOneBy(['id' => $message->from->id])) {
|
||||
$account = new Account($message->from->id);
|
||||
$this->em->persist($account);
|
||||
$this->accountRepo->add($account);
|
||||
}
|
||||
|
||||
// Setting/updating account data
|
||||
|
|
Loading…
Reference in a new issue