src/EventsSubscriber/User/ReferralSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventsSubscriber\User;
  4. use App\Entity\Referral;
  5. use App\Event\Payment\PaymentHistoryCreatedEvent;
  6. use App\Event\User\CreatedEvent;
  7. use App\Services\Creator\Event\CreatorCreatedEvent;
  8. use App\Services\Sponsor\Event\SponsorCreatedEvent;
  9. use App\Services\User\ReferralService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ReferralSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             CreatedEvent::class => 'onUserCreated',
  17.             CreatorCreatedEvent::class => 'onCreatorCreated',
  18.             SponsorCreatedEvent::class => 'onSponsorCreated',
  19.             PaymentHistoryCreatedEvent::class => 'onPaymentHistoryCreated',
  20.         ];
  21.     }
  22.     public function __construct(
  23.         private readonly ReferralService $referralService,
  24.     ) {
  25.     }
  26.     public function onSponsorCreated(SponsorCreatedEvent $event): void
  27.     {
  28.         $this->referralService->getOrCreateAccountReferral(
  29.             user$event->getSponsor(),
  30.             durationDaysReferral::DURATION_365_DAYS_PER_REFERRED_USER,
  31.         );
  32.         if ($event->getReferredBy()) {
  33.             $this->referralService->linkReferral($event->getSponsor(), $event->getReferredBy());
  34.         }
  35.     }
  36.     public function onCreatorCreated(CreatorCreatedEvent $event): void
  37.     {
  38.         $this->referralService->getOrCreateAccountReferral($event->getCreator());
  39.     }
  40.     public function onUserCreated(CreatedEvent $event): void
  41.     {
  42.         $this->referralService->getOrCreateAccountReferral($event->getUser());
  43.     }
  44.     public function onPaymentHistoryCreated(PaymentHistoryCreatedEvent $event): void
  45.     {
  46.         $this->referralService->processReferralReward($event->getPaymentHistory());
  47.     }
  48. }