|
| 1 | +<?php |
| 2 | +namespace Enqueue\ElasticaBundle\Queue; |
| 3 | + |
| 4 | +use Enqueue\Client\CommandSubscriberInterface; |
| 5 | +use Enqueue\Consumption\QueueSubscriberInterface; |
| 6 | +use Enqueue\Util\JSON; |
| 7 | +use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; |
| 8 | +use FOS\ElasticaBundle\Provider\IndexableInterface; |
| 9 | +use Interop\Queue\PsrContext; |
| 10 | +use Interop\Queue\PsrMessage; |
| 11 | +use Interop\Queue\PsrProcessor; |
| 12 | +use Symfony\Bridge\Doctrine\RegistryInterface; |
| 13 | + |
| 14 | +final class SyncIndexWithDoctrineORMObjectChangeProcessor implements PsrProcessor, CommandSubscriberInterface, QueueSubscriberInterface |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var ObjectPersisterInterface |
| 18 | + */ |
| 19 | + private $objectPersister; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var IndexableInterface |
| 23 | + */ |
| 24 | + private $indexable; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var RegistryInterface |
| 28 | + */ |
| 29 | + private $doctrine; |
| 30 | + |
| 31 | + public function __construct(RegistryInterface $doctrine, ObjectPersisterInterface $objectPersister, IndexableInterface $indexable) |
| 32 | + { |
| 33 | + $this->objectPersister = $objectPersister; |
| 34 | + $this->indexable = $indexable; |
| 35 | + $this->doctrine = $doctrine; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 41 | + public function process(PsrMessage $message, PsrContext $context) |
| 42 | + { |
| 43 | + $data = JSON::decode($message->getBody()); |
| 44 | + |
| 45 | + if (false == isset($data['action'], $data['modelClass'], $data['id'], $data['indexName'], $data['typeName'])) { |
| 46 | + return self::REJECT; |
| 47 | + } |
| 48 | + |
| 49 | + $indexName = $data['indexName']; |
| 50 | + $typeName = $data['typeName']; |
| 51 | + |
| 52 | + $objectRepository = $this->doctrine->getManagerForClass($data['modelClass'])->getRepository($data['modelClass']); |
| 53 | + |
| 54 | + switch ($data['action']) { |
| 55 | + case 'update': |
| 56 | + if (false == $object = $objectRepository->find($data['id'])) { |
| 57 | + $this->objectPersister->deleteById($data['id']); |
| 58 | + |
| 59 | + return self::REJECT; |
| 60 | + } |
| 61 | + |
| 62 | + if ($this->objectPersister->handlesObject($object)) { |
| 63 | + if ($this->indexable->isObjectIndexable($indexName, $typeName, $object)) { |
| 64 | + $this->objectPersister->replaceOne($object); |
| 65 | + } else { |
| 66 | + $this->objectPersister->deleteOne($object); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + break; |
| 71 | + case 'insert': |
| 72 | + if (false == $object = $objectRepository->find($data['id'])) { |
| 73 | + $this->objectPersister->deleteById($data['id']); |
| 74 | + |
| 75 | + return self::REJECT; |
| 76 | + } |
| 77 | + |
| 78 | + if ($this->objectPersister->handlesObject($object) && $this->indexable->isObjectIndexable($indexName, $typeName, $object)) { |
| 79 | + $this->objectPersister->insertOne($object); |
| 80 | + } |
| 81 | + |
| 82 | + break; |
| 83 | + case 'delete': |
| 84 | + $this->objectPersister->deleteById($data['id']); |
| 85 | + |
| 86 | + break; |
| 87 | + default: |
| 88 | + return self::REJECT; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@inheritdoc} |
| 94 | + */ |
| 95 | + public static function getSubscribedCommand() |
| 96 | + { |
| 97 | + return [ |
| 98 | + 'processorName' => Commands::SYNC_INDEX_WITH_DOCTRINE_ORM_OBJECT_CHANGE, |
| 99 | + 'queueName' => Commands::SYNC_INDEX_WITH_DOCTRINE_ORM_OBJECT_CHANGE, |
| 100 | + 'queueNameHardcoded' => true, |
| 101 | + 'exclusive' => true, |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * {@inheritdoc} |
| 107 | + */ |
| 108 | + public static function getSubscribedQueues() |
| 109 | + { |
| 110 | + return [Commands::SYNC_INDEX_WITH_DOCTRINE_ORM_OBJECT_CHANGE]; |
| 111 | + } |
| 112 | +} |
0 commit comments