-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAddPaymentInfoSubscriber.php
More file actions
68 lines (54 loc) · 2.24 KB
/
AddPaymentInfoSubscriber.php
File metadata and controls
68 lines (54 loc) · 2.24 KB
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
<?php
declare(strict_types=1);
namespace Setono\SyliusAnalyticsPlugin\EventSubscriber;
use Psr\EventDispatcher\EventDispatcherInterface;
use Setono\GoogleAnalyticsBundle\Event\ClientSideEvent;
use Setono\GoogleAnalyticsEvents\Event\AddPaymentInfoEvent;
use Setono\SyliusAnalyticsPlugin\Resolver\Items\ItemsResolverInterface;
use Setono\SyliusAnalyticsPlugin\Util\FormatAmountTrait;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Core\Model\OrderInterface;
use Webmozart\Assert\Assert;
final class AddPaymentInfoSubscriber extends AbstractEventSubscriber
{
use FormatAmountTrait;
private EventDispatcherInterface $eventDispatcher;
private ItemsResolverInterface $itemsResolver;
public function __construct(EventDispatcherInterface $eventDispatcher, ItemsResolverInterface $itemsResolver)
{
parent::__construct();
$this->eventDispatcher = $eventDispatcher;
$this->itemsResolver = $itemsResolver;
}
public static function getSubscribedEvents(): array
{
return [
'sylius.order.post_payment' => 'track',
];
}
public function track(ResourceControllerEvent $resourceControllerEvent): void
{
try {
/** @var OrderInterface|mixed $order */
$order = $resourceControllerEvent->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);
$lastPayment = $order->getLastPayment();
Assert::notNull($lastPayment);
$paymentMethod = $lastPayment->getMethod();
Assert::notNull($paymentMethod);
$paymentMethodCode = $paymentMethod->getCode();
Assert::notNull($paymentMethodCode);
$this->eventDispatcher->dispatch(
new ClientSideEvent(
AddPaymentInfoEvent::create()
->setValue(self::formatAmount($order->getTotal()))
->setCurrency($order->getCurrencyCode())
->setPaymentType($paymentMethodCode)
->setItems($this->itemsResolver->resolveFromOrder($order)),
),
);
} catch (\Throwable $e) {
$this->log(AddPaymentInfoEvent::getName(), $e);
}
}
}