Factory.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Tax Class factory
  8. */
  9. namespace Magento\Tax\Model\TaxClass;
  10. class Factory
  11. {
  12. /**
  13. * @var \Magento\Framework\ObjectManagerInterface
  14. */
  15. protected $_objectManager;
  16. /**
  17. * Type to class map
  18. *
  19. * @var array
  20. */
  21. protected $_types = [
  22. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER => \Magento\Tax\Model\TaxClass\Type\Customer::class,
  23. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT => \Magento\Tax\Model\TaxClass\Type\Product::class,
  24. ];
  25. /**
  26. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  27. */
  28. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
  29. {
  30. $this->_objectManager = $objectManager;
  31. }
  32. /**
  33. * Create new config object
  34. *
  35. * @param \Magento\Tax\Model\ClassModel $taxClass
  36. * @return \Magento\Tax\Model\TaxClass\Type\TypeInterface
  37. * @throws \Magento\Framework\Exception\LocalizedException
  38. */
  39. public function create(\Magento\Tax\Model\ClassModel $taxClass)
  40. {
  41. $taxClassType = $taxClass->getClassType();
  42. if (!array_key_exists($taxClassType, $this->_types)) {
  43. throw new \Magento\Framework\Exception\LocalizedException(
  44. __('Invalid type of tax class "%1"', $taxClassType)
  45. );
  46. }
  47. return $this->_objectManager->create(
  48. $this->_types[$taxClassType],
  49. ['data' => ['id' => $taxClass->getId()]]
  50. );
  51. }
  52. }