CalculatorFactory.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Calculation;
  7. use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
  8. class CalculatorFactory
  9. {
  10. /**
  11. * Identifier constant for unit based calculation
  12. */
  13. const CALC_UNIT_BASE = 'UNIT_BASE_CALCULATION';
  14. /**
  15. * Identifier constant for row based calculation
  16. */
  17. const CALC_ROW_BASE = 'ROW_BASE_CALCULATION';
  18. /**
  19. * Identifier constant for total based calculation
  20. */
  21. const CALC_TOTAL_BASE = 'TOTAL_BASE_CALCULATION';
  22. /**
  23. * @var \Magento\Framework\ObjectManagerInterface
  24. */
  25. protected $objectManager;
  26. /**
  27. * Constructor
  28. *
  29. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  30. */
  31. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
  32. {
  33. $this->objectManager = $objectManager;
  34. }
  35. /**
  36. * Create new calculator
  37. *
  38. * @param string $type Type of calculator
  39. * @param int $storeId
  40. * @param CustomerAddress $billingAddress
  41. * @param CustomerAddress $shippingAddress
  42. * @param null|int $customerTaxClassId
  43. * @param null|int $customerId
  44. * @return \Magento\Tax\Model\Calculation\AbstractCalculator
  45. * @throws \InvalidArgumentException
  46. */
  47. public function create(
  48. $type,
  49. $storeId,
  50. CustomerAddress $billingAddress = null,
  51. CustomerAddress $shippingAddress = null,
  52. $customerTaxClassId = null,
  53. $customerId = null
  54. ) {
  55. switch ($type) {
  56. case self::CALC_UNIT_BASE:
  57. $className = \Magento\Tax\Model\Calculation\UnitBaseCalculator::class;
  58. break;
  59. case self::CALC_ROW_BASE:
  60. $className = \Magento\Tax\Model\Calculation\RowBaseCalculator::class;
  61. break;
  62. case self::CALC_TOTAL_BASE:
  63. $className = \Magento\Tax\Model\Calculation\TotalBaseCalculator::class;
  64. break;
  65. default:
  66. throw new \InvalidArgumentException('Unknown calculation type: ' . $type);
  67. }
  68. /** @var \Magento\Tax\Model\Calculation\AbstractCalculator $calculator */
  69. $calculator = $this->objectManager->create($className, ['storeId' => $storeId]);
  70. if (null != $shippingAddress) {
  71. $calculator->setShippingAddress($shippingAddress);
  72. }
  73. if (null != $billingAddress) {
  74. $calculator->setBillingAddress($billingAddress);
  75. }
  76. if (null != $customerTaxClassId) {
  77. $calculator->setCustomerTaxClassId($customerTaxClassId);
  78. }
  79. if (null != $customerId) {
  80. $calculator->setCustomerId($customerId);
  81. }
  82. return $calculator;
  83. }
  84. }