FOSElasticaBundle/Doctrine/ORM/Listener.php
r1pp3rj4ck 0f46f4b96d [Listener] Allow conditional indexing based on callback method
Added optional is_indexable_callback config param to persistence. If this is a method on the entity, the listener will only process it if the method returns true. Also updated documentation.
2012-04-10 14:36:25 -04:00

73 lines
2.8 KiB
PHP

<?php
namespace FOQ\ElasticaBundle\Doctrine\ORM;
use FOQ\ElasticaBundle\Doctrine\AbstractListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Common\EventSubscriber;
class Listener extends AbstractListener implements EventSubscriber
{
public function postPersist(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof $this->objectClass) {
if ($this->isIndexableCallback && !is_callable(array($entity, $this->isIndexableCallback))) {
if (method_exists($entity, $this->isIndexableCallback)) {
$exception = sprintf('The specified check method %s::%s is out of scope.', $this->objectClass, $this->isIndexableCallback);
} else {
$exception = sprintf('The specified check method %s::%s does not exist', $this->objectClass, $this->isIndexableCallback);
}
throw new \RuntimeException($exception);
}
if (($this->isIndexableCallback && call_user_func(array($entity, $this->isIndexableCallback))) || !$this->isIndexableCallback) {
$this->objectPersister->insertOne($entity);
}
}
}
public function postUpdate(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof $this->objectClass) {
if ($this->isIndexableCallback && !is_callable(array($entity, $this->isIndexableCallback))) {
if (method_exists($entity, $this->isIndexableCallback)) {
$exception = sprintf('The specified check method %s::%s is out of scope.', $this->objectClass, $this->isIndexableCallback);
} else {
$exception = sprintf('The specified check method %s::%s does not exist', $this->objectClass, $this->isIndexableCallback);
}
throw new \RuntimeException($exception);
}
if (($this->isIndexableCallback && call_user_func(array($entity, $this->isIndexableCallback))) || !$this->isIndexableCallback) {
$this->objectPersister->replaceOne($entity);
} else {
$this->scheduleForRemoval($entity, $eventArgs->getEntityManager());
$this->removeIfScheduled($entity);
}
}
}
public function preRemove(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof $this->objectClass) {
$this->scheduleForRemoval($entity, $eventArgs->getEntityManager());
}
}
public function postRemove(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof $this->objectClass) {
$this->removeIfScheduled($entity);
}
}
}