src/App/Subscriber/RefundContributionSubscriber.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use App\Contract\Payments\CanBeRefundedInterface;
  4. use App\Entity\CreditHistory;
  5. use App\Entity\DealBid;
  6. use App\Entity\Message\MessageBid;
  7. use App\Entity\ProjectBid;
  8. use App\Entity\ShopVideoBid;
  9. use App\Event\CreditsEvent;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use LogicException;
  12. use RuntimeException;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class RefundContributionSubscriber implements EventSubscriberInterface
  15. {
  16.     private ManagerRegistry $doctrine;
  17.     public function __construct(ManagerRegistry $doctrine)
  18.     {
  19.         $this->doctrine $doctrine;
  20.     }
  21.     public function onCredits(CreditsEvent $event): void
  22.     {
  23.         $refundActions = [
  24.             CreditsEvent::ACTION_DEAL_BID_REFUND,
  25.             CreditsEvent::ACTION_PROJECT_BID_REFUND,
  26.             CreditsEvent::ACTION_SHOP_VIDEO_BID_REFUND,
  27.             CreditsEvent::ACTION_MESSAGE_BID_REFUND,
  28.         ];
  29.         if (!in_array($event->getAction(), $refundActionstrue)) {
  30.             return;
  31.         }
  32.         $refundedBid $event->getRelatedEntity();
  33.         if (!($refundedBid instanceof CanBeRefundedInterface)) {
  34.             throw new LogicException('The related entity should be CanBeRefundedInterface.');
  35.         }
  36.         if ($refundedBid instanceof DealBid) {
  37.             $refundedCreditsHistory $this->doctrine->getRepository(CreditHistory::class)
  38.                 ->findOneBy(['dealBid' => $refundedBid]);
  39.         } elseif ($refundedBid instanceof ProjectBid) {
  40.             $refundedCreditsHistory $this->doctrine->getRepository(CreditHistory::class)
  41.                 ->findOneBy(['projectBid' => $refundedBid]);
  42.         } elseif ($refundedBid instanceof ShopVideoBid) {
  43.             $refundedCreditsHistory $this->doctrine->getRepository(CreditHistory::class)
  44.                 ->findOneBy(['shopVideoBid' => $refundedBid]);
  45.         } elseif ($refundedBid instanceof MessageBid) {
  46.             $refundedCreditsHistory $this->doctrine->getRepository(CreditHistory::class)
  47.                 ->findOneBy(['messageBid' => $refundedBid]);
  48.         } else {
  49.             throw new RuntimeException('Unknown bid.');
  50.         }
  51.         if (null === $refundedCreditsHistory) {
  52.             throw new LogicException('Can not find CreditsHistory to refund.');
  53.         }
  54.         $refundedCreditsHistory->setRefunded(true);
  55.         $refundedBid->setRefunded(true);
  56.         $this->doctrine->getManager()->flush();
  57.     }
  58.     /**
  59.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  60.      */
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             CreditsEvent::class => 'onCredits',
  65.         ];
  66.     }
  67. }