Base.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Total\Config;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. /**
  9. * Configuration class for totals
  10. */
  11. class Base extends \Magento\Sales\Model\Config\Ordered
  12. {
  13. /**
  14. * Cache key for collectors
  15. *
  16. * @var string
  17. */
  18. protected $_collectorsCacheKey = 'sorted_collectors';
  19. /**
  20. * Total models list
  21. *
  22. * @var array
  23. */
  24. protected $_totalModels = [];
  25. /**
  26. * Configuration path where to collect registered totals
  27. *
  28. * @var string
  29. */
  30. protected $_configGroup = 'totals';
  31. /**
  32. * @var \Magento\Sales\Model\Order\TotalFactory
  33. */
  34. protected $_orderTotalFactory;
  35. /**
  36. * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
  37. * @param \Psr\Log\LoggerInterface $logger
  38. * @param \Magento\Sales\Model\Config $salesConfig
  39. * @param \Magento\Sales\Model\Order\TotalFactory $orderTotalFactory
  40. * @param \Magento\Framework\Simplexml\Element|mixed $sourceData
  41. * @param SerializerInterface $serializer
  42. */
  43. public function __construct(
  44. \Magento\Framework\App\Cache\Type\Config $configCacheType,
  45. \Psr\Log\LoggerInterface $logger,
  46. \Magento\Sales\Model\Config $salesConfig,
  47. \Magento\Sales\Model\Order\TotalFactory $orderTotalFactory,
  48. $sourceData = null,
  49. SerializerInterface $serializer = null
  50. ) {
  51. parent::__construct($configCacheType, $logger, $salesConfig, $sourceData, $serializer);
  52. $this->_orderTotalFactory = $orderTotalFactory;
  53. }
  54. /**
  55. * Init model class by configuration
  56. *
  57. * @param string $class
  58. * @param string $totalCode
  59. * @param array $totalConfig
  60. * @return \Magento\Sales\Model\Order\Total\AbstractTotal
  61. * @throws \Magento\Framework\Exception\LocalizedException
  62. */
  63. protected function _initModelInstance($class, $totalCode, $totalConfig)
  64. {
  65. $model = $this->_orderTotalFactory->create($class);
  66. if (!$model instanceof \Magento\Sales\Model\Order\Total\AbstractTotal) {
  67. throw new \Magento\Framework\Exception\LocalizedException(
  68. __('The total model should be extended from \Magento\Sales\Model\Order\Total\AbstractTotal.')
  69. );
  70. }
  71. $model->setCode($totalCode);
  72. $model->setTotalConfigNode($totalConfig);
  73. $this->_modelsConfig[$totalCode] = $this->_prepareConfigArray($totalCode, $totalConfig);
  74. $this->_modelsConfig[$totalCode] = $model->processConfigArray($this->_modelsConfig[$totalCode]);
  75. return $model;
  76. }
  77. /**
  78. * Retrieve total calculation models
  79. *
  80. * @return array
  81. */
  82. public function getTotalModels()
  83. {
  84. if (empty($this->_totalModels)) {
  85. $this->_initModels();
  86. $this->_initCollectors();
  87. $this->_totalModels = $this->_collectors;
  88. }
  89. return $this->_totalModels;
  90. }
  91. }