scopes = $scopes; $this->resourceConnection = $resourceConnection; $this->storeResource = $storeResource; $this->websiteResource = $websiteResource; $this->deploymentConfig = $deploymentConfig; } /** * @inheritdoc */ public function process(array $data) { if ($this->deploymentConfig->isDbAvailable()) {//read only from db $this->storeData = $this->storeResource->readAllStores(); $this->websiteData = $this->websiteResource->readAllWebsites(); } else { $this->storeData = $this->scopes->get('stores'); $this->websiteData = $this->scopes->get('websites'); } $defaultConfig = isset($data['default']) ? $data['default'] : []; $result = [ 'default' => $defaultConfig, 'websites' => [], 'stores' => [] ]; $websitesConfig = isset($data['websites']) ? $data['websites'] : []; $result['websites'] = $this->prepareWebsitesConfig($defaultConfig, $websitesConfig); $storesConfig = isset($data['stores']) ? $data['stores'] : []; $result['stores'] = $this->prepareStoresConfig($defaultConfig, $websitesConfig, $storesConfig); return $result; } /** * Prepare website data from Config/Type/Scopes * * @param array $defaultConfig * @param array $websitesConfig * @return array */ private function prepareWebsitesConfig( array $defaultConfig, array $websitesConfig ) { $result = []; foreach ((array)$this->websiteData as $website) { $code = $website['code']; $id = $website['website_id']; $websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : []; $result[$code] = array_replace_recursive($defaultConfig, $websiteConfig); $result[$id] = $result[$code]; } return $result; } /** * Prepare stores data from Config/Type/Scopes * * @param array $defaultConfig * @param array $websitesConfig * @param array $storesConfig * @return array */ private function prepareStoresConfig( array $defaultConfig, array $websitesConfig, array $storesConfig ) { $result = []; foreach ((array)$this->storeData as $store) { $code = $store['code']; $id = $store['store_id']; $websiteConfig = []; if (isset($store['website_id'])) { $websiteConfig = $this->getWebsiteConfig($websitesConfig, $store['website_id']); } $storeConfig = isset($storesConfig[$code]) ? $storesConfig[$code] : []; $result[$code] = array_replace_recursive($defaultConfig, $websiteConfig, $storeConfig); $result[$id] = $result[$code]; } return $result; } /** * Find information about website by its ID. * * @param array $websites Has next format: (website_code => [website_data]) * @param int $id * @return array */ private function getWebsiteConfig(array $websites, $id) { foreach ((array)$this->websiteData as $website) { if ($website['website_id'] == $id) { $code = $website['code']; return isset($websites[$code]) ? $websites[$code] : []; } } return []; } }