You are here

ResponseSubscriber.php in Analytics 8

File

src/EventSubscriber/ResponseSubscriber.php
View source
<?php

namespace Drupal\analytics\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Analytics ResponseSubscriber.
 */
class ResponseSubscriber implements EventSubscriberInterface {

  /**
   * The analytics module configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Constructs a new ResponseSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   The configuration factory.
   */
  public function __construct(ConfigFactoryInterface $config) {
    $this->config = $config
      ->get('analytics.settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::RESPONSE => [
        'onKernelResponse',
      ],
    ];
  }

  /**
   * Respond to the kernel response event.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event.
   */
  public function onKernelResponse(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }

    // Adds "Permissions-Policy: interest-cohort=()" header to disable FLoC.
    if ($this->config
      ->get('privacy.disable_floc')) {
      $event
        ->getResponse()->headers
        ->set('Permissions-Policy', 'interest-cohort=()');
    }
  }

}

Classes

Namesort descending Description
ResponseSubscriber Analytics ResponseSubscriber.