Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Indexer.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Indexer\Model;
7 
15 
20 {
24  protected $_idFieldName = 'indexer_id';
25 
29  protected $config;
30 
34  protected $actionFactory;
35 
39  protected $structureFactory;
40 
44  protected $view;
45 
49  protected $stateFactory;
50 
54  protected $state;
55 
59  protected $indexersFactory;
60 
70  public function __construct(
74  \Magento\Framework\Mview\ViewInterface $view,
75  Indexer\StateFactory $stateFactory,
76  Indexer\CollectionFactory $indexersFactory,
77  array $data = []
78  ) {
79  $this->config = $config;
80  $this->actionFactory = $actionFactory;
81  $this->structureFactory = $structureFactory;
82  $this->view = $view;
83  $this->stateFactory = $stateFactory;
84  $this->indexersFactory = $indexersFactory;
85  parent::__construct($data);
86  }
87 
95  public function getId()
96  {
97  return $this->getData($this->_idFieldName);
98  }
99 
108  public function setId($id)
109  {
110  $this->setData($this->_idFieldName, $id);
111  return $this;
112  }
113 
122  public function setIdFieldName($name)
123  {
124  $this->_idFieldName = $name;
125  return $this;
126  }
127 
135  public function getIdFieldName()
136  {
137  return $this->_idFieldName;
138  }
139 
145  public function getViewId()
146  {
147  return $this->getData('view_id');
148  }
149 
155  public function getActionClass()
156  {
157  return $this->getData('action_class');
158  }
159 
165  public function getTitle()
166  {
167  return $this->getData('title');
168  }
169 
175  public function getDescription()
176  {
177  return $this->getData('description');
178  }
179 
185  public function getFields()
186  {
187  return $this->getData('fields');
188  }
189 
195  public function getSources()
196  {
197  return $this->getData('sources');
198  }
199 
205  public function getHandlers()
206  {
207  return $this->getData('handlers');
208  }
209 
217  public function load($indexerId)
218  {
219  $indexer = $this->config->getIndexer($indexerId);
220  if (empty($indexer) || empty($indexer['indexer_id']) || $indexer['indexer_id'] != $indexerId) {
221  throw new \InvalidArgumentException("{$indexerId} indexer does not exist.");
222  }
223 
224  $this->setId($indexerId);
225  $this->setData($indexer);
226 
227  return $this;
228  }
229 
235  public function getView()
236  {
237  if (!$this->view->getId()) {
238  $this->view->load($this->getViewId());
239  }
240  return $this->view;
241  }
242 
248  public function getState()
249  {
250  if (!$this->state) {
251  $this->state = $this->stateFactory->create();
252  $this->state->loadByIndexer($this->getId());
253  }
254  return $this->state;
255  }
256 
263  public function setState(StateInterface $state)
264  {
265  $this->state = $state;
266  return $this;
267  }
268 
274  public function isScheduled()
275  {
276  return $this->getView()->isEnabled();
277  }
278 
285  public function setScheduled($scheduled)
286  {
287  if ($scheduled) {
288  $this->getView()->subscribe();
289  } else {
290  $this->getView()->unsubscribe();
291  }
292  $this->getState()->save();
293  }
294 
300  public function isValid()
301  {
302  return $this->getState()->getStatus() == StateInterface::STATUS_VALID;
303  }
304 
310  public function isInvalid()
311  {
312  return $this->getState()->getStatus() == StateInterface::STATUS_INVALID;
313  }
314 
320  public function isWorking()
321  {
322  return $this->getState()->getStatus() == StateInterface::STATUS_WORKING;
323  }
324 
330  public function invalidate()
331  {
332  $state = $this->getState();
334  $state->save();
335  }
336 
342  public function getStatus()
343  {
344  return $this->getState()->getStatus();
345  }
346 
352  public function getLatestUpdated()
353  {
354  if ($this->getView()->isEnabled() && $this->getView()->getUpdated()) {
355  if (!$this->getState()->getUpdated()) {
356  return $this->getView()->getUpdated();
357  }
358  $indexerUpdatedDate = new \DateTime($this->getState()->getUpdated());
359  $viewUpdatedDate = new \DateTime($this->getView()->getUpdated());
360  if ($viewUpdatedDate > $indexerUpdatedDate) {
361  return $this->getView()->getUpdated();
362  }
363  }
364  return $this->getState()->getUpdated();
365  }
366 
373  protected function getActionInstance()
374  {
375  return $this->actionFactory->create(
376  $this->getActionClass(),
377  [
378  'indexStructure' => $this->getStructureInstance(),
379  'data' => $this->getData(),
380  ]
381  );
382  }
383 
389  protected function getStructureInstance()
390  {
391  if (!$this->getData('structure')) {
392  return null;
393  }
394  return $this->structureFactory->create($this->getData('structure'));
395  }
396 
403  public function reindexAll()
404  {
406  $state = $this->getState();
408  $state->save();
409  if ($this->getView()->isEnabled()) {
410  $this->getView()->suspend();
411  }
412  try {
413  $this->getActionInstance()->executeFull();
415  $state->save();
416  $this->getView()->resume();
417  } catch (\Throwable $exception) {
419  $state->save();
420  $this->getView()->resume();
421  throw $exception;
422  }
423  }
424  }
425 
432  public function reindexRow($id)
433  {
434  $this->getActionInstance()->executeRow($id);
435  $this->getState()->save();
436  }
437 
444  public function reindexList($ids)
445  {
446  $this->getActionInstance()->executeList($ids);
447  $this->getState()->save();
448  }
449 }
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(ConfigInterface $config, ActionFactory $actionFactory, StructureFactory $structureFactory, \Magento\Framework\Mview\ViewInterface $view, Indexer\StateFactory $stateFactory, Indexer\CollectionFactory $indexersFactory, array $data=[])
Definition: Indexer.php:70
$id
Definition: fieldset.phtml:14
setState(StateInterface $state)
Definition: Indexer.php:263
setData($key, $value=null)
Definition: DataObject.php:72
if(!isset($_GET['name'])) $name
Definition: log.php:14