src/Subscriber/Balance/Webmaster/ProvisionTrackingSubscriber.php line 134

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Balance\Webmaster;
  3. use App\Dictionary\BalanceType;
  4. use App\Dictionary\VAT;
  5. use App\Event\Balance\BalanceWebmasterProvisionCreatedEvent;
  6. use App\Event\User\MemberCreatedEvent;
  7. use App\Lib\Payment\PayMethod\AdminBonusMethod;
  8. use App\Lib\System\Job\DelineSunctionPaymentTrackingJob;
  9. use App\Service\Property\WebmasterPropertyService;
  10. use App\Service\System\JobService;
  11. use App\Service\User\WebmasterService;
  12. use App\Service\Webmaster\BalanceWebmasterService;
  13. use App\Service\Webmaster\WebmasterProvisionSignupCampaignService;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ProvisionTrackingSubscriber implements EventSubscriberInterface
  17. {
  18.     protected BalanceWebmasterService $balanceWebmasterService;
  19.     protected JobService $jobService;
  20.     protected WebmasterPropertyService $webmasterPropertyService;
  21.     protected WebmasterProvisionSignupCampaignService $service;
  22.     /**
  23.      * @var array|int[]
  24.      */
  25.     protected array $advertiserIds = [];
  26.     /**
  27.      * ProvisionTrackingSubscriber constructor.
  28.      */
  29.     public function __construct(JobService $jobServiceBalanceWebmasterService $balanceWebmasterService,
  30.         WebmasterPropertyService $webmasterPropertyService,
  31.         WebmasterProvisionSignupCampaignService $webmasterProvisionSignupCampaignService,
  32.         ParameterBagInterface $params,
  33.     ) {
  34.         $this->jobService $jobService;
  35.         $this->balanceWebmasterService $balanceWebmasterService;
  36.         $this->webmasterPropertyService $webmasterPropertyService;
  37.         $this->service $webmasterProvisionSignupCampaignService;
  38.         $advertiserIds explode(','$params->get('advertiser_ids'));
  39.         $this->advertiserIds array_map(function ($item) {
  40.             return (int) trim($item);
  41.         }, $advertiserIds);
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             BalanceWebmasterProvisionCreatedEvent::class => [
  47.                 ['trackIncome'0],
  48.             ],
  49.             MemberCreatedEvent::class => [
  50.                 ['createWebmasterProvisionOnMemberSignupCompleted'0],
  51.             ],
  52.         ];
  53.     }
  54.     protected function getPaymentMethodBlacklist(): array
  55.     {
  56.         return [
  57.             AdminBonusMethod::NAME,
  58.         ];
  59.     }
  60.     protected function getAdvertiserIds(): array
  61.     {
  62.         return $this->advertiserIds;
  63.     }
  64.     protected function ensureConditionsForIncomeProvision(BalanceWebmasterProvisionCreatedEvent $event): bool
  65.     {
  66.         $provision $event->getBalance();
  67.         if (BalanceType::INCOME !== $provision->getType()) {
  68.             return false;
  69.         }
  70.         $advertisingWebmaster $provision->getWebmaster();
  71.         // exit early, as this is only for some webmaster ids
  72.         if (!in_array($advertisingWebmaster->getId(), $this->getAdvertiserIds())) {
  73.             return false;
  74.         }
  75.         // payment could be null for provisions of provisions
  76.         $payment $provision->getPayment();
  77.         if (!$payment) {
  78.             return false;
  79.         }
  80.         // ignore bonuses
  81.         if (in_array($payment->getMethod(), $this->getPaymentMethodBlacklist())) {
  82.             return false;
  83.         }
  84.         $account $payment->getAccount();
  85.         // nothing to do without a tracking id
  86.         $trackingId $account->getAdvertisedByTrackingId();
  87.         if (empty($trackingId)) {
  88.             return false;
  89.         }
  90.         if (!$account->getAdvertisedBy() || $account->getAdvertisedBy()->getId() != $advertisingWebmaster->getId()) {
  91.             return false;
  92.         }
  93.         return true;
  94.     }
  95.     /**
  96.      * @throws \Doctrine\ORM\ORMException
  97.      * @throws \Doctrine\ORM\OptimisticLockException
  98.      */
  99.     public function trackIncome(BalanceWebmasterProvisionCreatedEvent $event)
  100.     {
  101.         if (!$this->ensureConditionsForIncomeProvision($event)) {
  102.             return;
  103.         }
  104.         $provision $event->getBalance();
  105.         $payment $provision->getPayment();
  106.         $account $payment->getAccount();
  107.         $trackingId $account->getAdvertisedByTrackingId();
  108.         $jobManager $this->jobService;
  109.         $job = new DelineSunctionPaymentTrackingJob($trackingId$provision->getAmount(), $payment->getId(), $account->getId());
  110.         $jobManager->createSaveAndSchedule($job''nullDelineSunctionPaymentTrackingJob::queue);
  111.     }
  112.     public function createWebmasterProvisionOnMemberSignupCompleted(MemberCreatedEvent $event)
  113.     {
  114.         if (!$this->service->canTriggerSignupProvision($event->getMember())) {
  115.             return;
  116.         }
  117.         $webmaster $event->getAccount()->getAdvertisedBy();
  118.         $provisionAmount $this->webmasterPropertyService->getValueForWebmaster($webmasterWebmasterService::PROPERTY_MEMBERCREATED_PROVISION_AMOUNT_NET);
  119.         $provision $this->balanceWebmasterService->createSignupProvisionByWebmaster($webmasterBalanceType::INCOME);
  120.         $provision->setAmountAddVat($provisionAmountVAT::PERCENT_21_FACTOR);
  121.         $this->balanceWebmasterService->storeBalanceWebmaster($provision);
  122.     }
  123. }