<?php
namespace App\Subscriber\Livecam;
use App\Dictionary\LivecamLogStatus;
use App\Dictionary\LiveChatMessageSource;
use App\Event\Livecam\LivecamLogCreatedEvent;
use App\Event\Livecam\LivecamLogFinishedEvent;
use App\Event\Livecam\LivecamLogUpdatedEvent;
use App\Lib\Purchase\LivecamPurchase;
use App\Service\Livecam\LiveChatMessageService;
use App\Service\Payment\PurchaseService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LivecamLogSubscriber implements EventSubscriberInterface
{
protected PurchaseService $service;
protected LiveChatMessageService $messageService;
public function __construct(LiveChatMessageService $messageService, PurchaseService $service)
{
$this->messageService = $messageService;
$this->service = $service;
}
public static function getSubscribedEvents(): array
{
return [
LivecamLogFinishedEvent::class => [
['onLivecamFinishedCreatePurchase', 0],
['onLivecamFinishedSendLiveChatMessage', 1],
],
LivecamLogUpdatedEvent::class => [
['onLivecamLogUpdatedSendCoinUpdate', 0],
],
LivecamLogCreatedEvent::class => [
['onLivecamLogCreatedSendLiveChatMessage', 0],
],
];
}
/**
* @return void
*
* @throws \App\Exception\Purchase\CoinsLockedException
* @throws \App\Exception\Purchase\InsufficientCoinsException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onLivecamFinishedCreatePurchase(LivecamLogFinishedEvent $event)
{
$entity = $event->getEntity();
if (LivecamLogStatus::FINISHED !== $entity->getState()) {
return;
}
$coins = $entity->getUsedCoins();
if (0 === $coins) {
return;
}
$consumer = $entity->getConsumer();
if ($consumer->getIsAmateur() || $consumer->getAccount()->getAdmin()) {
// no money transfer
return;
}
$purchase = new LivecamPurchase($entity->getAmateur(), $coins, $entity->getId());
$this->service->handlePurchase($purchase, $entity->getConsumer());
}
/**
* @return void
*/
public function onLivecamLogUpdatedSendCoinUpdate(LivecamLogUpdatedEvent $event)
{
}
/**
* @return void
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onLivecamLogCreatedSendLiveChatMessage(LivecamLogCreatedEvent $event)
{
$sender = $event->getEntity()->getConsumer();
$message = $this->messageService->createLiveChatMessageBySender($sender);
$message->setSource(LiveChatMessageSource::SYSTEM);
$message->setMessage('Ich habe Deine Cam betreten.');
$recipient = $event->getEntity()->getAmateur();
$this->messageService->sendLiveChatMessageToRecipient($message, $recipient);
}
/**
* @return void
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onLivecamFinishedSendLiveChatMessage(LivecamLogFinishedEvent $event)
{
$entity = $event->getEntity();
if (LivecamLogStatus::FINISHED !== $entity->getState()) {
return;
}
$sender = $event->getEntity()->getConsumer();
$message = $this->messageService->createLiveChatMessageBySender($sender);
$message->setSource(LiveChatMessageSource::SYSTEM);
$message->setMessage('Ich habe Deine Cam verlassen.');
$recipient = $event->getEntity()->getAmateur();
$this->messageService->sendLiveChatMessageToRecipient($message, $recipient);
}
}