| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * TechDivision\Import\Loaders\CoreConfigDataLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * NOTICE OF LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This source file is subject to the Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * that is available through the world-wide-web at this URL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * http://opensource.org/licenses/osl-3.0.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * PHP version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author    Tim Wagner <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @copyright 2020 TechDivision GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @link      https://github.com/techdivision/import | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @link      http://www.techdivision.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | namespace TechDivision\Import\Loaders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use TechDivision\Import\Utils\ScopeKeys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use TechDivision\Import\Utils\MemberNames; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use TechDivision\Import\Utils\Generators\GeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use TechDivision\Import\Services\ConfigurationProcessorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * Generic loader implementation for the Magento core configuration data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @author    Tim Wagner <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @copyright 2020 TechDivision GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @link      https://github.com/techdivision/import | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @link      http://www.techdivision.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | class CoreConfigDataLoader implements LoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * The available websites. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     private $storeWebsites = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * The core config data from the Magento instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     private $coreConfigData = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * The UID generator for the core config data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @var \TechDivision\Import\Utils\Generators\GeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private $coreConfigDataUidGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Initialize the loader with the configuration processor and the UID generator for the core config data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param \TechDivision\Import\Services\ConfigurationProcessorInterface $configurationProcessor     The registry processor instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param \TechDivision\Import\Utils\Generators\GeneratorInterface      $coreConfigDataUidGenerator The UID generator for the core config data | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         ConfigurationProcessorInterface $configurationProcessor, | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         GeneratorInterface $coreConfigDataUidGenerator | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         // initialize the UUID generator | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $this->coreConfigDataUidGenerator = $coreConfigDataUidGenerator; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         // load the core configuration data from the Magento instance | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $this->coreConfigData = $configurationProcessor->getCoreConfigData(); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         // load the store website data from the Magento instance | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $this->storeWebsites = $configurationProcessor->getStoreWebsites(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Return's the Magento configuration value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param string  $path    The Magento path of the requested configuration value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param mixed   $default The default value that has to be returned, if the requested configuration value is not set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string  $scope   The scope the configuration value has been set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param integer $scopeId The scope ID the configuration value has been set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return mixed The configuration value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 93 |  | View Code Duplication |     public function load($path = null, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         // initialize the core config data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $coreConfigData = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             MemberNames::PATH => $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             MemberNames::SCOPE => $scope, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             MemberNames::SCOPE_ID => $scopeId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         // generate the UID from the passed data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         // iterate over the core config data and try to find the requested configuration value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (isset($this->coreConfigData[$uniqueIdentifier])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         // query whether or not we've to query for the configuration value on fallback level 'websites' also | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if ($scope === ScopeKeys::SCOPE_STORES) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             // query whether or not the website with the passed ID is available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             foreach ($this->storeWebsites as $storeWebsite) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 if ($storeWebsite[MemberNames::WEBSITE_ID] === $scopeId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     // replace scope with 'websites' and website ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     $coreConfigData = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                         $coreConfigData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                         array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                             MemberNames::SCOPE    => ScopeKeys::SCOPE_WEBSITES, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                             MemberNames::SCOPE_ID => $storeWebsite[MemberNames::WEBSITE_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     // generate the UID from the passed data, merged with the 'websites' scope and ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     // query whether or not, the configuration value on 'websites' level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                         return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         // replace scope with 'default' and scope ID '0' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $coreConfigData = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $coreConfigData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 MemberNames::SCOPE    => ScopeKeys::SCOPE_DEFAULT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 MemberNames::SCOPE_ID => 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         // generate the UID from the passed data, merged with the 'default' scope and ID 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $uniqueIdentifier = $this->coreConfigDataUidGenerator->generate($coreConfigData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         // query whether or not, the configuration value on 'default' level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         if (isset($this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             return $this->coreConfigData[$uniqueIdentifier][MemberNames::VALUE]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         // if not, return the passed default value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         if ($default !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         // throw an exception if no value can be found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         // in the Magento configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         throw new \Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 'Can\'t find a value for configuration "%s-%s-%d" in "core_config_data"', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 $scope, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 $scopeId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.