|
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\Event\AffiliateEvent; |
|
9
|
|
|
use Odiseo\SyliusReferralsPlugin\Factory\AffiliateFactoryInterface; |
|
10
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\AffiliateRepositoryInterface; |
|
11
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
|
12
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
13
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class AffiliateGenerator implements AffiliateGeneratorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private AffiliateFactoryInterface $affiliateFactory; |
|
18
|
|
|
private AffiliateRepositoryInterface $affiliateRepository; |
|
19
|
|
|
private EventDispatcherInterface $eventDispatcher; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct( |
|
22
|
|
|
AffiliateFactoryInterface $affiliateFactory, |
|
23
|
|
|
AffiliateRepositoryInterface $affiliateRepository, |
|
24
|
|
|
EventDispatcherInterface $eventDispatcher |
|
25
|
|
|
) { |
|
26
|
|
|
$this->affiliateFactory = $affiliateFactory; |
|
27
|
|
|
$this->affiliateRepository = $affiliateRepository; |
|
28
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function generate(CustomerInterface $customer, ?ProductInterface $product = null): AffiliateInterface |
|
32
|
|
|
{ |
|
33
|
|
|
$this->eventDispatcher->dispatch( |
|
34
|
|
|
new AffiliateEvent(), |
|
35
|
|
|
AffiliateEvent::PRE_GENERATE |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$affiliate = $this->affiliateFactory->createForCustomer($customer); |
|
39
|
|
|
$affiliate->setType(AffiliateInterface::REWARD_TYPE_PROMOTION); |
|
40
|
|
|
if ($product instanceof ProductInterface) { |
|
41
|
|
|
$affiliate->setProduct($product); |
|
42
|
|
|
$affiliate->setExpiresAt(new \DateTime('+15 day')); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->eventDispatcher->dispatch( |
|
46
|
|
|
new AffiliateEvent(), |
|
47
|
|
|
AffiliateEvent::GENERATE |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$this->affiliateRepository->add($affiliate); |
|
51
|
|
|
|
|
52
|
|
|
$this->eventDispatcher->dispatch( |
|
53
|
|
|
new AffiliateEvent(), |
|
54
|
|
|
AffiliateEvent::POST_GENERATE |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return $affiliate; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|