Handles ensuring only one instance of each type is created and that the correct connection is injected in. Provides an interface similar to Cake\ORM\TableRegistry.
 /**
  * Prepare some additional data from the context.
  *
  * If the table option was provided to the constructor and it
  * was a string, TypeRegistry will be used to get the correct table instance.
  *
  * If an object is provided as the type option, it will be used as is.
  *
  * If no type option is provided, the type name will be derived based on
  * naming conventions. This inference will work with a number of common objects
  * like arrays, Collection objects and ResultSets.
  *
  * @return void
  * @throws \RuntimeException When a table object cannot be located/inferred.
  */
 protected function _prepare()
 {
     $type = $this->_context['type'];
     $entity = $this->_context['entity'];
     if (empty($type)) {
         if (is_array($entity) || $entity instanceof Traversable) {
             $entity = (new Collection($entity))->first();
         }
         $isDocument = $entity instanceof Document;
         if ($isDocument) {
             $type = $entity->source();
         }
         if (!$type && $isDocument && get_class($entity) !== 'Cake\\ElasticSearch\\Document') {
             list(, $entityClass) = namespaceSplit(get_class($entity));
             $type = Inflector::pluralize($entityClass);
         }
     }
     if (is_string($type)) {
         $type = TypeRegistry::get($type);
     }
     if (!is_object($type)) {
         throw new RuntimeException('Unable to find type class for current entity');
     }
     $this->_isCollection = is_array($entity) || $entity instanceof Traversable;
     $alias = $this->_rootName = $type->name();
     $this->_context['type'] = $type;
 }
 /**
  * Returns the Repository object to use
  *
  * @return AuditStash\Model\Type\AuditLogsType;
  */
 protected function _table()
 {
     return $this->_controller()->AuditLogs = TypeRegistry::get('AuditStash.AuditLogs');
 }
 /**
  * Tests that metadata is correctly stored
  *
  * @return void
  */
 public function testLogEventWithMetadata()
 {
     $client = ConnectionManager::get('test_elastic');
     $persister = new ElasticSearchPersister();
     $persister->connection($client);
     $events[] = new AuditDeleteEvent('1234', 50, 'articles', 'authors');
     $events[0]->setMetaInfo(['a' => 'b', 'c' => 'd']);
     $persister->logEvents($events);
     $client->getIndex()->refresh();
     $articles = TypeRegistry::get('Articles')->find()->toArray();
     $this->assertCount(1, $articles);
     $this->assertEquals(['a' => 'b', 'c' => 'd'], $articles[0]->meta);
 }