src/Subscriber/Livecam/LivecamLogSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Livecam;
  3. use App\Dictionary\LivecamLogStatus;
  4. use App\Dictionary\LiveChatMessageSource;
  5. use App\Event\Livecam\LivecamLogCreatedEvent;
  6. use App\Event\Livecam\LivecamLogFinishedEvent;
  7. use App\Event\Livecam\LivecamLogUpdatedEvent;
  8. use App\Lib\Purchase\LivecamPurchase;
  9. use App\Service\Livecam\LiveChatMessageService;
  10. use App\Service\Payment\PurchaseService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class LivecamLogSubscriber implements EventSubscriberInterface
  13. {
  14.     protected PurchaseService $service;
  15.     protected LiveChatMessageService $messageService;
  16.     public function __construct(LiveChatMessageService $messageServicePurchaseService $service)
  17.     {
  18.         $this->messageService $messageService;
  19.         $this->service $service;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             LivecamLogFinishedEvent::class => [
  25.                 ['onLivecamFinishedCreatePurchase'0],
  26.                 ['onLivecamFinishedSendLiveChatMessage'1],
  27.             ],
  28.             LivecamLogUpdatedEvent::class => [
  29.                 ['onLivecamLogUpdatedSendCoinUpdate'0],
  30.             ],
  31.             LivecamLogCreatedEvent::class => [
  32.                 ['onLivecamLogCreatedSendLiveChatMessage'0],
  33.             ],
  34.         ];
  35.     }
  36.     /**
  37.      * @return void
  38.      *
  39.      * @throws \App\Exception\Purchase\CoinsLockedException
  40.      * @throws \App\Exception\Purchase\InsufficientCoinsException
  41.      * @throws \Doctrine\ORM\ORMException
  42.      * @throws \Doctrine\ORM\OptimisticLockException
  43.      */
  44.     public function onLivecamFinishedCreatePurchase(LivecamLogFinishedEvent $event)
  45.     {
  46.         $entity $event->getEntity();
  47.         if (LivecamLogStatus::FINISHED !== $entity->getState()) {
  48.             return;
  49.         }
  50.         $coins $entity->getUsedCoins();
  51.         if (=== $coins) {
  52.             return;
  53.         }
  54.         $consumer $entity->getConsumer();
  55.         if ($consumer->getIsAmateur() || $consumer->getAccount()->getAdmin()) {
  56.             // no money transfer
  57.             return;
  58.         }
  59.         $purchase = new LivecamPurchase($entity->getAmateur(), $coins$entity->getId());
  60.         $this->service->handlePurchase($purchase$entity->getConsumer());
  61.     }
  62.     /**
  63.      * @return void
  64.      */
  65.     public function onLivecamLogUpdatedSendCoinUpdate(LivecamLogUpdatedEvent $event)
  66.     {
  67.     }
  68.     /**
  69.      * @return void
  70.      *
  71.      * @throws \Doctrine\ORM\ORMException
  72.      * @throws \Doctrine\ORM\OptimisticLockException
  73.      */
  74.     public function onLivecamLogCreatedSendLiveChatMessage(LivecamLogCreatedEvent $event)
  75.     {
  76.         $sender $event->getEntity()->getConsumer();
  77.         $message $this->messageService->createLiveChatMessageBySender($sender);
  78.         $message->setSource(LiveChatMessageSource::SYSTEM);
  79.         $message->setMessage('Ich habe Deine Cam betreten.');
  80.         $recipient $event->getEntity()->getAmateur();
  81.         $this->messageService->sendLiveChatMessageToRecipient($message$recipient);
  82.     }
  83.     /**
  84.      * @return void
  85.      *
  86.      * @throws \Doctrine\ORM\ORMException
  87.      * @throws \Doctrine\ORM\OptimisticLockException
  88.      */
  89.     public function onLivecamFinishedSendLiveChatMessage(LivecamLogFinishedEvent $event)
  90.     {
  91.         $entity $event->getEntity();
  92.         if (LivecamLogStatus::FINISHED !== $entity->getState()) {
  93.             return;
  94.         }
  95.         $sender $event->getEntity()->getConsumer();
  96.         $message $this->messageService->createLiveChatMessageBySender($sender);
  97.         $message->setSource(LiveChatMessageSource::SYSTEM);
  98.         $message->setMessage('Ich habe Deine Cam verlassen.');
  99.         $recipient $event->getEntity()->getAmateur();
  100.         $this->messageService->sendLiveChatMessageToRecipient($message$recipient);
  101.     }
  102. }