<?php
namespace App\Subscriber\Livecam;
use App\Dictionary\BalanceDetailUsageType;
use App\Dictionary\BalanceType;
use App\Event\Balance\BalanceAmateurProvisionCreatedEvent;
use App\Event\RedisEventManager;
use Frivol\Common\Service\Notification\LivecamProvisionNotification;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BalanceAmateurSubscriber implements EventSubscriberInterface
{
protected RedisEventManager $redisEventManager;
public function __construct(RedisEventManager $redisEventManager)
{
$this->redisEventManager = $redisEventManager;
}
public static function getSubscribedEvents(): array
{
return [
BalanceAmateurProvisionCreatedEvent::class => [
['onLivecamProvisionSendNotification', 0],
],
];
}
/**
* @return void
*/
public function onLivecamProvisionSendNotification(BalanceAmateurProvisionCreatedEvent $event)
{
$balance = $event->getBalance();
if (BalanceType::INCOME !== $balance->getType()) {
return;
}
if ($balance->isTypeCam()) { // of course, it is need to disable it temporarily
return;
}
$detail = $balance->getBalanceDetail();
if (!$detail || BalanceDetailUsageType::CAM !== $detail->getUsageType()) {
return;
}
if ($balance->getAmount() / 1.21 < 0.01) {
return;
}
$amateur = $balance->getMember();
$amount = number_format($balance->getAmount() / 1.21, 2, ',', '.');
$notification = new LivecamProvisionNotification();
$notification->setData([
'message' => 'Du hast eine Gutschrift über '.$amount.' EUR erhalten.',
'amount' => $amount,
]);
$notification->setRecipientUsername($amateur->getUsername());
$notification->setRecipientId($amateur->getId());
$this->redisEventManager->publishToMember($amateur, $notification);
}
}