Skip to content

Use the identifier and not the entire object for sendUpdateIndexMessage #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions Doctrine/SyncIndexWithObjectChangeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,38 @@ public function __construct(Context $context, $modelClass, array $config)
public function postUpdate(LifecycleEventArgs $args)
{
if ($args->getObject() instanceof $this->modelClass) {
$this->scheduledForUpdateIndex[] = ['action' => SyncProcessor::UPDATE_ACTION, 'args' => $args];
$this->scheduledForUpdateIndex[] = [
'action' => SyncProcessor::UPDATE_ACTION,
'id' => $this->extractId($args->getObject())
];
}
}

public function postPersist(LifecycleEventArgs $args)
{
if ($args->getObject() instanceof $this->modelClass) {
$this->scheduledForUpdateIndex[] = ['action' => SyncProcessor::INSERT_ACTION, 'args' => $args];
$this->scheduledForUpdateIndex[] = [
'action' => SyncProcessor::INSERT_ACTION,
'id' => $this->extractId($args->getObject())
];
}
}

public function preRemove(LifecycleEventArgs $args)
{
if ($args->getObject() instanceof $this->modelClass) {
$this->scheduledForUpdateIndex[] = ['action' => SyncProcessor::REMOVE_ACTION, 'args' => $args];
$this->scheduledForUpdateIndex[] = [
'action' => SyncProcessor::REMOVE_ACTION,
'id' => $this->extractId($args->getObject())
];
}
}

public function postFlush(PostFlushEventArgs $event)
{
if (count($this->scheduledForUpdateIndex)) {
foreach ($this->scheduledForUpdateIndex as $updateIndex) {
$this->sendUpdateIndexMessage($updateIndex['action'], $updateIndex['args']);
$this->sendUpdateIndexMessage($updateIndex['action'], $updateIndex['id']);
}

$this->scheduledForUpdateIndex = [];
Expand All @@ -79,17 +88,10 @@ public function getSubscribedEvents()

/**
* @param string $action
* @param LifecycleEventArgs $args
* @param $id
*/
private function sendUpdateIndexMessage($action, LifecycleEventArgs $args)
private function sendUpdateIndexMessage($action, $id)
{
$object = $args->getObject();

$rp = (new \ReflectionClass($this->modelClass))->getProperty($this->config['model_id']);
$rp->setAccessible(true);
$id = $rp->getValue($object);
$rp->setAccessible(false);

$queue = $this->context->createQueue(Commands::SYNC_INDEX_WITH_OBJECT_CHANGE);

$message = $this->context->createMessage(JSON::encode([
Expand All @@ -104,4 +106,18 @@ private function sendUpdateIndexMessage($action, LifecycleEventArgs $args)

$this->context->createProducer()->send($queue, $message);
}

/**
* @param $object
* @return mixed
*/
private function extractId($object)
{
$rp = new \ReflectionProperty($object, $this->config['model_id']);
$rp->setAccessible(true);
$id = $rp->getValue($object);
$rp->setAccessible(false);

return $id;
}
}