Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
20  const DEFAULT_BATCH_SIZE = 1000;
21 
25  private static $maxVersionQueryBatch = 100000;
26 
30  protected $_idFieldName = 'view_id';
31 
35  protected $config;
36 
40  protected $actionFactory;
41 
45  protected $changelog;
46 
51 
55  protected $state;
56 
60  private $changelogBatchSize;
61 
71  public function __construct(
74  View\StateInterface $state,
75  View\ChangelogInterface $changelog,
77  array $data = [],
78  array $changelogBatchSize = []
79  ) {
80  $this->config = $config;
81  $this->actionFactory = $actionFactory;
82  $this->state = $state;
83  $this->changelog = $changelog;
84  $this->subscriptionFactory = $subscriptionFactory;
85  $this->changelogBatchSize = $changelogBatchSize;
86  parent::__construct($data);
87  }
88 
94  public function getId()
95  {
96  return $this->getData($this->_idFieldName);
97  }
98 
105  public function setId($id)
106  {
107  $this->setData($this->_idFieldName, $id);
108  return $this;
109  }
110 
117  public function setIdFieldName($name)
118  {
119  $this->_idFieldName = $name;
120  return $this;
121  }
122 
128  public function getIdFieldName()
129  {
130  return $this->_idFieldName;
131  }
132 
138  public function getActionClass()
139  {
140  return $this->getData('action_class');
141  }
142 
148  public function getGroup()
149  {
150  return $this->getData('group');
151  }
152 
158  public function getSubscriptions()
159  {
160  return $this->getData('subscriptions');
161  }
162 
170  public function load($viewId)
171  {
172  $view = $this->config->getView($viewId);
173  if (empty($view) || empty($view['view_id']) || $view['view_id'] != $viewId) {
174  throw new \InvalidArgumentException("{$viewId} view does not exist.");
175  }
176 
177  $this->setId($viewId);
178  $this->setData($view);
179 
180  return $this;
181  }
182 
189  public function subscribe()
190  {
191  if ($this->getState()->getMode() != View\StateInterface::MODE_ENABLED) {
192  try {
193  // Create changelog table
194  $this->getChangelog()->create();
195 
196  // Create subscriptions
197  foreach ($this->getSubscriptions() as $subscriptionConfig) {
199  $subscriptionInstance = $this->subscriptionFactory->create(
200  [
201  'view' => $this,
202  'tableName' => $subscriptionConfig['name'],
203  'columnName' => $subscriptionConfig['column'],
204  'subscriptionModel' => !empty($subscriptionConfig['subscription_model'])
205  ? $subscriptionConfig['subscription_model']
207  ]
208  );
209  $subscriptionInstance->create();
210  }
211 
212  // Update view state
213  $this->getState()->setMode(View\StateInterface::MODE_ENABLED)->save();
214  } catch (\Exception $e) {
215  throw $e;
216  }
217  }
218 
219  return $this;
220  }
221 
228  public function unsubscribe()
229  {
230  if ($this->getState()->getMode() != View\StateInterface::MODE_DISABLED) {
231  try {
232  // Remove subscriptions
233  foreach ($this->getSubscriptions() as $subscriptionConfig) {
235  $subscriptionInstance = $this->subscriptionFactory->create(
236  [
237  'view' => $this,
238  'tableName' => $subscriptionConfig['name'],
239  'columnName' => $subscriptionConfig['column'],
240  'subscriptionModel' => !empty($subscriptionConfig['subscriptionModel'])
241  ? $subscriptionConfig['subscriptionModel']
243  ]
244  );
245  $subscriptionInstance->remove();
246  }
247 
248  // Update view state
249  $this->getState()->setMode(View\StateInterface::MODE_DISABLED)->save();
250  } catch (\Exception $e) {
251  throw $e;
252  }
253  }
254 
255  return $this;
256  }
257 
264  public function update()
265  {
266  if ($this->getState()->getStatus() == View\StateInterface::STATUS_IDLE) {
267  try {
268  $currentVersionId = $this->getChangelog()->getVersion();
269  } catch (ChangelogTableNotExistsException $e) {
270  return;
271  }
272  $lastVersionId = (int) $this->getState()->getVersionId();
273  $action = $this->actionFactory->get($this->getActionClass());
274 
275  try {
276  $this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();
277 
278  $versionBatchSize = self::$maxVersionQueryBatch;
279  $batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
280  ? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
282 
283  for ($vsFrom = $lastVersionId; $vsFrom < $currentVersionId; $vsFrom += $versionBatchSize) {
284  // Don't go past the current version for atomicy.
285  $versionTo = min($currentVersionId, $vsFrom + $versionBatchSize);
286  $ids = $this->getChangelog()->getList($vsFrom, $versionTo);
287 
288  // We run the actual indexer in batches.
289  // Chunked AFTER loading to avoid duplicates in separate chunks.
290  $chunks = array_chunk($ids, $batchSize);
291  foreach ($chunks as $ids) {
292  $action->execute($ids);
293  }
294  }
295 
296  $this->getState()->loadByView($this->getId());
297  $statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
300  $this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save();
301  } catch (\Exception $exception) {
302  $this->getState()->loadByView($this->getId());
303  $statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
306  $this->getState()->setStatus($statusToRestore)->save();
307  throw $exception;
308  }
309  }
310  }
311 
317  public function suspend()
318  {
319  if ($this->getState()->getMode() == View\StateInterface::MODE_ENABLED) {
320  $state = $this->getState();
321  $state->setVersionId($this->getChangelog()->getVersion());
323  $state->save();
324  }
325  }
326 
332  public function resume()
333  {
334  $state = $this->getState();
335  if ($state->getStatus() == View\StateInterface::STATUS_SUSPENDED) {
337  $state->save();
338  }
339  }
340 
346  public function clearChangelog()
347  {
348  if ($this->getState()->getMode() == View\StateInterface::MODE_ENABLED) {
349  $this->getChangelog()->clear($this->getState()->getVersionId());
350  }
351  }
352 
358  public function getState()
359  {
360  if (!$this->state->getViewId()) {
361  $this->state->loadByView($this->getId());
362  }
363  return $this->state;
364  }
365 
372  public function setState(View\StateInterface $state)
373  {
374  $this->state = $state;
375  return $this;
376  }
377 
383  public function isEnabled()
384  {
385  return $this->getState()->getMode() == View\StateInterface::MODE_ENABLED;
386  }
387 
393  public function isIdle()
394  {
395  return $this->getState()->getStatus() == \Magento\Framework\Mview\View\StateInterface::STATUS_IDLE;
396  }
397 
403  public function isWorking()
404  {
405  return $this->getState()->getStatus() == \Magento\Framework\Mview\View\StateInterface::STATUS_WORKING;
406  }
407 
413  public function isSuspended()
414  {
415  return $this->getState()->getStatus() == \Magento\Framework\Mview\View\StateInterface::STATUS_SUSPENDED;
416  }
417 
423  public function getUpdated()
424  {
425  return $this->getState()->getUpdated();
426  }
427 
433  public function getChangelog()
434  {
435  if (!$this->changelog->getViewId()) {
436  $this->changelog->setViewId($this->getId());
437  }
438  return $this->changelog;
439  }
440 }
getData($key='', $index=null)
Definition: DataObject.php:119
$id
Definition: fieldset.phtml:14
setState(View\StateInterface $state)
Definition: View.php:372
setData($key, $value=null)
Definition: DataObject.php:72
__construct(ConfigInterface $config, ActionFactory $actionFactory, View\StateInterface $state, View\ChangelogInterface $changelog, SubscriptionFactory $subscriptionFactory, array $data=[], array $changelogBatchSize=[])
Definition: View.php:71
if(!isset($_GET['name'])) $name
Definition: log.php:14