src/Subscriber/Payment/Purchase/PurchaseMessengerSubscriber.php line 118

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Payment\Purchase;
  3. use App\Dictionary\ContentType;
  4. use App\Dictionary\ConversationMessageSource;
  5. use App\Dictionary\ConversationMessageType;
  6. use App\Event\Payment\PurchaseCreatedEvent;
  7. use App\Lib\Purchase\ContentPurchasable;
  8. use App\Lib\Purchase\ConversationMessageAttachmentPurchasable;
  9. use App\Lib\Purchase\ConversationMessagePurchasable;
  10. use App\Service\Content\ContentService;
  11. use App\Service\Messenger\MessengerCacheService;
  12. use App\Service\Messenger\MessengerService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class PurchaseMessengerSubscriber implements EventSubscriberInterface
  15. {
  16.     protected MessengerService $messenger;
  17.     protected MessengerCacheService $cacheService;
  18.     protected ContentService $contentService;
  19.     public function __construct(MessengerService $messengerContentService $contentService,
  20.         MessengerCacheService $cacheService)
  21.     {
  22.         $this->messenger $messenger;
  23.         $this->contentService $contentService;
  24.         $this->cacheService $cacheService;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             PurchaseCreatedEvent::class => [
  30.                 ['onContentPurchaseSendMessengerNotification'0],
  31.                 ['onMessengerPurchaseUpdateConversationMessage'1],
  32.                 ['onAttachmentPurchaseClearConversationCache'2],
  33.             ],
  34.         ];
  35.     }
  36.     /**
  37.      * @return void
  38.      *
  39.      * @throws \Doctrine\ORM\ORMException
  40.      * @throws \Doctrine\ORM\OptimisticLockException
  41.      */
  42.     public function onContentPurchaseSendMessengerNotification(PurchaseCreatedEvent $event)
  43.     {
  44.         $purchasable $event->getPurchasable();
  45.         if (!$purchasable instanceof ContentPurchasable) {
  46.             return;
  47.         }
  48.         $content $purchasable->getContent();
  49.         $seller $purchasable->getSeller();
  50.         $buyer $event->getPurchase()->getBuyer();
  51.         $atDate $this->getMessengerNotificationTextDate($event->getPurchase()->getCreatedAt());
  52.         $text $this->getMessengerNotificationTextTemplate();
  53.         if (ContentType::IMAGESET === $content->getType()) {
  54.             $text $this->getMessengerNotificationImagesetImageTextTemplate();
  55.         }
  56.         $provi = ($purchasable->getAmount() * 0.25) / 1.21;
  57.         $text sprintf($text$seller->getUsername(), $buyer->getUsername(), $content->getTitle(), $atDate$provi);
  58.         $this->messenger->sendDirectMessage($buyer$seller$text,
  59.             ConversationMessageType::CONTENT_PURCHASEConversationMessageSource::SYSTEM);
  60.     }
  61.     protected function getMessengerNotificationTextDate(\DateTimeInterface $created): string
  62.     {
  63.         $date $created->format('d.m.Y');
  64.         $time $created->format('H:i');
  65.         return $date.', '.$time.' Uhr';
  66.     }
  67.     protected function getMessengerNotificationTextTemplate(): string
  68.     {
  69.         return 'Hallo %s, der User %s hat dein Video "%s" am %s gekauft. Dein Verdienst: %01.2f EUR.'.
  70.             "\n\nBitte reagiere: Bedanke dich mit einer Nachricht an den User und/oder bitte um eine Bewertung indem ".
  71.             'du direkt hier antwortest. So kannst du weitere Verkäufe generieren!'.
  72.             "\n\nHinweis: Diese Nachricht ist nur für dich sichtbar!";
  73.     }
  74.     protected function getMessengerNotificationImagesetImageTextTemplate(): string
  75.     {
  76.         return 'Hallo %s, der User %s hat EIN Bild aus dem Bilderset "%s" am %s gekauft. Dein Verdienst: %01.2f EUR.'.
  77.             "\n\nBitte reagiere: Bedanke dich mit einer Nachricht an den User und/oder bitte um eine Bewertung indem ".
  78.             'du direkt hier antwortest. So kannst du weitere Verkäufe generieren!'.
  79.             "\n\nHinweis: Diese Nachricht ist nur für dich sichtbar!";
  80.     }
  81.     /**
  82.      * @return void
  83.      */
  84.     public function onAttachmentPurchaseClearConversationCache(PurchaseCreatedEvent $event)
  85.     {
  86.         $purchasable $event->getPurchasable();
  87.         if (!$purchasable instanceof ConversationMessageAttachmentPurchasable) {
  88.             return;
  89.         }
  90.         $message $purchasable->getEntity();
  91.         if ($conversation $message->getConversation()) {
  92.             $this->cacheService->clearMessagesCacheForConversation($conversation->getId());
  93.         }
  94.     }
  95.     /**
  96.      * @throws \Doctrine\ORM\ORMException
  97.      * @throws \Doctrine\ORM\OptimisticLockException
  98.      */
  99.     public function onMessengerPurchaseUpdateConversationMessage(PurchaseCreatedEvent $event)
  100.     {
  101.         $purchase $event->getPurchasable();
  102.         if ($purchase instanceof ConversationMessagePurchasable || $purchase instanceof ConversationMessageAttachmentPurchasable) {
  103.             $message $purchase->getEntity();
  104.             if ($message->getPurchase()) {
  105.                 // just in case: already purchased
  106.                 return;
  107.             }
  108.             $message->setPurchase($event->getPurchase());
  109.             $this->messenger->storeConversationMessage($message);
  110.         }
  111.     }
  112. }