Skip to content

Commit 4c1094a

Browse files
extracted getting column to separate method
1 parent 330d595 commit 4c1094a

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

lib/internal/Magento/Framework/Mview/View/Subscription.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
namespace Magento\Framework\Mview\View;
88

99
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Adapter\AdapterInterface;
1011
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;
1214

1315
/**
1416
* Mview subscription.
@@ -18,17 +20,17 @@ class Subscription implements SubscriptionInterface
1820
/**
1921
* Database connection
2022
*
21-
* @var \Magento\Framework\DB\Adapter\AdapterInterface
23+
* @var AdapterInterface
2224
*/
2325
protected $connection;
2426

2527
/**
26-
* @var \Magento\Framework\DB\Ddl\TriggerFactory
28+
* @var TriggerFactory
2729
*/
2830
protected $triggerFactory;
2931

3032
/**
31-
* @var \Magento\Framework\Mview\View\CollectionInterface
33+
* @var CollectionInterface
3234
*/
3335
protected $viewCollection;
3436

@@ -60,7 +62,7 @@ class Subscription implements SubscriptionInterface
6062
*
6163
* @var array
6264
*/
63-
private $ignoredUpdateColumns = [];
65+
private $ignoredUpdateColumns;
6466

6567
/**
6668
* @var Resource
@@ -69,18 +71,18 @@ class Subscription implements SubscriptionInterface
6971

7072
/**
7173
* @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
7577
* @param string $tableName
7678
* @param string $columnName
7779
* @param array $ignoredUpdateColumns
7880
*/
7981
public function __construct(
8082
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,
8486
$tableName,
8587
$columnName,
8688
$ignoredUpdateColumns = []
@@ -96,9 +98,9 @@ public function __construct(
9698
}
9799

98100
/**
99-
* Create subsciption
101+
* Create subscription
100102
*
101-
* @return \Magento\Framework\Mview\View\SubscriptionInterface
103+
* @return SubscriptionInterface
102104
*/
103105
public function create()
104106
{
@@ -115,7 +117,7 @@ public function create()
115117

116118
// Add statements for linked views
117119
foreach ($this->getLinkedViews() as $view) {
118-
/** @var \Magento\Framework\Mview\ViewInterface $view */
120+
/** @var ViewInterface $view */
119121
$trigger->addStatement($this->buildStatement($event, $view));
120122
}
121123

@@ -129,7 +131,7 @@ public function create()
129131
/**
130132
* Remove subscription
131133
*
132-
* @return \Magento\Framework\Mview\View\SubscriptionInterface
134+
* @return SubscriptionInterface
133135
*/
134136
public function remove()
135137
{
@@ -144,7 +146,7 @@ public function remove()
144146

145147
// Add statements for linked views
146148
foreach ($this->getLinkedViews() as $view) {
147-
/** @var \Magento\Framework\Mview\ViewInterface $view */
149+
/** @var ViewInterface $view */
148150
$trigger->addStatement($this->buildStatement($event, $view));
149151
}
150152

@@ -170,13 +172,13 @@ protected function getLinkedViews()
170172
$viewList = $this->viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED);
171173

172174
foreach ($viewList as $view) {
173-
/** @var \Magento\Framework\Mview\ViewInterface $view */
175+
/** @var ViewInterface $view */
174176
// Skip the current view
175177
if ($view->getId() == $this->getView()->getId()) {
176178
continue;
177179
}
178180
// Search in view subscriptions
179-
foreach ($view->getSubscriptions() ?? [] as $subscription) {
181+
foreach ($view->getSubscriptions() as $subscription) {
180182
if ($subscription['name'] != $this->getTableName()) {
181183
continue;
182184
}
@@ -191,21 +193,12 @@ protected function getLinkedViews()
191193
* Build trigger statement for INSERT, UPDATE, DELETE events
192194
*
193195
* @param string $event
194-
* @param \Magento\Framework\Mview\ViewInterface $view
196+
* @param ViewInterface $view
195197
* @return string
196198
*/
197199
protected function buildStatement($event, $view)
198200
{
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);
209202
$changelog = $view->getChangelog();
210203

211204
switch ($event) {
@@ -242,14 +235,31 @@ protected function buildStatement($event, $view)
242235
default:
243236
return '';
244237
}
238+
245239
return sprintf(
246240
$trigger,
247241
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
248242
$this->connection->quoteIdentifier($changelog->getColumnName()),
249-
$this->connection->quoteIdentifier($subscription['column'])
243+
$this->connection->quoteIdentifier($column)
250244
);
251245
}
252246

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+
253263
/**
254264
* Build an "after" event for the given table and event
255265
*
@@ -269,7 +279,7 @@ private function getAfterEventTriggerName($event)
269279
/**
270280
* Retrieve View related to subscription
271281
*
272-
* @return \Magento\Framework\Mview\ViewInterface
282+
* @return ViewInterface
273283
* @codeCoverageIgnore
274284
*/
275285
public function getView()

0 commit comments

Comments
 (0)