-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathStartCheckoutSubscriber.php
58 lines (46 loc) · 1.58 KB
/
StartCheckoutSubscriber.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
<?php
declare(strict_types=1);
namespace Setono\SyliusFacebookPlugin\EventSubscriber;
use Psr\EventDispatcher\EventDispatcherInterface;
use Setono\SyliusFacebookPlugin\Event\CheckoutStartedEvent;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Webmozart\Assert\Assert;
final class StartCheckoutSubscriber extends EventSubscriber
{
private CartContextInterface $cartContext;
public function __construct(
EventDispatcherInterface $eventDispatcher,
CartContextInterface $cartContext
) {
parent::__construct($eventDispatcher);
$this->cartContext = $cartContext;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'track',
];
}
protected function callback(): callable
{
return function (RequestEvent $event): ?CheckoutStartedEvent {
if (!$event->isMainRequest()) {
return null;
}
$request = $event->getRequest();
if ($request->attributes->get('_route') !== 'sylius_shop_checkout_start') {
return null;
}
/** @var OrderInterface $order */
$order = $this->cartContext->getCart();
Assert::isInstanceOf($order, OrderInterface::class);
if ($order->isEmpty()) {
return null;
}
return new CheckoutStartedEvent($order);
};
}
}