You are here

class LtiToolProviderProvisionEventSubscriber in LTI Tool Provider 2.x

Same name and namespace in other branches
  1. 8 modules/lti_tool_provider_provision/src/EventSubscriber/LtiToolProviderProvisionEventSubscriber.php \Drupal\lti_tool_provider_provision\EventSubscriber\LtiToolProviderProvisionEventSubscriber

Hierarchy

Expanded class hierarchy of LtiToolProviderProvisionEventSubscriber

1 string reference to 'LtiToolProviderProvisionEventSubscriber'
lti_tool_provider_provision.services.yml in modules/lti_tool_provider_provision/lti_tool_provider_provision.services.yml
modules/lti_tool_provider_provision/lti_tool_provider_provision.services.yml
1 service uses LtiToolProviderProvisionEventSubscriber
lti_tool_provider_provision.event_subscriber in modules/lti_tool_provider_provision/lti_tool_provider_provision.services.yml
Drupal\lti_tool_provider_provision\EventSubscriber\LtiToolProviderProvisionEventSubscriber

File

modules/lti_tool_provider_provision/src/EventSubscriber/LtiToolProviderProvisionEventSubscriber.php, line 16

Namespace

Drupal\lti_tool_provider_provision\EventSubscriber
View source
class LtiToolProviderProvisionEventSubscriber implements EventSubscriberInterface {

  /**
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @var EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * @var ProvisionService
   */
  private $provisionService;

  /**
   * LtiToolProviderProvisionEventSubscriber constructor.
   *
   * @param ConfigFactoryInterface $configFactory
   * @param ProvisionService $provisionService
   * @param EventDispatcherInterface $eventDispatcher
   */
  public function __construct(ConfigFactoryInterface $configFactory, ProvisionService $provisionService, EventDispatcherInterface $eventDispatcher) {
    $this->configFactory = $configFactory;
    $this->provisionService = $provisionService;
    $this->eventDispatcher = $eventDispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    return [
      LtiToolProviderLaunchRedirectEvent::EVENT_NAME => 'onLaunch',
      LtiToolProviderProvisionCreateProvisionEvent::EVENT_NAME => 'onCreateProvision',
    ];
  }

  /**
   * @param LtiToolProviderLaunchRedirectEvent $event
   */
  public function onLaunch(LtiToolProviderLaunchRedirectEvent $event) {
    $context = $event
      ->getContext();
    try {
      if ($entity = $this->provisionService
        ->provision($context)) {
        if ($this->configFactory
          ->get('lti_tool_provider_provision.settings')
          ->get('entity_sync')) {
          $this->provisionService
            ->syncProvisionedEntity($context, $entity);
          $entity
            ->save();
        }
        $provisionEvent = new LtiToolProviderProvisionEvent($context, $entity, $entity
          ->toUrl()
          ->toString());
        LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $provisionEvent);
        if ($provisionEvent
          ->isCancelled()) {
          throw new Exception($event
            ->getMessage());
        }
        if ($this->configFactory
          ->get('lti_tool_provider_provision.settings')
          ->get('entity_redirect')) {
          $event
            ->setDestination($provisionEvent
            ->getDestination());
        }
      }
    } catch (Exception $e) {
      $event
        ->sendLtiError($context, $e
        ->getMessage());
      Drupal::logger('lti_tool_provider_provision')
        ->error($e
        ->getMessage());
    }
  }

  /**
   * @param LtiToolProviderProvisionCreateProvisionEvent $event
   */
  public function onCreateProvision(LtiToolProviderProvisionCreateProvisionEvent $event) {
    $context = $event
      ->getContext();
    $access = TRUE;
    if ($this->configFactory
      ->get('lti_tool_provider_provision.settings')
      ->get('allowed_roles_enabled')) {
      $access = FALSE;
      $ltiRoles = parse_roles($context['roles']);
      $allowedRoles = $this->configFactory
        ->get('lti_tool_provider_provision.settings')
        ->get('allowed_roles');
      foreach ($ltiRoles as $ltiRole) {
        if (isset($allowedRoles[$ltiRole]) && $allowedRoles[$ltiRole]) {
          $access = TRUE;
          break;
        }
      }
    }
    if (!$access) {
      $event
        ->cancel('Unable to provision entity.');
    }
  }

}

Members