Ported AccountFactory.

This commit is contained in:
Alexey Skobkin 2023-03-28 22:29:19 +03:00
parent 5e2f8787ae
commit fd8161c9e1
No known key found for this signature in database
GPG key ID: 5D5CEF6F221278E7
2 changed files with 38 additions and 40 deletions

View file

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

View 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;
}
}