src/EventSubscriber/MarketEventSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Notification;
  4. use App\Entity\UserTransaction;
  5. use App\Event\ItemOnMarketPurchasedEvent;
  6. use App\Event\ItemQuickSellEvent;
  7. use App\Service\NotificationService;
  8. use App\Service\UserTransactionManager;
  9. use App\Service\UserWalletManager;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Config\Definition\Exception\Exception;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class MarketEventSubscriber implements EventSubscriberInterface
  14. {
  15.   private $notificationService;
  16.   private $em;
  17.   private $userWalletManager;
  18.   private $userTransactionManager;
  19.   public function __construct(
  20.     NotificationService $notificationService,
  21.     EntityManagerInterface $entityManager,
  22.     UserWalletManager $userWalletManager,
  23.     UserTransactionManager $userTransactionManager
  24.   )
  25.   {
  26.     $this->notificationService $notificationService;
  27.     $this->em $entityManager;
  28.     $this->userWalletManager $userWalletManager;
  29.     $this->userTransactionManager $userTransactionManager;
  30.   }
  31.   public function onItemOnMarketPurchased(ItemOnMarketPurchasedEvent $event)
  32.   {
  33.     $userItem $event->getUserItem();
  34.     $owner $userItem->getUser();
  35.     $buyer $event->getBuyer();
  36.     $itemPrice $userItem->getPrice();
  37.     $buyerWallet $this->userWalletManager->getWallet($buyer);
  38.     $ownerWallet $this->userWalletManager->getWallet($owner);
  39.     if($itemPrice $buyerWallet->getValue()){
  40.       throw new Exception('Not enough money');
  41.     }
  42.     try {
  43.       $this->em->getConnection()->beginTransaction();
  44.       $buyerWallet->substractValue($itemPrice);
  45.       $ownerWallet->addValue($itemPrice);
  46.       $this->userTransactionManager->create($buyer$owner$itemPriceUserTransaction::TYPE_ITEM_MARKET_PURCHASED);
  47.       $this->userTransactionManager->create($owner$buyer, (-1)*$itemPriceUserTransaction::TYPE_ITEM_MARKET_SOLD);
  48.       $this->notificationService->create(
  49.         $owner,
  50.         Notification::TYPE_ITEM_SOLD,
  51.         [
  52.           'item_id' => $userItem->getItem()->getId(),
  53.           'item_name' => $userItem->getItem()->getName(),
  54.           'item_price' => $userItem->getPrice(),
  55.           'item_avatar' => $userItem->getItem()->getThumbnailUrl(),
  56.         ]
  57.       );
  58.       $userItem->setUser($buyer);
  59.       $userItem->setOnMarket(false);
  60.       $this->em->persist($userItem);
  61.       $this->em->persist($buyerWallet);
  62.       $this->em->persist($ownerWallet);
  63.       $this->em->flush();
  64.       $this->em->getConnection()->commit();
  65.     } catch (\Exception $e){
  66.       $this->em->getConnection()->rollBack();
  67.       throw new Exception($e->getMessage());
  68.     }
  69.   }
  70.   public function onItemQuickSold(ItemQuickSellEvent $event)
  71.   {
  72.     $userItem $event->getUserItem();
  73.     $userWallet $this->userWalletManager->getWallet($userItem->getUser());
  74.     try {
  75.       $this->em->getConnection()->beginTransaction();
  76.       $quickSellPrice $userItem->getItem()->getPrice()/2;
  77.       $userWallet->addValue($quickSellPrice);
  78.       $this->userTransactionManager->create(null$userItem->getUser(), $quickSellPriceUserTransaction::TYPE_MARKET_QUICK_SOLD);
  79.       $this->em->remove($userItem);
  80.       $this->em->persist($userWallet);
  81.       $this->em->flush();
  82.       $this->em->getConnection()->commit();
  83.     } catch (\Exception $e){
  84.       $this->em->getConnection()->rollBack();
  85.       throw new Exception($e->getMessage());
  86.     }
  87.   }
  88.   public static function getSubscribedEvents()
  89.   {
  90.     return [
  91.       ItemOnMarketPurchasedEvent::NAME => 'onItemOnMarketPurchased',
  92.       ItemQuickSellEvent::NAME => 'onItemQuickSold',
  93.     ];
  94.   }
  95. }