Merge branch '2.0'
This commit is contained in:
commit
b4ebbc0a55
|
@ -28,17 +28,14 @@ abstract class AbstractListener
|
|||
*/
|
||||
protected $events;
|
||||
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
**/
|
||||
public function __construct(ObjectPersisterInterface $objectPersister, $objectClass, array $events, LoggerInterface $logger = null)
|
||||
public function __construct(ObjectPersisterInterface $objectPersister, $objectClass, array $events)
|
||||
{
|
||||
$this->objectPersister = $objectPersister;
|
||||
$this->objectClass = $objectClass;
|
||||
$this->events = $events;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,15 +46,4 @@ abstract class AbstractListener
|
|||
return $this->events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the failure message if a logger is available
|
||||
*
|
||||
* $param string $message
|
||||
*/
|
||||
protected function logFailure($message)
|
||||
{
|
||||
if (null !== $this->logger) {
|
||||
$this->logger->warn(sprintf('%s: %s', get_class($this), $message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,7 @@ class Listener extends AbstractListener implements EventSubscriber
|
|||
$document = $eventArgs->getDocument();
|
||||
|
||||
if ($document instanceof $this->objectClass) {
|
||||
try {
|
||||
$this->objectPersister->insertOne($document);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFailure($e->getMessage());
|
||||
}
|
||||
$this->objectPersister->insertOne($document);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,11 +22,7 @@ class Listener extends AbstractListener implements EventSubscriber
|
|||
$document = $eventArgs->getDocument();
|
||||
|
||||
if ($document instanceof $this->objectClass) {
|
||||
try {
|
||||
$this->objectPersister->replaceOne($document);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFailure($e->getMessage());
|
||||
}
|
||||
$this->objectPersister->replaceOne($document);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,11 +31,7 @@ class Listener extends AbstractListener implements EventSubscriber
|
|||
$document = $eventArgs->getDocument();
|
||||
|
||||
if ($document instanceof $this->objectClass) {
|
||||
try {
|
||||
$this->objectPersister->deleteOne($document);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFailure($e->getMessage());
|
||||
}
|
||||
$this->objectPersister->deleteOne($document);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,11 +13,7 @@ class Listener extends AbstractListener implements EventSubscriber
|
|||
$entity = $eventArgs->getEntity();
|
||||
|
||||
if ($entity instanceof $this->objectClass) {
|
||||
try {
|
||||
$this->objectPersister->insertOne($entity);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFailure($e->getMessage());
|
||||
}
|
||||
$this->objectPersister->insertOne($entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,11 +22,7 @@ class Listener extends AbstractListener implements EventSubscriber
|
|||
$entity = $eventArgs->getEntity();
|
||||
|
||||
if ($entity instanceof $this->objectClass) {
|
||||
try {
|
||||
$this->objectPersister->replaceOne($entity);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFailure($e->getMessage());
|
||||
}
|
||||
$this->objectPersister->replaceOne($entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,11 +31,7 @@ class Listener extends AbstractListener implements EventSubscriber
|
|||
$entity = $eventArgs->getEntity();
|
||||
|
||||
if ($entity instanceof $this->objectClass) {
|
||||
try {
|
||||
$this->objectPersister->deleteOne($entity);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFailure($e->getMessage());
|
||||
}
|
||||
$this->objectPersister->deleteOne($entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,17 +21,13 @@ class ObjectPersister implements ObjectPersisterInterface
|
|||
protected $transformer;
|
||||
protected $objectClass;
|
||||
protected $fields;
|
||||
protected $logger;
|
||||
protected $throwExceptions;
|
||||
|
||||
public function __construct(Elastica_Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields, LoggerInterface $logger = null, $throwExceptions = true)
|
||||
public function __construct(Elastica_Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->transformer = $transformer;
|
||||
$this->objectClass = $objectClass;
|
||||
$this->fields = $fields;
|
||||
$this->logger = $logger;
|
||||
$this->throwExceptions = true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -42,12 +38,8 @@ class ObjectPersister implements ObjectPersisterInterface
|
|||
*/
|
||||
public function insertOne($object)
|
||||
{
|
||||
try {
|
||||
$document = $this->transformToElasticaDocument($object);
|
||||
$this->type->addDocument($document);
|
||||
} catch (Exception $e) {
|
||||
$this->onError($e);
|
||||
}
|
||||
$document = $this->transformToElasticaDocument($object);
|
||||
$this->type->addDocument($document);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,13 +50,9 @@ class ObjectPersister implements ObjectPersisterInterface
|
|||
**/
|
||||
public function replaceOne($object)
|
||||
{
|
||||
try {
|
||||
$document = $this->transformToElasticaDocument($object);
|
||||
$this->type->deleteById($document->getId());
|
||||
$this->type->addDocument($document);
|
||||
} catch (Exception $e) {
|
||||
$this->onError($e);
|
||||
}
|
||||
$document = $this->transformToElasticaDocument($object);
|
||||
$this->type->deleteById($document->getId());
|
||||
$this->type->addDocument($document);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,12 +63,8 @@ class ObjectPersister implements ObjectPersisterInterface
|
|||
**/
|
||||
public function deleteOne($object)
|
||||
{
|
||||
try {
|
||||
$document = $this->transformToElasticaDocument($object);
|
||||
$this->type->deleteById($document->getId());
|
||||
} catch (Exception $e) {
|
||||
$this->onError($e);
|
||||
}
|
||||
$document = $this->transformToElasticaDocument($object);
|
||||
$this->type->deleteById($document->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,15 +74,11 @@ class ObjectPersister implements ObjectPersisterInterface
|
|||
**/
|
||||
public function insertMany(array $objects)
|
||||
{
|
||||
try {
|
||||
$documents = array();
|
||||
foreach ($objects as $object) {
|
||||
$documents[] = $this->transformToElasticaDocument($object);
|
||||
}
|
||||
$this->type->addDocuments($documents);
|
||||
} catch (Exception $e) {
|
||||
$this->onError($e);
|
||||
$documents = array();
|
||||
foreach ($objects as $object) {
|
||||
$documents[] = $this->transformToElasticaDocument($object);
|
||||
}
|
||||
$this->type->addDocuments($documents);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,20 +92,4 @@ class ObjectPersister implements ObjectPersisterInterface
|
|||
return $this->transformer->transform($object, $this->fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* What to do when an error occurs
|
||||
*
|
||||
* @param Exception
|
||||
**/
|
||||
protected function onError(Exception $exception)
|
||||
{
|
||||
if ($this->throwExceptions) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
if ($this->logger) {
|
||||
$message = sprintf('Elastica object persister failure (%s: %s)', get_class($exception), $exception->getMessage());
|
||||
$this->logger->warn($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,8 +50,6 @@
|
|||
<argument /> <!-- model to elastica transformer -->
|
||||
<argument /> <!-- model -->
|
||||
<argument /> <!-- properties mapping -->
|
||||
<argument type="service" id="logger" on-invalid="null" />
|
||||
<argument>%kernel.debug%</argument>
|
||||
</service>
|
||||
|
||||
<service id="foq_elastica.finder.prototype" class="FOQ\ElasticaBundle\Finder\TransformedFinder" public="true" abstract="true">
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
<argument /> <!-- object persister -->
|
||||
<argument /> <!-- model -->
|
||||
<argument type="collection" /> <!-- events -->
|
||||
<argument type="service" id="logger" on-invalid="null" />
|
||||
</service>
|
||||
|
||||
<service id="foq_elastica.elastica_to_model_transformer.prototype.mongodb" class="FOQ\ElasticaBundle\Doctrine\MongoDB\ElasticaToModelTransformer" public="false">
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
<argument /> <!-- object persister -->
|
||||
<argument /> <!-- model -->
|
||||
<argument type="collection" /> <!-- events -->
|
||||
<argument type="service" id="logger" on-invalid="null" />
|
||||
</service>
|
||||
|
||||
<service id="foq_elastica.elastica_to_model_transformer.prototype.orm" class="FOQ\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer" public="false">
|
||||
|
|
Loading…
Reference in a new issue