|
| 1 | +<?php |
| 2 | +namespace Enqueue\ElasticaBundle\Doctrine\Queue; |
| 3 | + |
| 4 | +use Enqueue\Client\CommandSubscriberInterface; |
| 5 | +use Enqueue\Consumption\QueueSubscriberInterface; |
| 6 | +use Enqueue\Consumption\Result; |
| 7 | +use Enqueue\Util\JSON; |
| 8 | +use FOS\ElasticaBundle\Persister\PersisterRegistry; |
| 9 | +use FOS\ElasticaBundle\Provider\IndexableInterface; |
| 10 | +use Interop\Queue\PsrContext; |
| 11 | +use Interop\Queue\PsrMessage; |
| 12 | +use Interop\Queue\PsrProcessor; |
| 13 | +use Symfony\Bridge\Doctrine\RegistryInterface; |
| 14 | + |
| 15 | +final class SyncIndexWithObjectChangeProcessor implements PsrProcessor, CommandSubscriberInterface, QueueSubscriberInterface |
| 16 | +{ |
| 17 | + const INSERT_ACTION = 'insert'; |
| 18 | + |
| 19 | + const UPDATE_ACTION = 'update'; |
| 20 | + |
| 21 | + const REMOVE_ACTION = 'remove'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var PersisterRegistry |
| 25 | + */ |
| 26 | + private $persisterRegistry; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var IndexableInterface |
| 30 | + */ |
| 31 | + private $indexable; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var RegistryInterface |
| 35 | + */ |
| 36 | + private $doctrine; |
| 37 | + |
| 38 | + public function __construct(RegistryInterface $doctrine, PersisterRegistry $persisterRegistry, IndexableInterface $indexable) |
| 39 | + { |
| 40 | + $this->persisterRegistry = $persisterRegistry; |
| 41 | + $this->indexable = $indexable; |
| 42 | + $this->doctrine = $doctrine; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + */ |
| 48 | + public function process(PsrMessage $message, PsrContext $context) |
| 49 | + { |
| 50 | + $data = JSON::decode($message->getBody()); |
| 51 | + |
| 52 | + if (false == isset($data['action'])) { |
| 53 | + return Result::reject('The message data misses action'); |
| 54 | + } |
| 55 | + if (false == isset($data['model_class'])) { |
| 56 | + return Result::reject('The message data misses model_class'); |
| 57 | + } |
| 58 | + if (false == isset($data['id'])) { |
| 59 | + return Result::reject('The message data misses id'); |
| 60 | + } |
| 61 | + if (false == isset($data['index_name'])) { |
| 62 | + return Result::reject('The message data misses id'); |
| 63 | + } |
| 64 | + if (false == isset($data['type_name'])) { |
| 65 | + return Result::reject('The message data misses id'); |
| 66 | + } |
| 67 | + |
| 68 | + $action = $data['action']; |
| 69 | + $modelClass = $data['model_class']; |
| 70 | + $id = $data['id']; |
| 71 | + $index = $data['index_name']; |
| 72 | + $type = $data['type_name']; |
| 73 | + |
| 74 | + $repository = $this->doctrine->getManagerForClass($modelClass)->getRepository($modelClass); |
| 75 | + $persister = $this->persisterRegistry->getPersister($index, $type); |
| 76 | + |
| 77 | + switch ($action) { |
| 78 | + case self::UPDATE_ACTION: |
| 79 | + if (false == $object = $repository->find($id)) { |
| 80 | + $persister->deleteById($id); |
| 81 | + |
| 82 | + return Result::ack(sprintf('The object "%s" with id "%s" could not be found.', $modelClass, $id)); |
| 83 | + } |
| 84 | + |
| 85 | + if ($persister->handlesObject($object)) { |
| 86 | + if ($this->indexable->isObjectIndexable($index, $type, $object)) { |
| 87 | + $persister->replaceOne($object); |
| 88 | + } else { |
| 89 | + $persister->deleteOne($object); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return self::ACK; |
| 94 | + case self::INSERT_ACTION: |
| 95 | + if (false == $object = $repository->find($id)) { |
| 96 | + $persister->deleteById($id); |
| 97 | + |
| 98 | + return Result::ack(sprintf('The object "%s" with id "%s" could not be found.', $modelClass, $id)); |
| 99 | + } |
| 100 | + |
| 101 | + if ($persister->handlesObject($object) && $this->indexable->isObjectIndexable($index, $type, $object)) { |
| 102 | + $persister->insertOne($object); |
| 103 | + } |
| 104 | + |
| 105 | + return self::ACK; |
| 106 | + case self::REMOVE_ACTION: |
| 107 | + $persister->deleteById($id); |
| 108 | + |
| 109 | + return self::ACK; |
| 110 | + default: |
| 111 | + return Result::reject(sprintf('The action "%s" is not supported', $action)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * {@inheritdoc} |
| 117 | + */ |
| 118 | + public static function getSubscribedCommand() |
| 119 | + { |
| 120 | + return [ |
| 121 | + 'processorName' => Commands::SYNC_INDEX_WITH_OBJECT_CHANGE, |
| 122 | + 'queueName' => Commands::SYNC_INDEX_WITH_OBJECT_CHANGE, |
| 123 | + 'queueNameHardcoded' => true, |
| 124 | + 'exclusive' => true, |
| 125 | + ]; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * {@inheritdoc} |
| 130 | + */ |
| 131 | + public static function getSubscribedQueues() |
| 132 | + { |
| 133 | + return [Commands::SYNC_INDEX_WITH_OBJECT_CHANGE]; |
| 134 | + } |
| 135 | +} |
0 commit comments