123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Tax\Model\TaxClass;
- use Magento\Framework\Api\FilterBuilder;
- use Magento\Framework\Api\Search\FilterGroup;
- use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\Framework\Exception\CouldNotDeleteException;
- use Magento\Framework\Exception\InputException;
- use Magento\Framework\Exception\LocalizedException as ModelException;
- use Magento\Tax\Api\TaxClassManagementInterface;
- use Magento\Tax\Model\ClassModel;
- use Magento\Tax\Model\ClassModelRegistry;
- use Magento\Tax\Model\ResourceModel\TaxClass\Collection as TaxClassCollection;
- use Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory as TaxClassCollectionFactory;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Repository implements \Magento\Tax\Api\TaxClassRepositoryInterface
- {
- /**
- * @var TaxClassCollectionFactory
- */
- protected $taxClassCollectionFactory;
- /**
- * @var \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory
- */
- protected $searchResultsFactory;
- /**
- * @var ClassModelRegistry
- */
- protected $classModelRegistry;
- const CLASS_ID_NOT_ALLOWED = 'class_id is not expected for this request.';
- /**
- * Search Criteria Builder
- *
- * @var SearchCriteriaBuilder
- */
- protected $searchCriteriaBuilder;
- /**
- * Filter Builder
- *
- * @var FilterBuilder
- */
- protected $filterBuilder;
- /**
- * @var \Magento\Tax\Model\ResourceModel\TaxClass
- */
- protected $taxClassResource;
- /**
- * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface
- */
- protected $joinProcessor;
- /**
- * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
- */
- private $collectionProcessor;
- /**
- * @param SearchCriteriaBuilder $searchCriteriaBuilder
- * @param FilterBuilder $filterBuilder
- * @param TaxClassCollectionFactory $taxClassCollectionFactory
- * @param \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory $searchResultsFactory
- * @param ClassModelRegistry $classModelRegistry
- * @param \Magento\Tax\Model\ResourceModel\TaxClass $taxClassResource
- * @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
- * @param CollectionProcessorInterface $collectionProcessor
- */
- public function __construct(
- SearchCriteriaBuilder $searchCriteriaBuilder,
- FilterBuilder $filterBuilder,
- TaxClassCollectionFactory $taxClassCollectionFactory,
- \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory $searchResultsFactory,
- ClassModelRegistry $classModelRegistry,
- \Magento\Tax\Model\ResourceModel\TaxClass $taxClassResource,
- \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
- CollectionProcessorInterface $collectionProcessor = null
- ) {
- $this->searchCriteriaBuilder = $searchCriteriaBuilder;
- $this->filterBuilder = $filterBuilder;
- $this->taxClassCollectionFactory = $taxClassCollectionFactory;
- $this->searchResultsFactory = $searchResultsFactory;
- $this->classModelRegistry = $classModelRegistry;
- $this->taxClassResource = $taxClassResource;
- $this->joinProcessor = $joinProcessor;
- $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
- }
- /**
- * {@inheritdoc}
- */
- public function save(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
- {
- if ($taxClass->getClassId()) {
- $originalTaxClassModel = $this->get($taxClass->getClassId());
- /* should not be allowed to switch the tax class type */
- if ($originalTaxClassModel->getClassType() !== $taxClass->getClassType()) {
- throw new InputException(__('Updating classType is not allowed.'));
- }
- }
- $this->validateTaxClassData($taxClass);
- try {
- $this->taxClassResource->save($taxClass);
- } catch (ModelException $e) {
- if (strpos($e->getMessage(), (string)__('Class name and class type')) !== false) {
- throw new InputException(
- __(
- 'A class with the same name already exists for ClassType %1.',
- $taxClass->getClassType()
- )
- );
- } else {
- throw $e;
- }
- }
- $this->classModelRegistry->registerTaxClass($taxClass);
- return $taxClass->getClassId();
- }
- /**
- * {@inheritdoc}
- */
- public function get($taxClassId)
- {
- return $this->classModelRegistry->retrieve($taxClassId);
- }
- /**
- * {@inheritdoc}
- */
- public function delete(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
- {
- $taxClassId = $taxClass->getClassId();
- try {
- $this->taxClassResource->delete($taxClass);
- } catch (CouldNotDeleteException $e) {
- throw $e;
- } catch (\Exception $e) {
- return false;
- }
- $this->classModelRegistry->remove($taxClassId);
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function deleteById($taxClassId)
- {
- $taxClassModel = $this->get($taxClassId);
- return $this->delete($taxClassModel);
- }
- /**
- * Validate TaxClass Data
- *
- * @param \Magento\Tax\Api\Data\TaxClassInterface $taxClass
- * @return void
- * @throws InputException
- */
- protected function validateTaxClassData(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
- {
- $exception = new InputException();
- if (!\Zend_Validate::is(trim($taxClass->getClassName()), 'NotEmpty')) {
- $exception->addError(
- __('"%fieldName" is required. Enter and try again.', ['fieldName' => ClassModel::KEY_NAME])
- );
- }
- $classType = $taxClass->getClassType();
- if (!\Zend_Validate::is(trim($classType), 'NotEmpty')) {
- $exception->addError(
- __('"%fieldName" is required. Enter and try again.', ['fieldName' => ClassModel::KEY_TYPE])
- );
- } elseif ($classType !== TaxClassManagementInterface::TYPE_CUSTOMER
- && $classType !== TaxClassManagementInterface::TYPE_PRODUCT
- ) {
- $exception->addError(
- __(
- 'Invalid value of "%value" provided for the %fieldName field.',
- ['fieldName' => ClassModel::KEY_TYPE, 'value' => $classType]
- )
- );
- }
- if ($exception->wasErrorAdded()) {
- throw $exception;
- }
- }
- /**
- * {@inheritdoc}
- */
- public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
- {
- $searchResults = $this->searchResultsFactory->create();
- $searchResults->setSearchCriteria($searchCriteria);
- /** @var TaxClassCollection $collection */
- $collection = $this->taxClassCollectionFactory->create();
- $this->joinProcessor->process($collection);
- $this->collectionProcessor->process($searchCriteria, $collection);
- $searchResults->setTotalCount($collection->getSize());
- $searchResults->setItems($collection->getItems());
- return $searchResults;
- }
- /**
- * Helper function that adds a FilterGroup to the collection.
- * @param FilterGroup $filterGroup
- * @param TaxClassCollection $collection
- * @return void
- * @deprecated 100.2.0
- */
- protected function addFilterGroupToCollection(FilterGroup $filterGroup, TaxClassCollection $collection)
- {
- $fields = [];
- $conditions = [];
- foreach ($filterGroup->getFilters() as $filter) {
- $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
- $fields[] = $filter->getField();
- $conditions[] = [$condition => $filter->getValue()];
- }
- if ($fields) {
- $collection->addFieldToFilter($fields, $conditions);
- }
- }
- /**
- * Retrieve collection processor
- *
- * @deprecated 100.2.0
- * @return CollectionProcessorInterface
- */
- private function getCollectionProcessor()
- {
- if (!$this->collectionProcessor) {
- $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
- \Magento\Framework\Api\SearchCriteria\CollectionProcessor::class
- );
- }
- return $this->collectionProcessor;
- }
- }
|