77
88namespace Magento \MessageQueue \Model \ResourceModel ;
99
10- use Magento \MessageQueue \Api \PoisonPillReadInterface ;
11- use Magento \MessageQueue \Api \PoisonPillPutInterface ;
12- use Magento \Framework \Model \ResourceModel \Db \Context ;
10+ use Magento \Framework \MessageQueue \PoisonPill \PoisonPillPutInterface ;
11+ use Magento \Framework \MessageQueue \PoisonPill \PoisonPillReadInterface ;
1312use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
1413
1514/**
16- * PoisonPill.
15+ * PoisonPill class that enclose read and put interface .
1716 */
1817class PoisonPill extends AbstractDb implements PoisonPillPutInterface, PoisonPillReadInterface
1918{
@@ -22,19 +21,6 @@ class PoisonPill extends AbstractDb implements PoisonPillPutInterface, PoisonPil
2221 */
2322 const QUEUE_POISON_PILL_TABLE = 'queue_poison_pill ' ;
2423
25- /**
26- * PoisonPill constructor.
27- *
28- * @param Context $context
29- * @param string|null $connectionName
30- */
31- public function __construct (
32- Context $ context ,
33- string $ connectionName = null
34- ) {
35- parent ::__construct ($ context , $ connectionName );
36- }
37-
3824 /**
3925 * @inheritdoc
4026 */
@@ -46,30 +32,43 @@ protected function _construct()
4632 /**
4733 * @inheritdoc
4834 */
49- public function put (): int
35+ public function put (): string
5036 {
5137 $ connection = $ this ->getConnection ();
5238 $ table = $ this ->getMainTable ();
53- $ connection ->insert ($ table , []);
54- return (int )$ connection ->lastInsertId ($ table );
39+ $ uuid = uniqid ('version- ' );
40+ $ version = $ this ->getVersionFromDb ();
41+ if ($ version !== '' ) {
42+ $ connection ->update ($ table , ['version ' => $ uuid ]);
43+ } else {
44+ $ connection ->insert ($ table , ['version ' => $ uuid ]);
45+ }
46+
47+ return $ uuid ;
5548 }
5649
5750 /**
5851 * @inheritdoc
5952 */
60- public function getLatestVersion () : int
53+ public function getLatestVersion (): string
54+ {
55+ return $ this ->getVersionFromDb ();
56+ }
57+
58+ /**
59+ * Returns version form DB or null.
60+ *
61+ * @return string
62+ */
63+ private function getVersionFromDb (): string
6164 {
6265 $ select = $ this ->getConnection ()->select ()->from (
6366 $ this ->getTable (self ::QUEUE_POISON_PILL_TABLE ),
6467 'version '
65- )->order (
66- 'version ' . \Magento \Framework \DB \Select::SQL_DESC
67- )->limit (
68- 1
6968 );
7069
71- $ version = ( int ) $ this ->getConnection ()->fetchOne ($ select );
70+ $ result = $ this ->getConnection ()->fetchOne ($ select );
7271
73- return $ version ;
72+ return ( string ) $ result ;
7473 }
7574}
0 commit comments