diff --git a/app/code/Magento/MessageQueue/Model/ResourceModel/Lock.php b/app/code/Magento/MessageQueue/Model/ResourceModel/Lock.php index 16c02a7505664..354cc2a9300db 100644 --- a/app/code/Magento/MessageQueue/Model/ResourceModel/Lock.php +++ b/app/code/Magento/MessageQueue/Model/ResourceModel/Lock.php @@ -1,29 +1,42 @@ lockFactory = $lockFactory; $this->interval = $interval; @@ -55,43 +68,63 @@ public function __construct( } /** - * {@inheritDoc} + * Init. + * + * @return void + * + * @codeCoverageIgnore + * @SuppressWarnings(PHPMD.CamelCaseMethodName) */ - protected function _construct() + protected function _construct(): void { $this->_init(self::QUEUE_LOCK_TABLE, 'id'); } /** - * {@inheritDoc} + * Read lock + * + * @param LockInterface $lock + * @param string $code + * @return void */ - public function read(\Magento\Framework\MessageQueue\LockInterface $lock, $code) + public function read(LockInterface $lock, string $code): void { + /** @var $object LockModel */ $object = $this->lockFactory->create(); - $object->load($code, 'message_code'); + $this->load($object, $code, 'message_code'); $lock->setId($object->getId()); $lock->setMessageCode($object->getMessageCode() ?: $code); $lock->setCreatedAt($object->getCreatedAt()); } /** - * {@inheritDoc} + * Save lock + * + * @param LockInterface $lock + * + * @return void + * @throws Exception */ - public function saveLock(\Magento\Framework\MessageQueue\LockInterface $lock) + public function saveLock(LockInterface $lock): void { + /** @var $object LockModel */ $object = $this->lockFactory->create(); $object->setMessageCode($lock->getMessageCode()); $object->setCreatedAt($this->dateTime->gmtTimestamp()); - $object->save(); + $this->save($object); + $lock->setId($object->getId()); } /** - * {@inheritDoc} + * Remove outdated locks + * + * @return void + * @throws Exception */ - public function releaseOutdatedLocks() + public function releaseOutdatedLocks(): void { - $date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp()); - $date->add(new \DateInterval('PT' . $this->interval . 'S')); + $date = (new DateTime())->setTimestamp($this->dateTime->gmtTimestamp()); + $date->add(new DateInterval('PT' . $this->interval . 'S')); $this->getConnection()->delete($this->getTable(self::QUEUE_LOCK_TABLE), ['created_at <= ?' => $date]); } } diff --git a/dev/tests/integration/testsuite/Magento/MessageQueue/Model/Plugin/ResourceModel/LockTest.php b/dev/tests/integration/testsuite/Magento/MessageQueue/Model/Plugin/ResourceModel/LockTest.php index 960114e35be7f..43470fba2c29b 100644 --- a/dev/tests/integration/testsuite/Magento/MessageQueue/Model/Plugin/ResourceModel/LockTest.php +++ b/dev/tests/integration/testsuite/Magento/MessageQueue/Model/Plugin/ResourceModel/LockTest.php @@ -1,4 +1,7 @@ objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->objectManager = Bootstrap::getObjectManager(); - $this->lock = $this->objectManager->get(\Magento\Framework\MessageQueue\LockInterface::class); - $this->writer = $this->objectManager->get(\Magento\Framework\MessageQueue\Lock\WriterInterface::class); - $this->reader = $this->objectManager->get(\Magento\Framework\MessageQueue\Lock\ReaderInterface::class); + $this->lock = $this->objectManager->get(LockInterface::class); + $this->writer = $this->objectManager->get(WriterInterface::class); + $this->reader = $this->objectManager->get(ReaderInterface::class); } /** @@ -44,11 +56,13 @@ protected function setUp() * * @return void */ - public function testLockClearedByMaintenanceModeOff() + public function testLockClearedByMaintenanceModeOff(): void { - /** @var $maintenanceMode \Magento\Framework\App\MaintenanceMode */ - $maintenanceMode = $this->objectManager->get(\Magento\Framework\App\MaintenanceMode::class); + /** @var $maintenanceMode MaintenanceMode */ + $maintenanceMode = $this->objectManager->get(MaintenanceMode::class); + // phpcs:disable $code = md5('consumer.name-1'); + // phpcs:enable $this->lock->setMessageCode($code); $this->writer->saveLock($this->lock); $this->reader->read($this->lock, $code); diff --git a/lib/internal/Magento/Framework/MessageQueue/Lock/ReaderInterface.php b/lib/internal/Magento/Framework/MessageQueue/Lock/ReaderInterface.php index 33490f5137491..7a99b3d0b0a43 100644 --- a/lib/internal/Magento/Framework/MessageQueue/Lock/ReaderInterface.php +++ b/lib/internal/Magento/Framework/MessageQueue/Lock/ReaderInterface.php @@ -1,10 +1,15 @@