lockFactory = $lockFactory; $this->reader = $reader; $this->writer = $writer; } /** * Create lock corresponding to the provided message. Throw MessageLockException if lock is already created. * * @param EnvelopeInterface $envelope * @param string $consumerName * @return LockInterface * @throws MessageLockException * @throws NotFoundException */ public function lock(EnvelopeInterface $envelope, $consumerName) { $lock = $this->lockFactory->create(); $properties = $envelope->getProperties(); if (empty($properties['message_id'])) { throw new NotFoundException(new Phrase("Property 'message_id' not found in properties.")); } $code = $consumerName . '-' . $properties['message_id']; $code = md5($code); $this->reader->read($lock, $code); if ($lock->getId()) { throw new MessageLockException(new Phrase('The "%1" message code was already processed.', [$code])); } $this->writer->saveLock($lock); return $lock; } }