WIP: Symfony 6 project remake #2
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace src\PointToolsBundle\Service\Factory\Telegram;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use src\PointToolsBundle\Entity\Telegram\Account;
|
||||
use src\PointToolsBundle\Repository\Telegram\AccountRepository;
|
||||
use src\PointToolsBundle\Service\Factory\AbstractFactory;
|
||||
use unreal4u\TelegramAPI\Telegram\Types\Message;
|
||||
|
||||
class AccountFactory extends AbstractFactory
|
||||
{
|
||||
/** @var AccountRepository */
|
||||
private $accountRepo;
|
||||
|
||||
|
||||
public function __construct(LoggerInterface $logger, AccountRepository $accountRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
$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->accountRepo->add($account);
|
||||
}
|
||||
|
||||
// Setting/updating account data
|
||||
$account->updateFromMessageData(
|
||||
$message->from->first_name,
|
||||
$message->from->last_name,
|
||||
$message->from->username,
|
||||
$message->chat->id
|
||||
);
|
||||
|
||||
return $account;
|
||||
}
|
||||
}
|
38
src/Factory/Telegram/AccountFactory.php
Normal file
38
src/Factory/Telegram/AccountFactory.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Factory\Telegram;
|
||||
|
||||
use App\Factory\AbstractFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use App\Entity\Telegram\Account;
|
||||
use App\Repository\Telegram\AccountRepository;
|
||||
use unreal4u\Telegram\Types\Message;
|
||||
|
||||
class AccountFactory extends AbstractFactory
|
||||
{
|
||||
public function __construct(
|
||||
LoggerInterface $logger,
|
||||
private readonly AccountRepository $accountRepository,
|
||||
) {
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
public function findOrCreateFromMessage(Message $message): Account
|
||||
{
|
||||
if (null === $account = $this->accountRepository->findOneBy(['id' => $message->from->id])) {
|
||||
$account = new Account($message->from->id);
|
||||
$this->accountRepository->save($account);
|
||||
}
|
||||
|
||||
// Setting/updating account data
|
||||
$account->updateFromMessageData(
|
||||
$message->from->first_name,
|
||||
$message->from->last_name,
|
||||
$message->from->username,
|
||||
$message->chat->id
|
||||
);
|
||||
|
||||
return $account;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue