Maslosoft Manganel API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/manganel
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/manganel/
*/
namespace Maslosoft\Manganel;
use Closure;
use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Mangan;
use Maslosoft\Manganel\Exceptions\ManganelException;
use Maslosoft\Manganel\Helpers\RecursiveFilter;
use Maslosoft\Manganel\Helpers\TypeNamer;
use Maslosoft\Manganel\Meta\ManganelMeta;
use UnexpectedValueException;
/**
* IndexMangager
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class IndexManager
{
/**
* Manganel instance
* @var Manganel
*/
private $manganel = null;
/**
* Model meta data
* @var ManganelMeta
*/
private $meta = null;
/**
* Annotated model
* @var AnnotatedInterface
*/
private $model;
/**
* Whether model is indexable
* @var bool
*/
private $isIndexable = false;
public function __construct($model)
{
$this->model = $model;
$this->meta = ManganelMeta::create($this->model);
if (!empty($this->meta->type()->indexId) && false !== $this->meta->type()->indexId)
{
$this->isIndexable = true;
}
if ($this->isIndexable)
{
if (!isset($this->model->_id))
{
throw new ManganelException(sprintf('Property `_id` is not set in model `%s`, this is required by Manganel', get_class($this->model)));
}
$this->manganel = Manganel::create($this->model);
}
}
public function index()
{
if (!$this->isIndexable)
{
return;
}
// NOTE: Transformer must ensure that _id is string, not MongoId
$body = SearchArray::fromModel($this->model);
if (array_key_exists('_id', $body))
{
$config = Mangan::fromModel($this->model)->sanitizersMap;
if (!array_key_exists(SearchArray::class, $config))
{
throw new UnexpectedValueException(sprintf('Mangan is not properly configured for Manganel. Signals must be generated or add configuration manually from `%s::getDefault()`', ConfigManager::class));
}
else
{
throw new UnexpectedValueException(sprintf('Cannot index `%s`, as it contains _id field. Either use MongoObjectId sanitizer on it, or rename.', get_class($this->model)));
}
}
// Create proper elastic search request array
$params = [
'body' => RecursiveFilter::mongoIdToString($body),
];
try
{
$fullParams = $this->getParams($params);
// Need to check if exists, or update will fail
$existsParams = [
'index' => $fullParams['index'],
'type' => $fullParams['type'],
'id' => $fullParams['id']
];
$exists = $this->getClient()->exists($existsParams);
if (!$exists)
{
$result = $this->getClient()->index($fullParams);
}
else
{
$updateParams = $fullParams;
$updateParams['body'] = [
'doc' => $fullParams['body']
];
$result = $this->getClient()->update($updateParams);
}
if (array_key_exists('result', $result) && $result['result'] === 'updated')
{
// For ES 5
return true;
}
elseif (is_array($result))
{
// For earlier ES
return true;
}
}
catch (BadRequest400Exception $e)
{
// Throw previous exception,
// as it holds more meaningfull information
$previous = $e->getPrevious();
$message = sprintf('Exception while indexing `%s`@`%s`: %s', get_class($this->model), $this->manganel->indexId, $previous->getMessage());
throw new BadRequest400Exception($message, 400, $e);
}
return false;
}
public function delete()
{
if (!$this->isIndexable)
{
return;
}
$this->getClient()->delete($this->getParams());
}
public function get($id = null)
{
if (!$this->isIndexable)
{
return;
}
$params = $id ? ['id' => (string) $id] : [];
$data = $this->getClient()->get($this->getParams($params))['_source'];
return SearchArray::toModel($data);
}
/**
* Get client
* @return Client
*/
public function getClient()
{
return $this->manganel->getClient();
}
private function getParams($params = [])
{
// Check refresh option
if ($this->manganel->refresh instanceof Closure)
{
$func = $this->manganel->refresh;
$refresh = (bool) $func($this->model);
}
else
{
$refresh = $this->manganel->refresh;
}
$result = [
'index' => strtolower($this->manganel->index),
'type' => TypeNamer::nameType($this->model),
'id' => (string) $this->model->_id,
'refresh' => $refresh
];
return array_merge($result, $params);
}
}
API documentation generated by ApiGen