1
1
<?php
2
+
3
+ declare (strict_types=1 );
4
+
2
5
/**
3
6
* Copyright © Magento, Inc. All rights reserved.
4
7
* See COPYING.txt for license details.
5
8
*/
6
9
namespace Magento \MessageQueue \Model \ResourceModel ;
7
10
8
- use \Magento \Framework \MessageQueue \Lock \ReaderInterface ;
9
- use \Magento \Framework \MessageQueue \Lock \WriterInterface ;
11
+ use DateInterval ;
12
+ use DateTime ;
13
+ use Exception ;
14
+ use Magento \Framework \MessageQueue \Lock \ReaderInterface ;
15
+ use Magento \Framework \MessageQueue \Lock \WriterInterface ;
16
+ use Magento \Framework \MessageQueue \LockInterface ;
17
+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
18
+ use Magento \Framework \Model \ResourceModel \Db \Context ;
19
+ use Magento \Framework \Stdlib \DateTime \DateTime as MagentoDateTime ;
20
+ use Magento \MessageQueue \Model \Lock as LockModel ;
21
+ use Magento \MessageQueue \Model \LockFactory ;
10
22
11
23
/**
12
24
* Class Lock to handle database lock table db transactions.
13
25
*/
14
- class Lock extends \ Magento \ Framework \ Model \ ResourceModel \ Db \ AbstractDb implements ReaderInterface, WriterInterface
26
+ class Lock extends AbstractDb implements ReaderInterface, WriterInterface
15
27
{
16
- /**#@+
17
- * Constants
28
+ /**
29
+ * @var string
18
30
*/
19
- const QUEUE_LOCK_TABLE = 'queue_lock ' ;
20
- /**#@-*/
31
+ private const QUEUE_LOCK_TABLE = 'queue_lock ' ;
21
32
22
- /**#@-*/
33
+ /**
34
+ * @var MagentoDateTime
35
+ */
23
36
private $ dateTime ;
24
37
25
38
/**
26
- * @var \Magento\MessageQueue\Model\ LockFactory
39
+ * @var LockFactory
27
40
*/
28
41
private $ lockFactory ;
29
42
@@ -35,18 +48,18 @@ class Lock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implemen
35
48
/**
36
49
* Initialize dependencies.
37
50
*
38
- * @param \Magento\Framework\Model\ResourceModel\Db\ Context $context
39
- * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
40
- * @param \Magento\MessageQueue\Model\ LockFactory $lockFactory
41
- * @param null $connectionName
51
+ * @param Context $context
52
+ * @param MagentoDateTime $dateTime
53
+ * @param LockFactory $lockFactory
54
+ * @param string| null $connectionName
42
55
* @param integer $interval
43
56
*/
44
57
public function __construct (
45
- \ Magento \ Framework \ Model \ ResourceModel \ Db \ Context $ context ,
46
- \ Magento \ Framework \ Stdlib \ DateTime \ DateTime $ dateTime ,
47
- \ Magento \ MessageQueue \ Model \ LockFactory $ lockFactory ,
48
- $ connectionName = null ,
49
- $ interval = 86400
58
+ Context $ context ,
59
+ MagentoDateTime $ dateTime ,
60
+ LockFactory $ lockFactory ,
61
+ ? string $ connectionName = null ,
62
+ int $ interval = 86400
50
63
) {
51
64
$ this ->lockFactory = $ lockFactory ;
52
65
$ this ->interval = $ interval ;
@@ -55,43 +68,63 @@ public function __construct(
55
68
}
56
69
57
70
/**
58
- * {@inheritDoc}
71
+ * Init.
72
+ *
73
+ * @return void
74
+ *
75
+ * @codeCoverageIgnore
76
+ * @SuppressWarnings(PHPMD.CamelCaseMethodName)
59
77
*/
60
- protected function _construct ()
78
+ protected function _construct (): void
61
79
{
62
80
$ this ->_init (self ::QUEUE_LOCK_TABLE , 'id ' );
63
81
}
64
82
65
83
/**
66
- * {@inheritDoc}
84
+ * Read lock
85
+ *
86
+ * @param LockInterface $lock
87
+ * @param string $code
88
+ * @return void
67
89
*/
68
- public function read (\ Magento \ Framework \ MessageQueue \ LockInterface $ lock , $ code )
90
+ public function read (LockInterface $ lock , string $ code ): void
69
91
{
92
+ /** @var $object LockModel */
70
93
$ object = $ this ->lockFactory ->create ();
71
- $ object ->load ($ code , 'message_code ' );
94
+ $ this ->load ($ object , $ code , 'message_code ' );
72
95
$ lock ->setId ($ object ->getId ());
73
96
$ lock ->setMessageCode ($ object ->getMessageCode () ?: $ code );
74
97
$ lock ->setCreatedAt ($ object ->getCreatedAt ());
75
98
}
76
99
77
100
/**
78
- * {@inheritDoc}
101
+ * Save lock
102
+ *
103
+ * @param LockInterface $lock
104
+ *
105
+ * @return void
106
+ * @throws Exception
79
107
*/
80
- public function saveLock (\ Magento \ Framework \ MessageQueue \ LockInterface $ lock )
108
+ public function saveLock (LockInterface $ lock ): void
81
109
{
110
+ /** @var $object LockModel */
82
111
$ object = $ this ->lockFactory ->create ();
83
112
$ object ->setMessageCode ($ lock ->getMessageCode ());
84
113
$ object ->setCreatedAt ($ this ->dateTime ->gmtTimestamp ());
85
- $ object ->save ();
114
+ $ this ->save ($ object );
115
+ $ lock ->setId ($ object ->getId ());
86
116
}
87
117
88
118
/**
89
- * {@inheritDoc}
119
+ * Remove outdated locks
120
+ *
121
+ * @return void
122
+ * @throws Exception
90
123
*/
91
- public function releaseOutdatedLocks ()
124
+ public function releaseOutdatedLocks (): void
92
125
{
93
- $ date = (new \ DateTime ())->setTimestamp ($ this ->dateTime ->gmtTimestamp ());
94
- $ date ->add (new \ DateInterval ('PT ' . $ this ->interval . 'S ' ));
126
+ $ date = (new DateTime ())->setTimestamp ($ this ->dateTime ->gmtTimestamp ());
127
+ $ date ->add (new DateInterval ('PT ' . $ this ->interval . 'S ' ));
95
128
$ this ->getConnection ()->delete ($ this ->getTable (self ::QUEUE_LOCK_TABLE ), ['created_at <= ? ' => $ date ]);
96
129
}
97
130
}
0 commit comments