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
|
|
|
private AffiliateReferralFactoryInterface $affiliateReferralFactory; |
18
|
|
|
private AffiliateReferralRepositoryInterface $affiliateReferralRepository; |
19
|
|
|
private EventDispatcherInterface $eventDispatcher; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
AffiliateReferralFactoryInterface $affiliateReferralFactory, |
23
|
|
|
AffiliateReferralRepositoryInterface $affiliateReferralRepository, |
24
|
|
|
EventDispatcherInterface $eventDispatcher |
25
|
|
|
) { |
26
|
|
|
$this->affiliateReferralFactory = $affiliateReferralFactory; |
27
|
|
|
$this->affiliateReferralRepository = $affiliateReferralRepository; |
28
|
|
|
$this->eventDispatcher = $eventDispatcher; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function generate( |
32
|
|
|
AffiliateInterface $affiliate, |
33
|
|
|
?ProductInterface $product = null |
34
|
|
|
): AffiliateReferralInterface { |
35
|
|
|
$this->eventDispatcher->dispatch( |
36
|
|
|
new AffiliateReferralEvent(), |
37
|
|
|
AffiliateReferralEvent::PRE_GENERATE |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$affiliateReferral = $this->affiliateReferralFactory->createForAffiliate($affiliate); |
41
|
|
|
$affiliateReferral->setRewardType(AffiliateReferralInterface::REWARD_TYPE_PROMOTION); |
42
|
|
|
if ($product instanceof ProductInterface) { |
43
|
|
|
$affiliateReferral->setProduct($product); |
44
|
|
|
$affiliateReferral->setExpiresAt(new \DateTime('+15 day')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->eventDispatcher->dispatch( |
48
|
|
|
new AffiliateReferralEvent(), |
49
|
|
|
AffiliateReferralEvent::GENERATE |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->affiliateReferralRepository->add($affiliateReferral); |
53
|
|
|
|
54
|
|
|
$this->eventDispatcher->dispatch( |
55
|
|
|
new AffiliateReferralEvent(), |
56
|
|
|
AffiliateReferralEvent::POST_GENERATE |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
return $affiliateReferral; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|