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; } }