PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-indexer/Model/Indexer.php

https://gitlab.com/daigiangaitu91/magento
PHP | 448 lines | 211 code | 40 blank | 197 comment | 19 complexity | d25472345d7ebc94e3e721c7f9cbc462 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Model;
  7. use Magento\Framework\Indexer\ActionFactory;
  8. use Magento\Framework\Indexer\ActionInterface;
  9. use Magento\Framework\Indexer\ConfigInterface;
  10. use Magento\Framework\Indexer\IndexerInterface as IdxInterface;
  11. use Magento\Framework\Indexer\IndexStructureInterface;
  12. use Magento\Framework\Indexer\StateInterface;
  13. use Magento\Framework\Indexer\StructureFactory;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Indexer extends \Magento\Framework\DataObject implements IdxInterface
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $_idFieldName = 'indexer_id';
  23. /**
  24. * @var ConfigInterface
  25. */
  26. protected $config;
  27. /**
  28. * @var ActionFactory
  29. */
  30. protected $actionFactory;
  31. /**
  32. * @var StructureFactory
  33. */
  34. protected $structureFactory;
  35. /**
  36. * @var \Magento\Framework\Mview\ViewInterface
  37. */
  38. protected $view;
  39. /**
  40. * @var \Magento\Indexer\Model\Indexer\StateFactory
  41. */
  42. protected $stateFactory;
  43. /**
  44. * @var \Magento\Indexer\Model\Indexer\State
  45. */
  46. protected $state;
  47. /**
  48. * @var Indexer\CollectionFactory
  49. */
  50. protected $indexersFactory;
  51. /**
  52. * @param ConfigInterface $config
  53. * @param ActionFactory $actionFactory
  54. * @param StructureFactory $structureFactory
  55. * @param \Magento\Framework\Mview\ViewInterface $view
  56. * @param Indexer\StateFactory $stateFactory
  57. * @param Indexer\CollectionFactory $indexersFactory
  58. * @param array $data
  59. */
  60. public function __construct(
  61. ConfigInterface $config,
  62. ActionFactory $actionFactory,
  63. StructureFactory $structureFactory,
  64. \Magento\Framework\Mview\ViewInterface $view,
  65. Indexer\StateFactory $stateFactory,
  66. Indexer\CollectionFactory $indexersFactory,
  67. array $data = []
  68. ) {
  69. $this->config = $config;
  70. $this->actionFactory = $actionFactory;
  71. $this->structureFactory = $structureFactory;
  72. $this->view = $view;
  73. $this->stateFactory = $stateFactory;
  74. $this->indexersFactory = $indexersFactory;
  75. parent::__construct($data);
  76. }
  77. /**
  78. * Return ID
  79. *
  80. * @codeCoverageIgnore
  81. *
  82. * @return string
  83. */
  84. public function getId()
  85. {
  86. return $this->getData($this->_idFieldName);
  87. }
  88. /**
  89. * Set ID
  90. *
  91. * @codeCoverageIgnore
  92. *
  93. * @param string $id
  94. * @return $this
  95. */
  96. public function setId($id)
  97. {
  98. $this->setData($this->_idFieldName, $id);
  99. return $this;
  100. }
  101. /**
  102. * Id field name setter
  103. *
  104. * @codeCoverageIgnore
  105. *
  106. * @param string $name
  107. * @return $this
  108. */
  109. public function setIdFieldName($name)
  110. {
  111. $this->_idFieldName = $name;
  112. return $this;
  113. }
  114. /**
  115. * Id field name getter
  116. *
  117. * @codeCoverageIgnore
  118. *
  119. * @return string
  120. */
  121. public function getIdFieldName()
  122. {
  123. return $this->_idFieldName;
  124. }
  125. /**
  126. * Return indexer's view ID
  127. *
  128. * @return string
  129. */
  130. public function getViewId()
  131. {
  132. return $this->getData('view_id');
  133. }
  134. /**
  135. * Return indexer action class
  136. *
  137. * @return string
  138. */
  139. public function getActionClass()
  140. {
  141. return $this->getData('action_class');
  142. }
  143. /**
  144. * Return indexer title
  145. *
  146. * @return string
  147. */
  148. public function getTitle()
  149. {
  150. return $this->getData('title');
  151. }
  152. /**
  153. * Return indexer description
  154. *
  155. * @return string
  156. */
  157. public function getDescription()
  158. {
  159. return $this->getData('description');
  160. }
  161. /**
  162. * Return indexer fields
  163. *
  164. * @return array
  165. */
  166. public function getFields()
  167. {
  168. return $this->getData('fields');
  169. }
  170. /**
  171. * Return indexer sources
  172. *
  173. * @return array
  174. */
  175. public function getSources()
  176. {
  177. return $this->getData('sources');
  178. }
  179. /**
  180. * Return indexer handlers
  181. *
  182. * @return array
  183. */
  184. public function getHandlers()
  185. {
  186. return $this->getData('handlers');
  187. }
  188. /**
  189. * Fill indexer data from config
  190. *
  191. * @param string $indexerId
  192. * @return IdxInterface
  193. * @throws \InvalidArgumentException
  194. */
  195. public function load($indexerId)
  196. {
  197. $indexer = $this->config->getIndexer($indexerId);
  198. if (empty($indexer) || empty($indexer['indexer_id']) || $indexer['indexer_id'] != $indexerId) {
  199. throw new \InvalidArgumentException("{$indexerId} indexer does not exist.");
  200. }
  201. $this->setId($indexerId);
  202. $this->setData($indexer);
  203. return $this;
  204. }
  205. /**
  206. * Return related view object
  207. *
  208. * @return \Magento\Framework\Mview\ViewInterface
  209. */
  210. public function getView()
  211. {
  212. if (!$this->view->getId()) {
  213. $this->view->load($this->getViewId());
  214. }
  215. return $this->view;
  216. }
  217. /**
  218. * Return related state object
  219. *
  220. * @return StateInterface
  221. */
  222. public function getState()
  223. {
  224. if (!$this->state) {
  225. $this->state = $this->stateFactory->create();
  226. $this->state->loadByIndexer($this->getId());
  227. }
  228. return $this->state;
  229. }
  230. /**
  231. * Set indexer state object
  232. *
  233. * @param StateInterface $state
  234. * @return IdxInterface
  235. */
  236. public function setState(StateInterface $state)
  237. {
  238. $this->state = $state;
  239. return $this;
  240. }
  241. /**
  242. * Check whether indexer is run by schedule
  243. *
  244. * @return bool
  245. */
  246. public function isScheduled()
  247. {
  248. return $this->getView()->isEnabled();
  249. }
  250. /**
  251. * Turn scheduled mode on/off
  252. *
  253. * @param bool $scheduled
  254. * @return void
  255. */
  256. public function setScheduled($scheduled)
  257. {
  258. if ($scheduled) {
  259. $this->getView()->subscribe();
  260. } else {
  261. $this->getView()->unsubscribe();
  262. }
  263. $this->getState()->save();
  264. }
  265. /**
  266. * Check whether indexer is valid
  267. *
  268. * @return bool
  269. */
  270. public function isValid()
  271. {
  272. return $this->getState()->getStatus() == StateInterface::STATUS_VALID;
  273. }
  274. /**
  275. * Check whether indexer is invalid
  276. *
  277. * @return bool
  278. */
  279. public function isInvalid()
  280. {
  281. return $this->getState()->getStatus() == StateInterface::STATUS_INVALID;
  282. }
  283. /**
  284. * Check whether indexer is working
  285. *
  286. * @return bool
  287. */
  288. public function isWorking()
  289. {
  290. return $this->getState()->getStatus() == StateInterface::STATUS_WORKING;
  291. }
  292. /**
  293. * Set indexer invalid
  294. *
  295. * @return void
  296. */
  297. public function invalidate()
  298. {
  299. $state = $this->getState();
  300. $state->setStatus(StateInterface::STATUS_INVALID);
  301. $state->save();
  302. }
  303. /**
  304. * Return indexer status
  305. *
  306. * @return string
  307. */
  308. public function getStatus()
  309. {
  310. return $this->getState()->getStatus();
  311. }
  312. /**
  313. * Return indexer or mview latest updated time
  314. *
  315. * @return string
  316. */
  317. public function getLatestUpdated()
  318. {
  319. if ($this->getView()->isEnabled() && $this->getView()->getUpdated()) {
  320. if (!$this->getState()->getUpdated()) {
  321. return $this->getView()->getUpdated();
  322. }
  323. $indexerUpdatedDate = new \DateTime($this->getState()->getUpdated());
  324. $viewUpdatedDate = new \DateTime($this->getView()->getUpdated());
  325. if ($viewUpdatedDate > $indexerUpdatedDate) {
  326. return $this->getView()->getUpdated();
  327. }
  328. }
  329. return $this->getState()->getUpdated();
  330. }
  331. /**
  332. * Return indexer action instance
  333. *
  334. * @return ActionInterface
  335. */
  336. protected function getActionInstance()
  337. {
  338. return $this->actionFactory->create(
  339. $this->getActionClass(),
  340. [
  341. 'indexStructure' => $this->getStructureInstance(),
  342. 'data' => $this->getData(),
  343. ]
  344. );
  345. }
  346. /**
  347. * Return indexer structure instance
  348. *
  349. * @return IndexStructureInterface
  350. */
  351. protected function getStructureInstance()
  352. {
  353. if (!$this->getData('structure')) {
  354. return null;
  355. }
  356. return $this->structureFactory->create($this->getData('structure'));
  357. }
  358. /**
  359. * Regenerate full index
  360. *
  361. * @return void
  362. * @throws \Exception
  363. */
  364. public function reindexAll()
  365. {
  366. if ($this->getState()->getStatus() != StateInterface::STATUS_WORKING) {
  367. $state = $this->getState();
  368. $state->setStatus(StateInterface::STATUS_WORKING);
  369. $state->save();
  370. if ($this->getView()->isEnabled()) {
  371. $this->getView()->suspend();
  372. }
  373. try {
  374. $this->getActionInstance()->executeFull();
  375. $state->setStatus(StateInterface::STATUS_VALID);
  376. $state->save();
  377. $this->getView()->resume();
  378. } catch (\Exception $exception) {
  379. $state->setStatus(StateInterface::STATUS_INVALID);
  380. $state->save();
  381. $this->getView()->resume();
  382. throw $exception;
  383. }
  384. }
  385. }
  386. /**
  387. * Regenerate one row in index by ID
  388. *
  389. * @param int $id
  390. * @return void
  391. */
  392. public function reindexRow($id)
  393. {
  394. $this->getActionInstance()->executeRow($id);
  395. $this->getState()->save();
  396. }
  397. /**
  398. * Regenerate rows in index by ID list
  399. *5
  400. * @param int[] $ids
  401. * @return void
  402. */
  403. public function reindexList($ids)
  404. {
  405. $this->getActionInstance()->executeList($ids);
  406. $this->getState()->save();
  407. }
  408. }