PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/module-staging/Model/UpdateRepository.php

https://bitbucket.org/sergiu-tot-fb/vendors
PHP | 256 lines | 153 code | 18 blank | 85 comment | 20 complexity | 1abec758bdd45e4f9369a11d96a8c7ef MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Staging\Model;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Staging\Api\Data\UpdateSearchResultInterfaceFactory as SearchResultFactory;
  9. use Magento\Staging\Api\UpdateRepositoryInterface;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Framework\Exception\CouldNotSaveException;
  12. use Magento\Framework\Exception\CouldNotDeleteException;
  13. use Magento\Framework\Exception\ValidatorException;
  14. use Magento\Staging\Model\ResourceModel\Update as UpdateResource;
  15. use Magento\Staging\Api\Data\UpdateInterface;
  16. use Magento\Staging\Model\Update\Validator;
  17. use Magento\Staging\Model\VersionHistoryInterface;
  18. /**
  19. * Class UpdateRepository
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class UpdateRepository implements UpdateRepositoryInterface
  23. {
  24. /**
  25. * @var UpdateInterface[]
  26. */
  27. protected $registry = [];
  28. /**
  29. * @var SearchResultFactory
  30. */
  31. protected $searchResultFactory;
  32. /**
  33. * @var UpdateResource
  34. */
  35. protected $resource;
  36. /**
  37. * @var UpdateFactory
  38. */
  39. protected $updateFactory;
  40. /**
  41. * @var Validator
  42. */
  43. protected $validator;
  44. /**
  45. * @var VersionHistoryInterface
  46. */
  47. protected $versionHistory;
  48. /**
  49. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  50. */
  51. private $collectionProcessor;
  52. /**
  53. * UpdateRepository constructor.
  54. * @param SearchResultFactory $searchResultFactory
  55. * @param UpdateResource $resource
  56. * @param UpdateFactory $updateFactory
  57. * @param Validator $validator
  58. * @param \Magento\Staging\Model\VersionHistoryInterface $versionHistory
  59. * @param CollectionProcessorInterface|null $collectionProcessor
  60. */
  61. public function __construct(
  62. SearchResultFactory $searchResultFactory,
  63. UpdateResource $resource,
  64. UpdateFactory $updateFactory,
  65. Validator $validator,
  66. VersionHistoryInterface $versionHistory,
  67. CollectionProcessorInterface $collectionProcessor = null
  68. ) {
  69. $this->searchResultFactory = $searchResultFactory;
  70. $this->resource = $resource;
  71. $this->updateFactory = $updateFactory;
  72. $this->validator = $validator;
  73. $this->versionHistory = $versionHistory;
  74. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  75. }
  76. /**
  77. * Loads a specified update.
  78. *
  79. * @param int $id
  80. * @return UpdateInterface
  81. * @throws NoSuchEntityException
  82. */
  83. public function get($id)
  84. {
  85. if (!isset($this->registry[$id])) {
  86. /** @var Update $update */
  87. $update = $this->updateFactory->create();
  88. if ($id == \Magento\Staging\Model\VersionManager::MIN_VERSION) {
  89. $update->setId($id);
  90. } else {
  91. $this->resource->load($update, $id);
  92. if (!$update->getId()) {
  93. throw new NoSuchEntityException(__('Update with id "%1" does not exist.', $id));
  94. }
  95. if ($update->getRollbackId()) {
  96. $update->setEndTime($this->get($update->getRollbackId())->getStartTime());
  97. }
  98. }
  99. $this->registry[$id] = $update;
  100. }
  101. return $this->registry[$id];
  102. }
  103. /**
  104. * Lists updates that match specified search criteria.
  105. *
  106. * @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
  107. * @return \Magento\Staging\Api\Data\UpdateSearchResultInterface
  108. */
  109. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria)
  110. {
  111. $searchResult = $this->searchResultFactory->create();
  112. $searchResult->setSearchCriteria($criteria);
  113. $this->collectionProcessor->process($criteria, $searchResult);
  114. return $searchResult;
  115. }
  116. /**
  117. * Deletes a specified update.
  118. *
  119. * @param UpdateInterface $entity
  120. * @return bool
  121. * @throws CouldNotDeleteException
  122. */
  123. public function delete(UpdateInterface $entity)
  124. {
  125. try {
  126. if ($this->versionHistory->getCurrentId() == $entity->getId()) {
  127. throw new CouldNotDeleteException(__('Active update can not be deleted'));
  128. }
  129. $rollbackId = $entity->getRollbackId();
  130. if ($rollbackId && $rollbackId !== $this->getVersionMaxIdByTime(time())) {
  131. $this->resource->delete($this->get($rollbackId));
  132. }
  133. $this->resource->delete($entity);
  134. } catch (\Exception $exception) {
  135. throw new CouldNotDeleteException(__($exception->getMessage()));
  136. }
  137. return true;
  138. }
  139. /**
  140. * Performs persist operations for a specified update.
  141. *
  142. * @param UpdateInterface $entity
  143. * @return UpdateInterface
  144. * @throws CouldNotSaveException
  145. */
  146. public function save(UpdateInterface $entity)
  147. {
  148. try {
  149. if (!$entity->getId()) {
  150. $this->validator->validateCreate($entity);
  151. $entity->setId($this->getIdForEntity($entity));
  152. $entity->isObjectNew(true);
  153. } else {
  154. $this->validator->validateUpdate($entity);
  155. $oldUpdate = $this->updateFactory->create();
  156. $id = $entity->getId();
  157. $this->resource->load($oldUpdate, $id);
  158. if (strtotime($entity->getStartTime()) != strtotime($oldUpdate->getStartTime())) {
  159. if ($id <= $this->versionHistory->getCurrentId()) {
  160. throw new ValidatorException(__('Start time could not be changed while update active'));
  161. }
  162. $entity->setOldId($oldUpdate->getId());
  163. $entity->setId($this->getIdForEntity($entity));
  164. }
  165. }
  166. if ($entity->getEndTime()) {
  167. $entity->setRollbackId($this->getRollback($entity));
  168. } elseif ($entity->getRollbackId()) {
  169. $this->delete($this->get($entity->getRollbackId()));
  170. $entity->setRollbackId(null);
  171. }
  172. $this->resource->save($entity);
  173. } catch (ValidatorException $exception) {
  174. throw new CouldNotSaveException(__($exception->getMessage()));
  175. } catch (\Exception $exception) {
  176. throw new CouldNotSaveException(__('Unable to save Future Update.'));
  177. }
  178. return $entity;
  179. }
  180. /**
  181. * @param UpdateInterface $entity
  182. * @return int
  183. * @throws CouldNotSaveException
  184. * @throws NoSuchEntityException
  185. */
  186. protected function getRollback(UpdateInterface $entity)
  187. {
  188. if ($entity->getRollbackId()) {
  189. $rollback = $this->get($entity->getRollbackId());
  190. $rollback->setStartTime($entity->getEndTime());
  191. } else {
  192. $rollback = $this->updateFactory->create();
  193. $rollback->setName(sprintf('Rollback for "%s"', $entity->getName()));
  194. $rollback->setStartTime($entity->getEndTime());
  195. $rollback->setIsRollback(true);
  196. }
  197. $rollback = $this->save($rollback);
  198. return $rollback->getId();
  199. }
  200. /**
  201. * @param UpdateInterface $entity
  202. * @return int
  203. */
  204. protected function getIdForEntity(UpdateInterface $entity)
  205. {
  206. $timestamp = strtotime($entity->getStartTime());
  207. try {
  208. $this->get($timestamp);
  209. while (true) {
  210. $this->get(++$timestamp);
  211. }
  212. } catch (NoSuchEntityException $e) {
  213. return $timestamp;
  214. }
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getVersionMaxIdByTime($timestamp)
  220. {
  221. return $this->resource->getMaxIdByTime($timestamp);
  222. }
  223. /**
  224. * Retrieve collection processor
  225. *
  226. * @deprecated 100.2.0
  227. * @return CollectionProcessorInterface
  228. */
  229. private function getCollectionProcessor()
  230. {
  231. if (!$this->collectionProcessor) {
  232. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  233. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  234. );
  235. }
  236. return $this->collectionProcessor;
  237. }
  238. }