src/Services/Activity/EventSubscriber/VideoEventSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Activity\EventSubscriber;
  4. use App\Services\Activity\ActivityServiceInterface;
  5. use App\Services\Campaign\CampaignServiceInterface;
  6. use App\Services\Creator\CreatorServiceInterface;
  7. use App\Services\System\SettingsServiceInterface;
  8. use App\Services\Video\Event\VideoStatisticsUpdatedEvent;
  9. use App\Services\Video\VideoServiceInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  12. class VideoEventSubscriber implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private readonly ActivityServiceInterface $activityService,
  16.         private readonly CreatorServiceInterface $creatorService,
  17.         private readonly SettingsServiceInterface $settingsService,
  18.         private readonly VideoServiceInterface $videoService,
  19.         private readonly CampaignServiceInterface $campaignService
  20.     ) {
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             VideoStatisticsUpdatedEvent::class => [
  26.                 ['updatePrice'1],
  27.                 ['validateStatistics'0],
  28.             ],
  29.         ];
  30.     }
  31.     public function updatePrice(VideoStatisticsUpdatedEvent $event): void
  32.     {
  33.         $video $this->videoService->get($event->getVideoId());
  34.         if ($event->isUpdateOnlyStatistics()) {
  35.             $campaignId $this->activityService->getCampaignId($video->getActivityId());
  36.             $this->campaignService->updateCampaignStatistics($campaignId);
  37.             return;
  38.         }
  39.         $this->activityService->updatePrice($video->getActivityId());
  40.     }
  41.     public function validateStatistics(VideoStatisticsUpdatedEvent $event): void
  42.     {
  43.         $video $this->videoService->get($event->getVideoId());
  44.         $expression $this->settingsService->get()->getAutoBanAfterStatisticsUpdate();
  45.         $expressionLanguage = new ExpressionLanguage();
  46.         if (trim($expression) === '') {
  47.             return;
  48.         }
  49.         /** @var bool $ban */
  50.         $ban $expressionLanguage->evaluate($expression, [
  51.             'views' => $video->getViews(),
  52.             'likes' => $video->getLikes(),
  53.             'shares' => $video->getShares(),
  54.             'comments' => $video->getComments(),
  55.         ]);
  56.         if ($ban) {
  57.             $this->creatorService->ban($video->getCreatorId());
  58.             $this->activityService->decline($video->getActivityId(), true);
  59.         }
  60.     }
  61. }