-
Notifications
You must be signed in to change notification settings - Fork 17
/
AddToCartSubscriber.php
49 lines (39 loc) · 1.47 KB
/
AddToCartSubscriber.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
<?php
declare(strict_types=1);
namespace Setono\SyliusFacebookPlugin\EventSubscriber;
use Psr\EventDispatcher\EventDispatcherInterface;
use Setono\SyliusFacebookPlugin\Event\ProductAddedToCartEvent;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Webmozart\Assert\Assert;
final class AddToCartSubscriber extends EventSubscriber
{
private CartContextInterface $cartContext;
public function __construct(
EventDispatcherInterface $eventDispatcher,
CartContextInterface $cartContext
) {
parent::__construct($eventDispatcher);
$this->cartContext = $cartContext;
}
public static function getSubscribedEvents(): array
{
return [
'sylius.order_item.post_add' => 'track',
];
}
protected function callback(): callable
{
return function (ResourceControllerEvent $event): ProductAddedToCartEvent {
/** @var mixed|OrderItemInterface $orderItem */
$orderItem = $event->getSubject();
Assert::isInstanceOf($orderItem, OrderItemInterface::class);
/** @var OrderInterface $order */
$order = $this->cartContext->getCart();
Assert::isInstanceOf($order, OrderInterface::class);
return new ProductAddedToCartEvent($order, $orderItem);
};
}
}