Repository.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Tax\Model\TaxClass;
  8. use Magento\Framework\Api\FilterBuilder;
  9. use Magento\Framework\Api\Search\FilterGroup;
  10. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. use Magento\Framework\Exception\CouldNotDeleteException;
  13. use Magento\Framework\Exception\InputException;
  14. use Magento\Framework\Exception\LocalizedException as ModelException;
  15. use Magento\Tax\Api\TaxClassManagementInterface;
  16. use Magento\Tax\Model\ClassModel;
  17. use Magento\Tax\Model\ClassModelRegistry;
  18. use Magento\Tax\Model\ResourceModel\TaxClass\Collection as TaxClassCollection;
  19. use Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory as TaxClassCollectionFactory;
  20. /**
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class Repository implements \Magento\Tax\Api\TaxClassRepositoryInterface
  24. {
  25. /**
  26. * @var TaxClassCollectionFactory
  27. */
  28. protected $taxClassCollectionFactory;
  29. /**
  30. * @var \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory
  31. */
  32. protected $searchResultsFactory;
  33. /**
  34. * @var ClassModelRegistry
  35. */
  36. protected $classModelRegistry;
  37. const CLASS_ID_NOT_ALLOWED = 'class_id is not expected for this request.';
  38. /**
  39. * Search Criteria Builder
  40. *
  41. * @var SearchCriteriaBuilder
  42. */
  43. protected $searchCriteriaBuilder;
  44. /**
  45. * Filter Builder
  46. *
  47. * @var FilterBuilder
  48. */
  49. protected $filterBuilder;
  50. /**
  51. * @var \Magento\Tax\Model\ResourceModel\TaxClass
  52. */
  53. protected $taxClassResource;
  54. /**
  55. * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
  56. */
  57. protected $joinProcessor;
  58. /**
  59. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  60. */
  61. private $collectionProcessor;
  62. /**
  63. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  64. * @param FilterBuilder $filterBuilder
  65. * @param TaxClassCollectionFactory $taxClassCollectionFactory
  66. * @param \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory $searchResultsFactory
  67. * @param ClassModelRegistry $classModelRegistry
  68. * @param \Magento\Tax\Model\ResourceModel\TaxClass $taxClassResource
  69. * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
  70. * @param CollectionProcessorInterface $collectionProcessor
  71. */
  72. public function __construct(
  73. SearchCriteriaBuilder $searchCriteriaBuilder,
  74. FilterBuilder $filterBuilder,
  75. TaxClassCollectionFactory $taxClassCollectionFactory,
  76. \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory $searchResultsFactory,
  77. ClassModelRegistry $classModelRegistry,
  78. \Magento\Tax\Model\ResourceModel\TaxClass $taxClassResource,
  79. \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
  80. CollectionProcessorInterface $collectionProcessor = null
  81. ) {
  82. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  83. $this->filterBuilder = $filterBuilder;
  84. $this->taxClassCollectionFactory = $taxClassCollectionFactory;
  85. $this->searchResultsFactory = $searchResultsFactory;
  86. $this->classModelRegistry = $classModelRegistry;
  87. $this->taxClassResource = $taxClassResource;
  88. $this->joinProcessor = $joinProcessor;
  89. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function save(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
  95. {
  96. if ($taxClass->getClassId()) {
  97. $originalTaxClassModel = $this->get($taxClass->getClassId());
  98. /* should not be allowed to switch the tax class type */
  99. if ($originalTaxClassModel->getClassType() !== $taxClass->getClassType()) {
  100. throw new InputException(__('Updating classType is not allowed.'));
  101. }
  102. }
  103. $this->validateTaxClassData($taxClass);
  104. try {
  105. $this->taxClassResource->save($taxClass);
  106. } catch (ModelException $e) {
  107. if (strpos($e->getMessage(), (string)__('Class name and class type')) !== false) {
  108. throw new InputException(
  109. __(
  110. 'A class with the same name already exists for ClassType %1.',
  111. $taxClass->getClassType()
  112. )
  113. );
  114. } else {
  115. throw $e;
  116. }
  117. }
  118. $this->classModelRegistry->registerTaxClass($taxClass);
  119. return $taxClass->getClassId();
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function get($taxClassId)
  125. {
  126. return $this->classModelRegistry->retrieve($taxClassId);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function delete(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
  132. {
  133. $taxClassId = $taxClass->getClassId();
  134. try {
  135. $this->taxClassResource->delete($taxClass);
  136. } catch (CouldNotDeleteException $e) {
  137. throw $e;
  138. } catch (\Exception $e) {
  139. return false;
  140. }
  141. $this->classModelRegistry->remove($taxClassId);
  142. return true;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function deleteById($taxClassId)
  148. {
  149. $taxClassModel = $this->get($taxClassId);
  150. return $this->delete($taxClassModel);
  151. }
  152. /**
  153. * Validate TaxClass Data
  154. *
  155. * @param \Magento\Tax\Api\Data\TaxClassInterface $taxClass
  156. * @return void
  157. * @throws InputException
  158. */
  159. protected function validateTaxClassData(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
  160. {
  161. $exception = new InputException();
  162. if (!\Zend_Validate::is(trim($taxClass->getClassName()), 'NotEmpty')) {
  163. $exception->addError(
  164. __('"%fieldName" is required. Enter and try again.', ['fieldName' => ClassModel::KEY_NAME])
  165. );
  166. }
  167. $classType = $taxClass->getClassType();
  168. if (!\Zend_Validate::is(trim($classType), 'NotEmpty')) {
  169. $exception->addError(
  170. __('"%fieldName" is required. Enter and try again.', ['fieldName' => ClassModel::KEY_TYPE])
  171. );
  172. } elseif ($classType !== TaxClassManagementInterface::TYPE_CUSTOMER
  173. && $classType !== TaxClassManagementInterface::TYPE_PRODUCT
  174. ) {
  175. $exception->addError(
  176. __(
  177. 'Invalid value of "%value" provided for the %fieldName field.',
  178. ['fieldName' => ClassModel::KEY_TYPE, 'value' => $classType]
  179. )
  180. );
  181. }
  182. if ($exception->wasErrorAdded()) {
  183. throw $exception;
  184. }
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  190. {
  191. $searchResults = $this->searchResultsFactory->create();
  192. $searchResults->setSearchCriteria($searchCriteria);
  193. /** @var TaxClassCollection $collection */
  194. $collection = $this->taxClassCollectionFactory->create();
  195. $this->joinProcessor->process($collection);
  196. $this->collectionProcessor->process($searchCriteria, $collection);
  197. $searchResults->setTotalCount($collection->getSize());
  198. $searchResults->setItems($collection->getItems());
  199. return $searchResults;
  200. }
  201. /**
  202. * Helper function that adds a FilterGroup to the collection.
  203. * @param FilterGroup $filterGroup
  204. * @param TaxClassCollection $collection
  205. * @return void
  206. * @deprecated 100.2.0
  207. */
  208. protected function addFilterGroupToCollection(FilterGroup $filterGroup, TaxClassCollection $collection)
  209. {
  210. $fields = [];
  211. $conditions = [];
  212. foreach ($filterGroup->getFilters() as $filter) {
  213. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  214. $fields[] = $filter->getField();
  215. $conditions[] = [$condition => $filter->getValue()];
  216. }
  217. if ($fields) {
  218. $collection->addFieldToFilter($fields, $conditions);
  219. }
  220. }
  221. /**
  222. * Retrieve collection processor
  223. *
  224. * @deprecated 100.2.0
  225. * @return CollectionProcessorInterface
  226. */
  227. private function getCollectionProcessor()
  228. {
  229. if (!$this->collectionProcessor) {
  230. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  231. \Magento\Framework\Api\SearchCriteria\CollectionProcessor::class
  232. );
  233. }
  234. return $this->collectionProcessor;
  235. }
  236. }