Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collector.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class Collector extends \Magento\Sales\Model\Config\Ordered
14 {
18  const XML_PATH_SALES_TOTALS_SORT = 'sales/totals_sort';
19 
25  protected $_retrievers = [];
26 
32  protected $_store;
33 
39  protected $_configGroup = 'totals';
40 
44  protected $_configSection = 'quote';
45 
51  protected $_collectorsCacheKey = 'sorted_quote_collectors';
52 
58  protected $_scopeConfig;
59 
63  protected $_totalFactory;
64 
76  public function __construct(
77  \Magento\Framework\App\Cache\Type\Config $configCacheType,
78  \Psr\Log\LoggerInterface $logger,
79  \Magento\Sales\Model\Config $salesConfig,
80  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
81  \Magento\Store\Model\StoreManagerInterface $storeManager,
82  \Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory,
83  $sourceData = null,
84  $store = null,
85  SerializerInterface $serializer = null
86  ) {
87  $this->_scopeConfig = $scopeConfig;
88  $this->_totalFactory = $totalFactory;
89  parent::__construct($configCacheType, $logger, $salesConfig, $sourceData, $serializer);
90  $this->_store = $store ?: $storeManager->getStore();
91  $this->_initModels()->_initCollectors()->_initRetrievers();
92  }
93 
99  public function getCollectors()
100  {
101  return $this->_collectors;
102  }
103 
109  public function getRetrievers()
110  {
111  return $this->_retrievers;
112  }
113 
123  protected function _initModelInstance($class, $totalCode, $totalConfig)
124  {
125  $model = $this->_totalFactory->create($class);
126  if (!$model instanceof \Magento\Quote\Model\Quote\Address\Total\AbstractTotal) {
127  throw new \Magento\Framework\Exception\LocalizedException(
128  __(
129  'The address total model should be extended from
130  \Magento\Quote\Model\Quote\Address\Total\AbstractTotal.'
131  )
132  );
133  }
134 
135  $model->setCode($totalCode);
136  $this->_modelsConfig[$totalCode] = $this->_prepareConfigArray($totalCode, $totalConfig);
137  $this->_modelsConfig[$totalCode] = $model->processConfigArray($this->_modelsConfig[$totalCode], $this->_store);
138 
139  return $model;
140  }
141 
148  private function _initRetrievers()
149  {
150  $sorts = $this->_scopeConfig->getValue(
151  self::XML_PATH_SALES_TOTALS_SORT,
153  $this->_store
154  );
155  foreach ($sorts as $code => $sortOrder) {
156  if (isset($this->_models[$code])) {
157  // Reserve enough space for collisions
158  $retrieverId = 100 * (int)$sortOrder;
159  // Check if there is a retriever with such id and find next available position if needed
160  while (isset($this->_retrievers[$retrieverId])) {
161  $retrieverId++;
162  }
163  $this->_retrievers[$retrieverId] = $this->_models[$code];
164  }
165  }
166  ksort($this->_retrievers);
167  $notSorted = array_diff(array_keys($this->_models), array_keys($sorts));
168  foreach ($notSorted as $code) {
169  $this->_retrievers[] = $this->_models[$code];
170  }
171  return $this;
172  }
173 }
__construct(\Magento\Framework\App\Cache\Type\Config $configCacheType, \Psr\Log\LoggerInterface $logger, \Magento\Sales\Model\Config $salesConfig, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Quote\Model\Quote\Address\TotalFactory $totalFactory, $sourceData=null, $store=null, SerializerInterface $serializer=null)
Definition: Collector.php:76
_prepareConfigArray($code, $totalConfig)
Definition: Ordered.php:138
$storeManager
__()
Definition: __.php:13
$logger
$_option $_optionId $class
Definition: date.phtml:13
_initModelInstance($class, $totalCode, $totalConfig)
Definition: Collector.php:123
$code
Definition: info.phtml:12