You are here

public function ProvisionService::provision in LTI Tool Provider 2.x

Same name and namespace in other branches
  1. 8 modules/lti_tool_provider_provision/src/Services/ProvisionService.php \Drupal\lti_tool_provider_provision\Services\ProvisionService::provision()

Parameters

array $context:

Return value

EntityInterface

Throws

Exception

File

modules/lti_tool_provider_provision/src/Services/ProvisionService.php, line 66

Class

ProvisionService

Namespace

Drupal\lti_tool_provider_provision\Services

Code

public function provision(array $context) : EntityInterface {
  $entityType = $this->config
    ->get('entity_type');
  $entityBundle = $this->config
    ->get('entity_bundle');
  if ($entityType && $entityBundle && isset($context['consumer_id']) && !empty($context['consumer_id']) && isset($context['context_id']) && !empty($context['context_id']) && isset($context['resource_link_id']) && !empty($context['resource_link_id'])) {
    $provision = $this
      ->getProvisionFromContext($context);
    if (!$provision) {
      $provision = LtiToolProviderProvision::create();
      if ($provision instanceof LtiToolProviderProvision) {
        $provision
          ->set('consumer_id', $context['consumer_id']);
        $provision
          ->set('context_id', $context['context_id']);
        $provision
          ->set('context_label', $context['context_label']);
        $provision
          ->set('context_title', $context['context_title']);
        $provision
          ->set('resource_link_id', $context['resource_link_id']);
        $provision
          ->set('resource_link_title', $context['resource_link_title']);
        $provision
          ->set('provision_type', $entityType);
        $provision
          ->set('provision_bundle', $entityBundle);
      }
      $event = new LtiToolProviderProvisionCreateProvisionEvent($context, $provision);
      LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $event);
      if ($event
        ->isCancelled()) {
        throw new Exception($event
          ->getMessage());
      }
      $provision = $event
        ->getEntity();
      $provision
        ->save();
    }
    if ($provision instanceof LtiToolProviderProvision) {
      $entity = $provision
        ->get('provision_id')->value ? $this->entityTypeManager
        ->getStorage($provision
        ->get('provision_type')->value)
        ->load($provision
        ->get('provision_id')->value) : NULL;
      if (!$entity) {
        $entity = $this
          ->createProvisionedEntity($context, $provision);
      }
      $entity = $this
        ->syncProvisionedEntity($context, $entity);
      $entity
        ->save();
      $provision
        ->set('provision_id', $entity
        ->id());
      $provision
        ->save();
      return $entity;
    }
  }
  throw new Exception('Unable to provision entity.');
}