forked from Setono/SyliusAnalyticsPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurchaseSubscriber.php
123 lines (99 loc) · 3.91 KB
/
PurchaseSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Setono\SyliusAnalyticsPlugin\EventListener;
use Psr\EventDispatcher\EventDispatcherInterface;
use Setono\SyliusAnalyticsPlugin\Builder\ItemBuilder;
use Setono\SyliusAnalyticsPlugin\Builder\PurchaseBuilder;
use Setono\SyliusAnalyticsPlugin\Context\PropertyContextInterface;
use Setono\SyliusAnalyticsPlugin\Event\BuilderEvent;
use Setono\TagBag\Tag\GtagEvent;
use Setono\TagBag\Tag\GtagEventInterface;
use Setono\TagBag\Tag\GtagLibrary;
use Setono\TagBag\TagBagInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Order\Repository\OrderRepositoryInterface;
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class PurchaseSubscriber extends TagSubscriber
{
private OrderRepositoryInterface $orderRepository;
public function __construct(
TagBagInterface $tagBag,
PropertyContextInterface $propertyContext,
EventDispatcherInterface $eventDispatcher,
RequestStack $requestStack,
FirewallMap $firewallMap,
OrderRepositoryInterface $orderRepository
) {
parent::__construct($tagBag, $propertyContext, $eventDispatcher, $requestStack, $firewallMap);
$this->orderRepository = $orderRepository;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'track',
];
}
public function track(RequestEvent $event): void
{
$request = $event->getRequest();
if (!$event->isMasterRequest() || !$this->isShopContext($request)) {
return;
}
if (!$request->attributes->has('_route')) {
return;
}
$route = $request->attributes->get('_route');
if ('sylius_shop_order_thank_you' !== $route) {
return;
}
/** @var mixed $orderId */
$orderId = $request->getSession()->get('sylius_order_id');
if (!is_scalar($orderId)) {
return;
}
$order = $this->orderRepository->find($orderId);
if (null === $order) {
return;
}
if (!$order instanceof OrderInterface) {
return;
}
$channel = $order->getChannel();
if (null === $channel) {
return;
}
if (!$this->hasProperties()) {
return;
}
$builder = PurchaseBuilder::create()
->setTransactionId((string) $order->getNumber())
->setAffiliation((string) $channel->getName() . ' (' . (string) $order->getLocaleCode() . ')')
->setValue((float) $this->moneyFormatter->format($order->getTotal()))
->setCurrency((string) $order->getCurrencyCode())
->setTax((float) $this->moneyFormatter->format($order->getTaxTotal()))
->setShipping((float) $this->moneyFormatter->format($order->getShippingTotal()))
;
foreach ($order->getItems() as $orderItem) {
$variant = $orderItem->getVariant();
if (null === $variant) {
continue;
}
$itemBuilder = ItemBuilder::create()
->setId((string) $variant->getCode())
->setName((string) $orderItem->getVariantName())
->setQuantity($orderItem->getQuantity())
->setPrice((float) $this->moneyFormatter->format($orderItem->getDiscountedUnitPrice()))
;
$this->eventDispatcher->dispatch(new BuilderEvent($itemBuilder, $orderItem));
$builder->addItem($itemBuilder);
}
$this->eventDispatcher->dispatch(new BuilderEvent($builder, $order));
$this->tagBag->addTag(
(new GtagEvent(GtagEventInterface::EVENT_PURCHASE, $builder->getData()))
->addDependency(GtagLibrary::NAME)
);
}
}