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;
$this->_rootName = $type->name();
$this->_context['type'] = $type;
}