<?php
declare(strict_types=1);
namespace App\EventsSubscriber\Activity;
use App\Contracts\Activity\Status;
use App\Event\Activity\ActivityActivatedEvent;
use App\Event\Activity\ApproveAllReviewEvent;
use App\Event\Activity\DeclineAllReviewEvent;
use App\Event\Activity\DeclineCampaignActivityEvent;
use App\Event\Campaign\RenewedCampaignEvent;
use App\Messenger\Activity\ApproveAllReviewMessage;
use App\Messenger\Activity\DeclineAllReviewMessage;
use App\Messenger\Activity\DeclineCampaignActivityMessage;
use App\Services\Activity\ActivityServiceInterface;
use App\Services\Campaign\CampaignServiceInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class ActivityEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ActivityActivatedEvent::class => 'onActivityActivated',
DeclineAllReviewEvent::class => 'declineAllReview',
ApproveAllReviewEvent::class => 'approveAllReview',
DeclineCampaignActivityEvent::class => 'declineCampaignActivity',
RenewedCampaignEvent::class => ['onRenewedCampaign', 10],
];
}
public function __construct(
private readonly MessageBusInterface $bus,
private readonly ActivityServiceInterface $activityService,
private readonly CampaignServiceInterface $campaignService,
) {}
public function onActivityActivated(ActivityActivatedEvent $event): void
{
$this->campaignService->updateCampaignStatistics($event->getActivity()->getCampaign()->getId());
}
public function declineAllReview(DeclineAllReviewEvent $event): void
{
$this->bus->dispatch(
new DeclineAllReviewMessage($event->getCampaign()->getId(), $event->getReason()),
);
}
public function approveAllReview(ApproveAllReviewEvent $event): void
{
$this->bus->dispatch(
new ApproveAllReviewMessage($event->getCampaign()->getId()),
);
}
public function declineCampaignActivity(DeclineCampaignActivityEvent $event): void
{
$this->bus->dispatch(
new DeclineCampaignActivityMessage($event->getCampaign()->getId()),
);
}
public function onRenewedCampaign(RenewedCampaignEvent $event): void
{
$activities = $this->activityService->getCampaignActivitiesByStatuses(
$event->getCampaign()->getId(),
[
Status::ACCEPTED,
Status::SPONSOR_REVIEW,
Status::VIDEO_PENDING,
Status::SPONSOR_DECLINE,
Status::DRAFT_VIDEO_REVIEW,
]
);
$this->activityService->deleteActivities($activities);
}
}