Ordered.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Config;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. /**
  10. * Configuration class for ordered items
  11. * @api
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. abstract class Ordered extends \Magento\Framework\App\Config\Base
  17. {
  18. /**
  19. * Cache key for collectors
  20. *
  21. * @var string|null
  22. */
  23. protected $_collectorsCacheKey = null;
  24. /**
  25. * Configuration group where to collect registered totals
  26. *
  27. * @var string
  28. */
  29. protected $_configGroup;
  30. /**
  31. * Configuration section where to collect registered totals
  32. *
  33. * @var string
  34. */
  35. protected $_configSection;
  36. /**
  37. * Prepared models
  38. *
  39. * @var array
  40. */
  41. protected $_models = [];
  42. /**
  43. * Models configuration
  44. *
  45. * @var array
  46. */
  47. protected $_modelsConfig = [];
  48. /**
  49. * Sorted models
  50. *
  51. * @var array
  52. */
  53. protected $_collectors = [];
  54. /**
  55. * @var \Magento\Framework\App\Cache\Type\Config
  56. */
  57. protected $_configCacheType;
  58. /**
  59. * @var \Psr\Log\LoggerInterface
  60. */
  61. protected $_logger;
  62. /**
  63. * @var \Magento\Sales\Model\Config
  64. */
  65. protected $_salesConfig;
  66. /**
  67. * @var SerializerInterface
  68. */
  69. private $serializer;
  70. /**
  71. * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
  72. * @param \Psr\Log\LoggerInterface $logger
  73. * @param \Magento\Sales\Model\Config $salesConfig
  74. * @param \Magento\Framework\Simplexml\Element $sourceData
  75. * @param SerializerInterface $serializer
  76. */
  77. public function __construct(
  78. \Magento\Framework\App\Cache\Type\Config $configCacheType,
  79. \Psr\Log\LoggerInterface $logger,
  80. \Magento\Sales\Model\Config $salesConfig,
  81. $sourceData = null,
  82. SerializerInterface $serializer = null
  83. ) {
  84. parent::__construct($sourceData);
  85. $this->_configCacheType = $configCacheType;
  86. $this->_logger = $logger;
  87. $this->_salesConfig = $salesConfig;
  88. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  89. }
  90. /**
  91. * Initialize total models configuration and objects
  92. *
  93. * @return $this
  94. */
  95. protected function _initModels()
  96. {
  97. $totals = $this->_salesConfig->getGroupTotals($this->_configSection, $this->_configGroup);
  98. foreach ($totals as $totalCode => $totalConfig) {
  99. $class = $totalConfig['instance'];
  100. if (!empty($class)) {
  101. $this->_models[$totalCode] = $this->_initModelInstance($class, $totalCode, $totalConfig);
  102. }
  103. }
  104. return $this;
  105. }
  106. /**
  107. * Init model class by configuration
  108. *
  109. * @param string $class
  110. * @param string $totalCode
  111. * @param array $totalConfig
  112. * @return mixed
  113. * @abstract
  114. */
  115. abstract protected function _initModelInstance($class, $totalCode, $totalConfig);
  116. /**
  117. * Prepare configuration array for total model
  118. *
  119. * @param string $code
  120. * @param \Magento\Framework\App\Config\Element $totalConfig
  121. * @return array
  122. */
  123. protected function _prepareConfigArray($code, $totalConfig)
  124. {
  125. $totalConfig = (array)$totalConfig;
  126. $totalConfig['_code'] = $code;
  127. return $totalConfig;
  128. }
  129. /**
  130. * Aggregate before/after information from all items and sort totals based on this data
  131. * Invoke simple sorting if the first element contains the "sort_order" key
  132. *
  133. * @param array $config
  134. * @return array
  135. */
  136. private function _getSortedCollectorCodes(array $config)
  137. {
  138. reset($config);
  139. $element = current($config);
  140. if (isset($element['sort_order'])) {
  141. uasort(
  142. $config,
  143. // @codingStandardsIgnoreStart
  144. /**
  145. * @param array $a
  146. * @param array $b
  147. * @return int
  148. */
  149. // @codingStandardsIgnoreEnd
  150. function ($a, $b) {
  151. if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
  152. return 0;
  153. }
  154. return $a['sort_order'] <=> $b['sort_order'];
  155. }
  156. );
  157. }
  158. $result = array_keys($config);
  159. return $result;
  160. }
  161. /**
  162. * Initialize collectors array.
  163. * Collectors array is array of total models ordered based on configuration settings
  164. *
  165. * @return $this
  166. */
  167. protected function _initCollectors()
  168. {
  169. $sortedCodes = [];
  170. $cachedData = $this->_configCacheType->load($this->_collectorsCacheKey);
  171. if ($cachedData) {
  172. $sortedCodes = $this->serializer->unserialize($cachedData);
  173. }
  174. if (!$sortedCodes) {
  175. $sortedCodes = $this->_getSortedCollectorCodes($this->_modelsConfig);
  176. $this->_configCacheType->save($this->serializer->serialize($sortedCodes), $this->_collectorsCacheKey);
  177. }
  178. foreach ($sortedCodes as $code) {
  179. $this->_collectors[$code] = $this->_models[$code];
  180. }
  181. return $this;
  182. }
  183. }