You are here

class TawkToConditionPluginsHandler in Tawk.to - Live chat application (Drupal 8) 8.2

Same name and namespace in other branches
  1. 8 src/Service/TawkToConditionPluginsHandler.php \Drupal\tawk_to\Service\TawkToConditionPluginsHandler

Defines the condition plugins handler.

Hierarchy

Expanded class hierarchy of TawkToConditionPluginsHandler

1 file declares its use of TawkToConditionPluginsHandler
TawkToCacheManager.php in src/Cache/TawkToCacheManager.php
1 string reference to 'TawkToConditionPluginsHandler'
tawk_to.services.yml in ./tawk_to.services.yml
tawk_to.services.yml
1 service uses TawkToConditionPluginsHandler
tawk_to.condition_plugins_handler in ./tawk_to.services.yml
Drupal\tawk_to\Service\TawkToConditionPluginsHandler

File

src/Service/TawkToConditionPluginsHandler.php, line 16

Namespace

Drupal\tawk_to\Service
View source
class TawkToConditionPluginsHandler {
  use ConditionAccessResolverTrait;

  /**
   * The plugin context handler.
   *
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
   */
  protected $contextHandler;

  /**
   * The context manager service.
   *
   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
   */
  protected $contextRepository;

  /**
   * The condition plugin manager.
   *
   * @var \Drupal\Core\Condition\ConditionManager
   */
  protected $manager;

  /**
   * The condition plugin defination.
   *
   * @var array
   */
  protected $tawkToVisibility;

  /**
   * Constructs the tawk.to access control handler instance.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $contextHandler
   *   The ContextHandler for applying contexts to conditions properly.
   * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contextRepository
   *   The lazy context repository service.
   * @param \Drupal\Core\Executable\ExecutableManagerInterface $manager
   *   The ConditionManager for building the visibility UI.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ContextHandlerInterface $contextHandler, ContextRepositoryInterface $contextRepository, ExecutableManagerInterface $manager) {
    $this->tawkToVisibility = $configFactory
      ->get('tawk_to.settings')
      ->get('visibility');
    $this->contextHandler = $contextHandler;
    $this->contextRepository = $contextRepository;
    $this->manager = $manager;
  }

  /**
   * {@inheritdoc}
   */
  public function checkAccess() {
    $conditions = [];
    if (!empty($this->tawkToVisibility)) {
      $conditions = $this
        ->getConditions();
      return $this
        ->resolveConditions($conditions, 'and');
    }
    return TRUE;
  }

  /**
   * Gets condition plugins based on the module configuration.
   *
   * @return \Drupal\Core\Condition\ConditionInterface[]
   *   A set of conditions.
   */
  public function getConditions() {
    $conditions = [];
    foreach ($this->tawkToVisibility as $conditionId => $configuration) {
      $condition = $this->manager
        ->createInstance($conditionId, $configuration);
      if ($condition instanceof ContextAwarePluginInterface) {
        try {
          $contextMapping = $condition
            ->getContextMapping();
          if ($contextMapping) {
            $contexts = $this->contextRepository
              ->getRuntimeContexts(array_values($contextMapping));
            $this->contextHandler
              ->applyContextMapping($condition, $contexts);
          }
        } catch (ContextException $e) {

          // @todo: Think the best way to handle this.
        }
      }
      $conditions[$conditionId] = $condition;
    }
    return $conditions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionAccessResolverTrait::resolveConditions protected function Resolves the given conditions based on the condition logic ('and'/'or').
TawkToConditionPluginsHandler::$contextHandler protected property The plugin context handler.
TawkToConditionPluginsHandler::$contextRepository protected property The context manager service.
TawkToConditionPluginsHandler::$manager protected property The condition plugin manager.
TawkToConditionPluginsHandler::$tawkToVisibility protected property The condition plugin defination.
TawkToConditionPluginsHandler::checkAccess public function
TawkToConditionPluginsHandler::getConditions public function Gets condition plugins based on the module configuration.
TawkToConditionPluginsHandler::__construct public function Constructs the tawk.to access control handler instance.