7
7
namespace Magento \Framework \Mview \View ;
8
8
9
9
use Magento \Framework \App \ResourceConnection ;
10
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
10
11
use Magento \Framework \DB \Ddl \Trigger ;
11
- use Magento \Framework \Mview \View \StateInterface ;
12
+ use Magento \Framework \DB \Ddl \TriggerFactory ;
13
+ use Magento \Framework \Mview \ViewInterface ;
12
14
13
15
/**
14
16
* Mview subscription.
@@ -18,17 +20,17 @@ class Subscription implements SubscriptionInterface
18
20
/**
19
21
* Database connection
20
22
*
21
- * @var \Magento\Framework\DB\Adapter\ AdapterInterface
23
+ * @var AdapterInterface
22
24
*/
23
25
protected $ connection ;
24
26
25
27
/**
26
- * @var \Magento\Framework\DB\Ddl\ TriggerFactory
28
+ * @var TriggerFactory
27
29
*/
28
30
protected $ triggerFactory ;
29
31
30
32
/**
31
- * @var \Magento\Framework\Mview\View\ CollectionInterface
33
+ * @var CollectionInterface
32
34
*/
33
35
protected $ viewCollection ;
34
36
@@ -60,7 +62,7 @@ class Subscription implements SubscriptionInterface
60
62
*
61
63
* @var array
62
64
*/
63
- private $ ignoredUpdateColumns = [] ;
65
+ private $ ignoredUpdateColumns ;
64
66
65
67
/**
66
68
* @var Resource
@@ -69,18 +71,18 @@ class Subscription implements SubscriptionInterface
69
71
70
72
/**
71
73
* @param ResourceConnection $resource
72
- * @param \Magento\Framework\DB\Ddl\ TriggerFactory $triggerFactory
73
- * @param \Magento\Framework\Mview\View\ CollectionInterface $viewCollection
74
- * @param \Magento\Framework\Mview\ ViewInterface $view
74
+ * @param TriggerFactory $triggerFactory
75
+ * @param CollectionInterface $viewCollection
76
+ * @param ViewInterface $view
75
77
* @param string $tableName
76
78
* @param string $columnName
77
79
* @param array $ignoredUpdateColumns
78
80
*/
79
81
public function __construct (
80
82
ResourceConnection $ resource ,
81
- \ Magento \ Framework \ DB \ Ddl \ TriggerFactory $ triggerFactory ,
82
- \ Magento \ Framework \ Mview \ View \ CollectionInterface $ viewCollection ,
83
- \ Magento \ Framework \ Mview \ ViewInterface $ view ,
83
+ TriggerFactory $ triggerFactory ,
84
+ CollectionInterface $ viewCollection ,
85
+ ViewInterface $ view ,
84
86
$ tableName ,
85
87
$ columnName ,
86
88
$ ignoredUpdateColumns = []
@@ -96,9 +98,9 @@ public function __construct(
96
98
}
97
99
98
100
/**
99
- * Create subsciption
101
+ * Create subscription
100
102
*
101
- * @return \Magento\Framework\Mview\View\ SubscriptionInterface
103
+ * @return SubscriptionInterface
102
104
*/
103
105
public function create ()
104
106
{
@@ -115,7 +117,7 @@ public function create()
115
117
116
118
// Add statements for linked views
117
119
foreach ($ this ->getLinkedViews () as $ view ) {
118
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
120
+ /** @var ViewInterface $view */
119
121
$ trigger ->addStatement ($ this ->buildStatement ($ event , $ view ));
120
122
}
121
123
@@ -129,7 +131,7 @@ public function create()
129
131
/**
130
132
* Remove subscription
131
133
*
132
- * @return \Magento\Framework\Mview\View\ SubscriptionInterface
134
+ * @return SubscriptionInterface
133
135
*/
134
136
public function remove ()
135
137
{
@@ -144,7 +146,7 @@ public function remove()
144
146
145
147
// Add statements for linked views
146
148
foreach ($ this ->getLinkedViews () as $ view ) {
147
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
149
+ /** @var ViewInterface $view */
148
150
$ trigger ->addStatement ($ this ->buildStatement ($ event , $ view ));
149
151
}
150
152
@@ -170,13 +172,13 @@ protected function getLinkedViews()
170
172
$ viewList = $ this ->viewCollection ->getViewsByStateMode (StateInterface::MODE_ENABLED );
171
173
172
174
foreach ($ viewList as $ view ) {
173
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
175
+ /** @var ViewInterface $view */
174
176
// Skip the current view
175
177
if ($ view ->getId () == $ this ->getView ()->getId ()) {
176
178
continue ;
177
179
}
178
180
// Search in view subscriptions
179
- foreach ($ view ->getSubscriptions () ?? [] as $ subscription ) {
181
+ foreach ($ view ->getSubscriptions () as $ subscription ) {
180
182
if ($ subscription ['name ' ] != $ this ->getTableName ()) {
181
183
continue ;
182
184
}
@@ -191,21 +193,12 @@ protected function getLinkedViews()
191
193
* Build trigger statement for INSERT, UPDATE, DELETE events
192
194
*
193
195
* @param string $event
194
- * @param \Magento\Framework\Mview\ ViewInterface $view
196
+ * @param ViewInterface $view
195
197
* @return string
196
198
*/
197
199
protected function buildStatement ($ event , $ view )
198
200
{
199
- // Get the subscription for the specific view and specific table.
200
- // We will use column name from it.
201
- $ subscriptions = $ view ->getSubscriptions () ?? [];
202
- if (empty ($ subscriptions [$ this ->getTableName ()])) {
203
- return '' ;
204
- }
205
-
206
- $ subscription = $ subscriptions [$ this ->getTableName ()];
207
-
208
- // Get the changelog from View to get changelog column name.
201
+ $ column = $ this ->getSubscriptionColumn ($ view );
209
202
$ changelog = $ view ->getChangelog ();
210
203
211
204
switch ($ event ) {
@@ -242,14 +235,31 @@ protected function buildStatement($event, $view)
242
235
default :
243
236
return '' ;
244
237
}
238
+
245
239
return sprintf (
246
240
$ trigger ,
247
241
$ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
248
242
$ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
249
- $ this ->connection ->quoteIdentifier ($ subscription [ ' column ' ] )
243
+ $ this ->connection ->quoteIdentifier ($ column )
250
244
);
251
245
}
252
246
247
+ /**
248
+ * Returns subscription column name by view
249
+ *
250
+ * @param ViewInterface $view
251
+ * @return string
252
+ */
253
+ private function getSubscriptionColumn (ViewInterface $ view ): string
254
+ {
255
+ $ subscriptions = $ view ->getSubscriptions ();
256
+ if (!isset ($ subscriptions [$ this ->getTableName ()]['column ' ])) {
257
+ throw new \RuntimeException (sprintf ('Column name for view with id "%s" doesn \'t exist ' , $ view ->getId ()));
258
+ }
259
+
260
+ return $ subscriptions [$ this ->getTableName ()]['column ' ];
261
+ }
262
+
253
263
/**
254
264
* Build an "after" event for the given table and event
255
265
*
@@ -269,7 +279,7 @@ private function getAfterEventTriggerName($event)
269
279
/**
270
280
* Retrieve View related to subscription
271
281
*
272
- * @return \Magento\Framework\Mview\ ViewInterface
282
+ * @return ViewInterface
273
283
* @codeCoverageIgnore
274
284
*/
275
285
public function getView ()
0 commit comments