You are here

public function NestedSetStorage::__call in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Storage/NestedSetStorage.php \Drupal\entity_hierarchy\Storage\NestedSetStorage::__call()

File

src/Storage/NestedSetStorage.php, line 83

Class

NestedSetStorage
Wraps the library nested set implementation with JIT table creation.

Namespace

Drupal\entity_hierarchy\Storage

Code

public function __call($name, $arguments) {
  $try_again = FALSE;
  try {
    return $this
      ->doCall($name, $arguments);
  } catch (\InvalidArgumentException $e) {
    $this->logger
      ->emergency(sprintf('The nested set table %s is corrupt and needs to be rebuilt. Use drush entity-hierarchy-tree-rebuild command.', $this->tableName));

    // Library can throw InvalidArgumentException. Let's self heal.
    if ($name === 'getNode') {
      return FALSE;
    }
    if ($name === 'findParent') {
      return FALSE;
    }
    if (strpos($name, 'find') === 0) {
      return [];
    }
  } catch (\Exception $e) {

    // If there was an exception, try to create the table.
    if (!($try_again = $this
      ->ensureTableExists())) {

      // If the exception happened for other reason than the missing table,
      // propagate the exception.
      throw $e;
    }
  }

  // Now that the table has been created, try again if necessary.
  if ($try_again) {
    return $this
      ->doCall($name, $arguments);
  }
  throw new \LogicException('Unexpected exception occurred.');
}