_scopeConfig = $scopeConfig; $this->_totalFactory = $totalFactory; parent::__construct($configCacheType, $logger, $salesConfig, $sourceData, $serializer); $this->_store = $store ?: $storeManager->getStore(); $this->_initModels()->_initCollectors()->_initRetrievers(); } /** * Get total models array ordered for right calculation logic * * @return array */ public function getCollectors() { return $this->_collectors; } /** * Get total models array ordered for right display sequence * * @return \Magento\Quote\Model\Quote\Address\Total\AbstractTotal[] */ public function getRetrievers() { return $this->_retrievers; } /** * Init model class by configuration * * @param string $class * @param string $totalCode * @param array $totalConfig * @return \Magento\Quote\Model\Quote\Address\Total\AbstractTotal * @throws \Magento\Framework\Exception\LocalizedException */ protected function _initModelInstance($class, $totalCode, $totalConfig) { $model = $this->_totalFactory->create($class); if (!$model instanceof \Magento\Quote\Model\Quote\Address\Total\AbstractTotal) { throw new \Magento\Framework\Exception\LocalizedException( __( 'The address total model should be extended from \Magento\Quote\Model\Quote\Address\Total\AbstractTotal.' ) ); } $model->setCode($totalCode); $this->_modelsConfig[$totalCode] = $this->_prepareConfigArray($totalCode, $totalConfig); $this->_modelsConfig[$totalCode] = $model->processConfigArray($this->_modelsConfig[$totalCode], $this->_store); return $model; } /** * Initialize retrievers array * * @return $this * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ private function _initRetrievers() { $sorts = $this->_scopeConfig->getValue( self::XML_PATH_SALES_TOTALS_SORT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->_store ); foreach ($sorts as $code => $sortOrder) { if (isset($this->_models[$code])) { // Reserve enough space for collisions $retrieverId = 100 * (int)$sortOrder; // Check if there is a retriever with such id and find next available position if needed while (isset($this->_retrievers[$retrieverId])) { $retrieverId++; } $this->_retrievers[$retrieverId] = $this->_models[$code]; } } ksort($this->_retrievers); $notSorted = array_diff(array_keys($this->_models), array_keys($sorts)); foreach ($notSorted as $code) { $this->_retrievers[] = $this->_models[$code]; } return $this; } }