<?php
namespace App\Subscriber\Payment\Purchase;
use App\Dictionary\ContentType;
use App\Dictionary\ConversationMessageSource;
use App\Dictionary\ConversationMessageType;
use App\Event\Payment\PurchaseCreatedEvent;
use App\Lib\Purchase\ContentPurchasable;
use App\Lib\Purchase\ConversationMessageAttachmentPurchasable;
use App\Lib\Purchase\ConversationMessagePurchasable;
use App\Service\Content\ContentService;
use App\Service\Messenger\MessengerCacheService;
use App\Service\Messenger\MessengerService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PurchaseMessengerSubscriber implements EventSubscriberInterface
{
protected MessengerService $messenger;
protected MessengerCacheService $cacheService;
protected ContentService $contentService;
public function __construct(MessengerService $messenger, ContentService $contentService,
MessengerCacheService $cacheService)
{
$this->messenger = $messenger;
$this->contentService = $contentService;
$this->cacheService = $cacheService;
}
public static function getSubscribedEvents(): array
{
return [
PurchaseCreatedEvent::class => [
['onContentPurchaseSendMessengerNotification', 0],
['onMessengerPurchaseUpdateConversationMessage', 1],
['onAttachmentPurchaseClearConversationCache', 2],
],
];
}
/**
* @return void
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onContentPurchaseSendMessengerNotification(PurchaseCreatedEvent $event)
{
$purchasable = $event->getPurchasable();
if (!$purchasable instanceof ContentPurchasable) {
return;
}
$content = $purchasable->getContent();
$seller = $purchasable->getSeller();
$buyer = $event->getPurchase()->getBuyer();
$atDate = $this->getMessengerNotificationTextDate($event->getPurchase()->getCreatedAt());
$text = $this->getMessengerNotificationTextTemplate();
if (ContentType::IMAGESET === $content->getType()) {
$text = $this->getMessengerNotificationImagesetImageTextTemplate();
}
$provi = ($purchasable->getAmount() * 0.25) / 1.21;
$text = sprintf($text, $seller->getUsername(), $buyer->getUsername(), $content->getTitle(), $atDate, $provi);
$this->messenger->sendDirectMessage($buyer, $seller, $text,
ConversationMessageType::CONTENT_PURCHASE, ConversationMessageSource::SYSTEM);
}
protected function getMessengerNotificationTextDate(\DateTimeInterface $created): string
{
$date = $created->format('d.m.Y');
$time = $created->format('H:i');
return $date.', '.$time.' Uhr';
}
protected function getMessengerNotificationTextTemplate(): string
{
return 'Hallo %s, der User %s hat dein Video "%s" am %s gekauft. Dein Verdienst: %01.2f EUR.'.
"\n\nBitte reagiere: Bedanke dich mit einer Nachricht an den User und/oder bitte um eine Bewertung indem ".
'du direkt hier antwortest. So kannst du weitere Verkäufe generieren!'.
"\n\nHinweis: Diese Nachricht ist nur für dich sichtbar!";
}
protected function getMessengerNotificationImagesetImageTextTemplate(): string
{
return 'Hallo %s, der User %s hat EIN Bild aus dem Bilderset "%s" am %s gekauft. Dein Verdienst: %01.2f EUR.'.
"\n\nBitte reagiere: Bedanke dich mit einer Nachricht an den User und/oder bitte um eine Bewertung indem ".
'du direkt hier antwortest. So kannst du weitere Verkäufe generieren!'.
"\n\nHinweis: Diese Nachricht ist nur für dich sichtbar!";
}
/**
* @return void
*/
public function onAttachmentPurchaseClearConversationCache(PurchaseCreatedEvent $event)
{
$purchasable = $event->getPurchasable();
if (!$purchasable instanceof ConversationMessageAttachmentPurchasable) {
return;
}
$message = $purchasable->getEntity();
if ($conversation = $message->getConversation()) {
$this->cacheService->clearMessagesCacheForConversation($conversation->getId());
}
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onMessengerPurchaseUpdateConversationMessage(PurchaseCreatedEvent $event)
{
$purchase = $event->getPurchasable();
if ($purchase instanceof ConversationMessagePurchasable || $purchase instanceof ConversationMessageAttachmentPurchasable) {
$message = $purchase->getEntity();
if ($message->getPurchase()) {
// just in case: already purchased
return;
}
$message->setPurchase($event->getPurchase());
$this->messenger->storeConversationMessage($message);
}
}
}