You are here

class PardotEventSubscriber in Pardot Integration 2.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/PardotEventSubscriber.php \Drupal\pardot\EventSubscriber\PardotEventSubscriber

Provides a response event subscriber for Pardot.

Sets config value for use in pardot.module.

Hierarchy

  • class \Drupal\pardot\EventSubscriber\PardotEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PardotEventSubscriber

1 string reference to 'PardotEventSubscriber'
pardot.services.yml in ./pardot.services.yml
pardot.services.yml
1 service uses PardotEventSubscriber
pardot_event_subscriber in ./pardot.services.yml
Drupal\pardot\EventSubscriber\PardotEventSubscriber

File

src/EventSubscriber/PardotEventSubscriber.php, line 18

Namespace

Drupal\pardot\EventSubscriber
View source
class PardotEventSubscriber implements EventSubscriberInterface {

  /**
   * The Pardot settings configuration.
   */
  private $config;

  /**
   * User storage.
   */
  private $user_storage;

  /**
   * The condition manager.
   *
   * @var \Drupal\Core\Executable\ExecutableManagerInterface
   */
  private $condition_manager;

  /**
   * @var \Drupal\Core\Session\AccountInterface
   */
  private $account;

  /**
   * Creates a new PardotEventSubscriber.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   *
   * @param \Drupal\Core\Executable\ExecutableManagerInterface $condition_manager
   *   The condition manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, ExecutableManagerInterface $condition_manager) {
    $this->config = $config_factory
      ->get('pardot.settings');
    $this->account = $account;
    $this->user_storage = $entity_type_manager
      ->getStorage('user');
    $this->condition_manager = $condition_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'evaluateTrackingScope',
    ];
    return $events;
  }

  /**
   * Evaluates tracking scoping conditions and sets state setting.
   *
   * @param GetResponseEvent $event
   */
  public function evaluateTrackingScope(GetResponseEvent $event) {

    // Check if Pardot is configured with an account ID.
    if (null !== $this->config
      ->get('account_id')) {

      // Load use role condition configuration and current user.
      $user_role_condition = $this->config
        ->get('user_role_condition');
      $current_user = $this->user_storage
        ->load($this->account
        ->id());

      // Create user_role condition with Pardot user condition configuration.
      // Evaluate using current user context.
      $user_role = $this->condition_manager
        ->createInstance('user_role')
        ->setConfig('roles', $user_role_condition['roles'])
        ->setContextValue('user', $current_user);
      $user_role_result = $user_role
        ->evaluate();

      // Create request_path condition with Pardot path condition configuration.
      $request_path = $this->condition_manager
        ->createInstance('request_path');
      $request_path
        ->setConfiguration($this->config
        ->get('path_condition'));

      // Negate request_path_evaluate() if applicable.
      $is_negated = $request_path
        ->isNegated();
      $request_path_result = $request_path
        ->isNegated() ? !$request_path
        ->evaluate() : $request_path
        ->evaluate();
      if ($user_role_result && $request_path_result) {
        \Drupal::state()
          ->set('pardot.include_tracking', 1);
      }
      else {
        \Drupal::state()
          ->set('pardot.include_tracking', 0);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PardotEventSubscriber::$account private property
PardotEventSubscriber::$condition_manager private property The condition manager.
PardotEventSubscriber::$config private property The Pardot settings configuration.
PardotEventSubscriber::$user_storage private property User storage.
PardotEventSubscriber::evaluateTrackingScope public function Evaluates tracking scoping conditions and sets state setting.
PardotEventSubscriber::getSubscribedEvents public static function
PardotEventSubscriber::__construct public function Creates a new PardotEventSubscriber.