You are here

class RequestSubscriber in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_user_sync/src/EventSubscriber/RequestSubscriber.php \Drupal\crm_core_user_sync\EventSubscriber\RequestSubscriber

CRM Core User Synchronization event subscriber.

Hierarchy

  • class \Drupal\crm_core_user_sync\EventSubscriber\RequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RequestSubscriber

1 file declares its use of RequestSubscriber
RequestSubscriberTest.php in modules/crm_core_user_sync/tests/src/Unit/RequestSubscriberTest.php
1 string reference to 'RequestSubscriber'
crm_core_user_sync.services.yml in modules/crm_core_user_sync/crm_core_user_sync.services.yml
modules/crm_core_user_sync/crm_core_user_sync.services.yml
1 service uses RequestSubscriber
crm_core_user_sync.request.event_subscriber in modules/crm_core_user_sync/crm_core_user_sync.services.yml
Drupal\crm_core_user_sync\EventSubscriber\RequestSubscriber

File

modules/crm_core_user_sync/src/EventSubscriber/RequestSubscriber.php, line 16

Namespace

Drupal\crm_core_user_sync\EventSubscriber
View source
class RequestSubscriber implements EventSubscriberInterface {

  /**
   * Current logged in user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Relation service.
   *
   * @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface
   */
  protected $relationService;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs event subscriber.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   Current logged in user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface $relation_service
   *   The relation service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   */
  public function __construct(AccountProxyInterface $current_user, ConfigFactoryInterface $config_factory, CrmCoreUserSyncRelationInterface $relation_service, EntityTypeManagerInterface $entityTypeManager) {
    $this->currentUser = $current_user;
    $this->configFactory = $config_factory;
    $this->relationService = $relation_service;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Kernel request event handler.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Response event.
   */
  public function onKernelRequest(GetResponseEvent $event) {
    if ($this->currentUser
      ->isAuthenticated()) {
      $config = $this->configFactory
        ->get('crm_core_user_sync.settings');
      if ($config
        ->get('contact_load')) {
        $individual_id = $this->relationService
          ->getUserIndividualId($this->currentUser
          ->id());
        if ($individual_id) {
          $individual = $this->entityTypeManager
            ->getStorage('crm_core_individual')
            ->load($individual_id);
          $account = $this->currentUser
            ->getAccount();
          $account->crm_core['contact'] = $individual;
          $this->currentUser
            ->setAccount($account);
        }
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
RequestSubscriber::$configFactory protected property The configuration factory.
RequestSubscriber::$currentUser protected property Current logged in user.
RequestSubscriber::$entityTypeManager protected property Entity type manager.
RequestSubscriber::$relationService protected property Relation service.
RequestSubscriber::getSubscribedEvents public static function
RequestSubscriber::onKernelRequest public function Kernel request event handler.
RequestSubscriber::__construct public function Constructs event subscriber.