class KernelEventsSubscriber in Commerce Google Tag Manager 8
Same name and namespace in other branches
- 8.2 src/EventSubscriber/KernelEventsSubscriber.php \Drupal\commerce_google_tag_manager\EventSubscriber\KernelEventsSubscriber
Event handler for Kernel events.
Hierarchy
- class \Drupal\commerce_google_tag_manager\EventSubscriber\KernelEventsSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of KernelEventsSubscriber
1 string reference to 'KernelEventsSubscriber'
1 service uses KernelEventsSubscriber
File
- src/
EventSubscriber/ KernelEventsSubscriber.php, line 16
Namespace
Drupal\commerce_google_tag_manager\EventSubscriberView source
class KernelEventsSubscriber implements EventSubscriberInterface {
/**
* The Commerce GTM event tracker.
*
* @var \Drupal\commerce_google_tag_manager\EventTrackerService
*/
private $eventTracker;
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
private $routeMatch;
/**
* The checkout order manager.
*
* @var \Drupal\commerce_checkout\CheckoutOrderManagerInterface
*/
private $checkoutOrderManager;
/**
* Constructs KernelEventsSubscriber object.
*
* @param \Drupal\commerce_google_tag_manager\EventTrackerService $event_tracker
* The Commerce GTM event tracker.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\commerce_checkout\CheckoutOrderManagerInterface $checkout_order_manager
* The checkout order manager.
*/
public function __construct(EventTrackerService $event_tracker, RouteMatchInterface $route_match, CheckoutOrderManagerInterface $checkout_order_manager) {
$this->eventTracker = $event_tracker;
$this->routeMatch = $route_match;
$this->checkoutOrderManager = $checkout_order_manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::FINISH_REQUEST => 'onFinishRequest',
];
}
/**
* Tracks an Enhanced Ecommerce checkout event.
*
* @param \Symfony\Component\HttpKernel\Event\FinishRequestEvent $event
* The request.
*/
public function onFinishRequest(FinishRequestEvent $event) {
if (!$this
->shouldTrackCheckout($event)) {
return;
}
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $this->routeMatch
->getParameter('commerce_order');
if (!$order) {
return;
}
$checkoutStepIndex = $this
->getCheckoutStepIndex($order);
if ($checkoutStepIndex) {
$this->eventTracker
->checkoutStep($checkoutStepIndex, $order);
}
}
/**
* Check if the current request matches the conditions to track the checkout.
*
* @param \Symfony\Component\HttpKernel\Event\FinishRequestEvent $event
* The request.
*
* @return bool
* Does this route should be tracked as "checkout".
*/
private function shouldTrackCheckout(FinishRequestEvent $event) {
if ($this->routeMatch
->getRouteName() !== 'commerce_checkout.form') {
return FALSE;
}
// Bail if we are not dealing with a master request or GET method.
if (!$event
->isMasterRequest() || !$event
->getRequest()
->isMethod('GET')) {
return FALSE;
}
return TRUE;
}
/**
* Returns an index for the current checkout step, starting at index 1.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order entity.
*
* @return int
* Get the Checkout step number.
*/
private function getCheckoutStepIndex(OrderInterface $order) {
$checkoutFlow = $this->checkoutOrderManager
->getCheckoutFlow($order);
$checkoutFlowPlugin = $checkoutFlow
->getPlugin();
$steps = $checkoutFlowPlugin
->getSteps();
$requestedStepId = $this->routeMatch
->getParameter('step');
$currentStepId = $this->checkoutOrderManager
->getCheckoutStepId($order, $requestedStepId);
$currentStepIndex = array_search($currentStepId, array_keys($steps));
if ($currentStepIndex === FALSE) {
return 0;
}
return ++$currentStepIndex;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
KernelEventsSubscriber:: |
private | property | The checkout order manager. | |
KernelEventsSubscriber:: |
private | property | The Commerce GTM event tracker. | |
KernelEventsSubscriber:: |
private | property | The route match. | |
KernelEventsSubscriber:: |
private | function | Returns an index for the current checkout step, starting at index 1. | |
KernelEventsSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
KernelEventsSubscriber:: |
public | function | Tracks an Enhanced Ecommerce checkout event. | |
KernelEventsSubscriber:: |
private | function | Check if the current request matches the conditions to track the checkout. | |
KernelEventsSubscriber:: |
public | function | Constructs KernelEventsSubscriber object. |