Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ordered.php
Go to the documentation of this file.
1 <?php
7 
10 
18 abstract class Ordered extends \Magento\Framework\App\Config\Base
19 {
25  protected $_collectorsCacheKey = null;
26 
32  protected $_configGroup;
33 
39  protected $_configSection;
40 
46  protected $_models = [];
47 
53  protected $_modelsConfig = [];
54 
60  protected $_collectors = [];
61 
65  protected $_configCacheType;
66 
70  protected $_logger;
71 
75  protected $_salesConfig;
76 
80  private $serializer;
81 
89  public function __construct(
90  \Magento\Framework\App\Cache\Type\Config $configCacheType,
91  \Psr\Log\LoggerInterface $logger,
92  \Magento\Sales\Model\Config $salesConfig,
93  $sourceData = null,
94  SerializerInterface $serializer = null
95  ) {
96  parent::__construct($sourceData);
97  $this->_configCacheType = $configCacheType;
98  $this->_logger = $logger;
99  $this->_salesConfig = $salesConfig;
100  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
101  }
102 
108  protected function _initModels()
109  {
110  $totals = $this->_salesConfig->getGroupTotals($this->_configSection, $this->_configGroup);
111  foreach ($totals as $totalCode => $totalConfig) {
112  $class = $totalConfig['instance'];
113  if (!empty($class)) {
114  $this->_models[$totalCode] = $this->_initModelInstance($class, $totalCode, $totalConfig);
115  }
116  }
117  return $this;
118  }
119 
129  abstract protected function _initModelInstance($class, $totalCode, $totalConfig);
130 
138  protected function _prepareConfigArray($code, $totalConfig)
139  {
140  $totalConfig = (array)$totalConfig;
141  $totalConfig['_code'] = $code;
142  return $totalConfig;
143  }
144 
152  private function _getSortedCollectorCodes(array $config)
153  {
154  reset($config);
155  $element = current($config);
156  if (isset($element['sort_order'])) {
157  uasort(
158  $config,
159  // @codingStandardsIgnoreStart
165  // @codingStandardsIgnoreEnd
166  function ($a, $b) {
167  if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
168  return 0;
169  }
170 
171  return $a['sort_order'] <=> $b['sort_order'];
172  }
173  );
174  }
175  $result = array_keys($config);
176  return $result;
177  }
178 
185  protected function _initCollectors()
186  {
187  $sortedCodes = [];
188  $cachedData = $this->_configCacheType->load($this->_collectorsCacheKey);
189  if ($cachedData) {
190  $sortedCodes = $this->serializer->unserialize($cachedData);
191  }
192  if (!$sortedCodes) {
193  $sortedCodes = $this->_getSortedCollectorCodes($this->_modelsConfig);
194  $this->_configCacheType->save($this->serializer->serialize($sortedCodes), $this->_collectorsCacheKey);
195  }
196  foreach ($sortedCodes as $code) {
197  $this->_collectors[$code] = $this->_models[$code];
198  }
199 
200  return $this;
201  }
202 }
$config
Definition: fraud_order.php:17
_prepareConfigArray($code, $totalConfig)
Definition: Ordered.php:138
$logger
_initModelInstance($class, $totalCode, $totalConfig)
$_option $_optionId $class
Definition: date.phtml:13
$totals
Definition: totalbar.phtml:10
__construct(\Magento\Framework\App\Cache\Type\Config $configCacheType, \Psr\Log\LoggerInterface $logger, \Magento\Sales\Model\Config $salesConfig, $sourceData=null, SerializerInterface $serializer=null)
Definition: Ordered.php:89
$code
Definition: info.phtml:12
$element
Definition: element.phtml:12