1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusReferralsPlugin\Generator; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateInterface; |
8
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateReferralInterface; |
9
|
|
|
use Odiseo\SyliusReferralsPlugin\Event\AffiliateReferralEvent; |
10
|
|
|
use Odiseo\SyliusReferralsPlugin\Factory\AffiliateReferralFactoryInterface; |
11
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\AffiliateReferralRepositoryInterface; |
12
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
13
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
|
15
|
|
|
final class AffiliateReferralGenerator implements AffiliateReferralGeneratorInterface |
16
|
|
|
{ |
17
|
|
|
public function __construct( |
18
|
|
|
private AffiliateReferralFactoryInterface $affiliateReferralFactory, |
19
|
|
|
private AffiliateReferralRepositoryInterface $affiliateReferralRepository, |
20
|
|
|
private EventDispatcherInterface $eventDispatcher, |
21
|
|
|
) { |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function generate( |
25
|
|
|
AffiliateInterface $affiliate, |
26
|
|
|
?ProductInterface $product = null, |
27
|
|
|
): AffiliateReferralInterface { |
28
|
|
|
$this->eventDispatcher->dispatch( |
29
|
|
|
new AffiliateReferralEvent(), |
30
|
|
|
AffiliateReferralEvent::PRE_GENERATE, |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$affiliateReferral = $this->affiliateReferralFactory->createForAffiliate($affiliate); |
34
|
|
|
$affiliateReferral->setRewardType(AffiliateReferralInterface::REWARD_TYPE_PROMOTION); |
35
|
|
|
if ($product instanceof ProductInterface) { |
36
|
|
|
$affiliateReferral->setProduct($product); |
37
|
|
|
$affiliateReferral->setExpiresAt(new \DateTime('+15 day')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->eventDispatcher->dispatch( |
41
|
|
|
new AffiliateReferralEvent(), |
42
|
|
|
AffiliateReferralEvent::GENERATE, |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$this->affiliateReferralRepository->add($affiliateReferral); |
46
|
|
|
|
47
|
|
|
$this->eventDispatcher->dispatch( |
48
|
|
|
new AffiliateReferralEvent(), |
49
|
|
|
AffiliateReferralEvent::POST_GENERATE, |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return $affiliateReferral; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|