src/BillingBundle/EventListeners/MsgEventListener.php line 53

Open in your IDE?
  1. <?php
  2. /** @noinspection PhpUnused */
  3. /*
  4.  * This file is part of the SynergyBot project.
  5.  *
  6.  * For the full copyright and license information,
  7.  * please read the LICENSE.md file that was distributed with this source code.
  8.  *
  9.  * The SymfonyBot project - inspiring people to chat!
  10.  *
  11.  * Copyright (c) 2022.
  12.  */
  13. declare(strict_types=1);
  14. /*
  15.  * This file is part of the SynergyBot project.
  16.  *
  17.  * For the full copyright and license information,
  18.  * please read the LICENSE.md file that was distributed with this source code.
  19.  *
  20.  * The SymfonyBot project - inspiring people to chat!
  21.  *
  22.  * Copyright (c) 2022.
  23.  */
  24. namespace App\BillingBundle\EventListeners;
  25. use App\BillingBundle\Config\OperationType;
  26. use App\BillingBundle\Entity\Account;
  27. use App\BillingBundle\Repository\AccountRepository;
  28. use App\BillingBundle\Repository\OperationRepository;
  29. use App\BillingBundle\Services\BillingService;
  30. use App\ChannelBundle\Event\OnChannelMsgDelivered;
  31. use App\ChannelBundle\Event\OnChannelMsgFail;
  32. use Doctrine\DBAL\LockMode;
  33. use Doctrine\ORM\EntityManagerInterface;
  34. use Psr\Log\LoggerInterface;
  35. class MsgEventListener
  36. {
  37.     public function __construct(
  38.         private readonly EntityManagerInterface $em,
  39.         private readonly AccountRepository $accountRepository,
  40.         private readonly OperationRepository $operationRepository,
  41.         private readonly LoggerInterface $logger,
  42.         private readonly BillingService $billingService)
  43.     {
  44.     }
  45.     public function onChannelMsgDelivered(OnChannelMsgDelivered $onMsgDelivered)
  46.     {
  47.         $msg $onMsgDelivered->getMsg();
  48.         $channel $msg->getChannel();
  49.         $bAccount $this->billingService->getUserBilling($channel->getUser());
  50.         $operation $this->operationRepository->findOneByAccountAndBusinessMsgAndType($bAccount$msgOperationType::BLOCK);
  51.         if (!$operation) {
  52.             return;
  53.         }
  54.         /** @var Account $bAccountPartner */
  55.         $bAccountPartner $this->accountRepository->findOneBy([
  56.             'isPartner' => true,
  57.         ]);
  58.         $operationType OperationType::PAY;
  59.         $this->em->beginTransaction();
  60.         $this->em->lock($bAccountLockMode::PESSIMISTIC_WRITE);
  61.         $this->em->lock($bAccountPartnerLockMode::PESSIMISTIC_WRITE);
  62.         try {
  63.             $bAccountPartner->increaseBalance($operation->getAmount());
  64.             $this->em->flush();
  65.             $this->em->commit();
  66.             $operation
  67.                 ->setType($operationType)
  68.                 ->setExternalId($msg->getRequestId())
  69.                 ->setActivatedAt(new \DateTime());
  70.             $this->em->flush();
  71.         } catch (\Exception $exception) {
  72.             $this->logger->error($exception->getMessage(), [$exception]);
  73.             $this->em->rollback();
  74.         }
  75.     }
  76.     public function onChannelMsgFail(OnChannelMsgFail $onMsgFail)
  77.     {
  78.         $msg $onMsgFail->getMsg();
  79.         $channel $msg->getChannel();
  80.         $msgPattern $msg->getMsgPattern();
  81.         if (!$msgPattern) {
  82.             return;
  83.         }
  84.         $bAccount $this->billingService->getUserBilling($channel->getUser());
  85.         $operation $this->operationRepository->findOneByAccountAndMsgAndType($bAccount$msgOperationType::BLOCK);
  86.         if (!$operation) {
  87.             return;
  88.         }
  89.         $bAccountPartner $this->accountRepository->findOneBy([
  90.             'isPartner' => true,
  91.         ]);
  92.         if (!$operation->isFree()) {
  93.             $this->em->beginTransaction();
  94.             $this->em->lock($bAccountLockMode::PESSIMISTIC_WRITE);
  95.             $this->em->lock($bAccountPartnerLockMode::PESSIMISTIC_WRITE);
  96.             try {
  97.                 $bAccount->increaseBalance($operation->getAmount());
  98.                 $bAccountPartner->reduceBalance($operation->getAmount());
  99.                 $this->em->flush();
  100.                 $this->em->commit();
  101.             } catch (\Exception $exception) {
  102.                 $this->logger->error($exception->getMessage(), [$exception]);
  103.                 $this->em->rollback();
  104.             }
  105.         }
  106.         $operation
  107.             ->setAmount(!$operation->isFree() ? $operation->getAmount() : 0)
  108.             ->setType(OperationType::RETURN)
  109.             ->setExternalId($msg->getRequestId());
  110.         $this->em->flush();
  111.     }
  112. }