public function ProvisionService::provision in LTI Tool Provider 8
Same name and namespace in other branches
- 2.x modules/lti_tool_provider_provision/src/Services/ProvisionService.php \Drupal\lti_tool_provider_provision\Services\ProvisionService::provision()
Parameters
array $context:
Return value
Throws
Exception
File
- modules/
lti_tool_provider_provision/ src/ Services/ ProvisionService.php, line 64
Class
Namespace
Drupal\lti_tool_provider_provision\ServicesCode
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.');
}