Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Repository.php
Go to the documentation of this file.
1 <?php
9 
21 use Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory as TaxClassCollectionFactory;
22 
27 {
32 
37 
42 
43  const CLASS_ID_NOT_ALLOWED = 'class_id is not expected for this request.';
44 
51 
57  protected $filterBuilder;
58 
62  protected $taxClassResource;
63 
67  protected $joinProcessor;
68 
72  private $collectionProcessor;
73 
84  public function __construct(
87  TaxClassCollectionFactory $taxClassCollectionFactory,
88  \Magento\Tax\Api\Data\TaxClassSearchResultsInterfaceFactory $searchResultsFactory,
91  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
92  CollectionProcessorInterface $collectionProcessor = null
93  ) {
94  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
95  $this->filterBuilder = $filterBuilder;
96  $this->taxClassCollectionFactory = $taxClassCollectionFactory;
97  $this->searchResultsFactory = $searchResultsFactory;
98  $this->classModelRegistry = $classModelRegistry;
99  $this->taxClassResource = $taxClassResource;
100  $this->joinProcessor = $joinProcessor;
101  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
102  }
103 
107  public function save(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
108  {
109  if ($taxClass->getClassId()) {
110  $originalTaxClassModel = $this->get($taxClass->getClassId());
111 
112  /* should not be allowed to switch the tax class type */
113  if ($originalTaxClassModel->getClassType() !== $taxClass->getClassType()) {
114  throw new InputException(__('Updating classType is not allowed.'));
115  }
116  }
117  $this->validateTaxClassData($taxClass);
118  try {
119  $this->taxClassResource->save($taxClass);
120  } catch (ModelException $e) {
121  if (strpos($e->getMessage(), (string)__('Class name and class type')) !== false) {
122  throw new InputException(
123  __(
124  'A class with the same name already exists for ClassType %1.',
125  $taxClass->getClassType()
126  )
127  );
128  } else {
129  throw $e;
130  }
131  }
132  $this->classModelRegistry->registerTaxClass($taxClass);
133  return $taxClass->getClassId();
134  }
135 
139  public function get($taxClassId)
140  {
141  return $this->classModelRegistry->retrieve($taxClassId);
142  }
143 
147  public function delete(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
148  {
149  $taxClassId = $taxClass->getClassId();
150  try {
151  $this->taxClassResource->delete($taxClass);
152  } catch (CouldNotDeleteException $e) {
153  throw $e;
154  } catch (\Exception $e) {
155  return false;
156  }
157  $this->classModelRegistry->remove($taxClassId);
158  return true;
159  }
160 
164  public function deleteById($taxClassId)
165  {
166  $taxClassModel = $this->get($taxClassId);
167  return $this->delete($taxClassModel);
168  }
169 
177  protected function validateTaxClassData(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
178  {
179  $exception = new InputException();
180 
181  if (!\Zend_Validate::is(trim($taxClass->getClassName()), 'NotEmpty')) {
182  $exception->addError(
183  __('"%fieldName" is required. Enter and try again.', ['fieldName' => ClassModel::KEY_NAME])
184  );
185  }
186 
187  $classType = $taxClass->getClassType();
188  if (!\Zend_Validate::is(trim($classType), 'NotEmpty')) {
189  $exception->addError(
190  __('"%fieldName" is required. Enter and try again.', ['fieldName' => ClassModel::KEY_TYPE])
191  );
194  ) {
195  $exception->addError(
196  __(
197  'Invalid value of "%value" provided for the %fieldName field.',
198  ['fieldName' => ClassModel::KEY_TYPE, 'value' => $classType]
199  )
200  );
201  }
202 
203  if ($exception->wasErrorAdded()) {
204  throw $exception;
205  }
206  }
207 
211  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
212  {
213  $searchResults = $this->searchResultsFactory->create();
214  $searchResults->setSearchCriteria($searchCriteria);
216  $collection = $this->taxClassCollectionFactory->create();
217  $this->joinProcessor->process($collection);
218  $this->collectionProcessor->process($searchCriteria, $collection);
219  $searchResults->setTotalCount($collection->getSize());
220  $searchResults->setItems($collection->getItems());
221  return $searchResults;
222  }
223 
231  protected function addFilterGroupToCollection(FilterGroup $filterGroup, TaxClassCollection $collection)
232  {
233  $fields = [];
234  $conditions = [];
235  foreach ($filterGroup->getFilters() as $filter) {
236  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
237  $fields[] = $filter->getField();
238  $conditions[] = [$condition => $filter->getValue()];
239  }
240  if ($fields) {
241  $collection->addFieldToFilter($fields, $conditions);
242  }
243  }
244 
251  private function getCollectionProcessor()
252  {
253  if (!$this->collectionProcessor) {
254  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
255  \Magento\Framework\Api\SearchCriteria\CollectionProcessor::class
256  );
257  }
258  return $this->collectionProcessor;
259  }
260 }
save(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
Definition: Repository.php:107
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
addFilterGroupToCollection(FilterGroup $filterGroup, TaxClassCollection $collection)
Definition: Repository.php:231
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
$searchCriteria
__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)
Definition: Repository.php:84
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
validateTaxClassData(\Magento\Tax\Api\Data\TaxClassInterface $taxClass)
Definition: Repository.php:177