<?php
declare(strict_types=1);
namespace App\Services\Activity\EventSubscriber;
use App\Services\Activity\ActivityServiceInterface;
use App\Services\Campaign\CampaignServiceInterface;
use App\Services\Creator\CreatorServiceInterface;
use App\Services\System\SettingsServiceInterface;
use App\Services\Video\Event\VideoStatisticsUpdatedEvent;
use App\Services\Video\VideoServiceInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
class VideoEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly ActivityServiceInterface $activityService,
private readonly CreatorServiceInterface $creatorService,
private readonly SettingsServiceInterface $settingsService,
private readonly VideoServiceInterface $videoService,
private readonly CampaignServiceInterface $campaignService
) {
}
public static function getSubscribedEvents(): array
{
return [
VideoStatisticsUpdatedEvent::class => [
['updatePrice', 1],
['validateStatistics', 0],
],
];
}
public function updatePrice(VideoStatisticsUpdatedEvent $event): void
{
$video = $this->videoService->get($event->getVideoId());
if ($event->isUpdateOnlyStatistics()) {
$campaignId = $this->activityService->getCampaignId($video->getActivityId());
$this->campaignService->updateCampaignStatistics($campaignId);
return;
}
$this->activityService->updatePrice($video->getActivityId());
}
public function validateStatistics(VideoStatisticsUpdatedEvent $event): void
{
$video = $this->videoService->get($event->getVideoId());
$expression = $this->settingsService->get()->getAutoBanAfterStatisticsUpdate();
$expressionLanguage = new ExpressionLanguage();
if (trim($expression) === '') {
return;
}
/** @var bool $ban */
$ban = $expressionLanguage->evaluate($expression, [
'views' => $video->getViews(),
'likes' => $video->getLikes(),
'shares' => $video->getShares(),
'comments' => $video->getComments(),
]);
if ($ban) {
$this->creatorService->ban($video->getCreatorId());
$this->activityService->decline($video->getActivityId(), true);
}
}
}