| Total Complexity | 47 |
| Total Lines | 434 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SimpleConfigurationLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SimpleConfigurationLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class SimpleConfigurationLoader implements ConfigurationLoaderInterface |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The key for the Magento Edition in the metadata extracted from the Composer configuration. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | const EDITION = 'edition'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The key for the Magento Version in the metadata extracted from the Composer configuration. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | const VERSION = 'version'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The container instance. |
||
| 61 | * |
||
| 62 | * @var \Symfony\Component\DependencyInjection\ContainerInterface |
||
| 63 | */ |
||
| 64 | protected $container; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The actual input instance. |
||
| 68 | * |
||
| 69 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 70 | */ |
||
| 71 | protected $input; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The library loader instance. |
||
| 75 | * |
||
| 76 | * @param \TechDivision\Import\Cli\LibraryLoader |
||
| 77 | */ |
||
| 78 | protected $libraryLoader; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The configuration factory instance. |
||
| 82 | * |
||
| 83 | * @var \TechDivision\Import\ConfigurationFactoryInterface |
||
| 84 | */ |
||
| 85 | protected $configurationFactory; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The available command names. |
||
| 89 | * |
||
| 90 | * @var \TechDivision\Import\Utils\CommandNames |
||
| 91 | */ |
||
| 92 | protected $commandNames; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The mapping of the command names to the entity type codes |
||
| 96 | * |
||
| 97 | * @var \TechDivision\Import\Utils\Mappings\CommandNameToEntityTypeCode |
||
| 98 | */ |
||
| 99 | protected $commandNameToEntityTypeCode; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Initializes the configuration loader. |
||
| 103 | * |
||
| 104 | * @param \Symfony\Component\Console\Input\InputInterface $input The input instance |
||
| 105 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The container instance |
||
| 106 | * @param \TechDivision\Import\Cli\Configuration\LibraryLoader $libraryLoader The configuration loader instance |
||
| 107 | * @param \TechDivision\Import\ConfigurationFactoryInterface $configurationFactory The configuration factory instance |
||
| 108 | * @param \TechDivision\Import\Utils\CommandNames $commandNames The available command names |
||
| 109 | * @param \TechDivision\Import\Utils\Mappings\CommandNameToEntityTypeCode $commandNameToEntityTypeCodes The mapping of the command names to the entity type codes |
||
| 110 | */ |
||
| 111 | public function __construct( |
||
| 112 | InputInterface $input, |
||
| 113 | ContainerInterface $container, |
||
| 114 | LibraryLoader $libraryLoader, |
||
| 115 | ConfigurationFactoryInterface $configurationFactory, |
||
| 116 | CommandNames $commandNames, |
||
| 117 | CommandNameToEntityTypeCode $commandNameToEntityTypeCodes |
||
| 118 | ) { |
||
| 119 | |||
| 120 | // set the passed instances |
||
| 121 | $this->input = $input; |
||
| 122 | $this->container = $container; |
||
| 123 | $this->libraryLoader = $libraryLoader; |
||
| 124 | $this->configurationFactory = $configurationFactory; |
||
| 125 | $this->commandNames = $commandNames; |
||
| 126 | $this->commandNameToEntityTypeCode = $commandNameToEntityTypeCodes; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Factory implementation to create a new initialized configuration instance. |
||
| 131 | * |
||
| 132 | * If command line options are specified, they will always override the |
||
| 133 | * values found in the configuration file. |
||
| 134 | * |
||
| 135 | * @return \TechDivision\Import\ConfigurationInterface The configuration instance |
||
| 136 | * @throws \Exception Is thrown, if the specified configuration file doesn't exist or the mandatory arguments/options to run the requested operation are not available |
||
| 137 | */ |
||
| 138 | public function load() |
||
| 139 | { |
||
| 140 | |||
| 141 | // load the actual vendor directory and entity type code |
||
| 142 | $vendorDir = $this->getVendorDir(); |
||
| 143 | |||
| 144 | // the path of the JMS serializer directory, relative to the vendor directory |
||
| 145 | $jmsDir = DIRECTORY_SEPARATOR . 'jms' . DIRECTORY_SEPARATOR . 'serializer' . DIRECTORY_SEPARATOR . 'src'; |
||
| 146 | |||
| 147 | // try to find the path to the JMS Serializer annotations |
||
| 148 | if (!file_exists($annotationDir = $vendorDir . DIRECTORY_SEPARATOR . $jmsDir)) { |
||
| 149 | // stop processing, if the JMS annotations can't be found |
||
| 150 | throw new \Exception( |
||
| 151 | sprintf( |
||
| 152 | 'The jms/serializer libarary can not be found in one of "%s"', |
||
| 153 | implode(', ', $vendorDir) |
||
|
|
|||
| 154 | ) |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | // register the autoloader for the JMS serializer annotations |
||
| 159 | \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace( |
||
| 160 | 'JMS\Serializer\Annotation', |
||
| 161 | $annotationDir |
||
| 162 | ); |
||
| 163 | |||
| 164 | // query whether or not, a configuration file has been specified |
||
| 165 | if ($configuration = $this->input->getOption(InputOptionKeys::CONFIGURATION)) { |
||
| 166 | // load the configuration from the file with the given filename |
||
| 167 | $instance = $this->createConfiguration($configuration); |
||
| 168 | } elseif ($magentoEdition = $this->input->getOption(InputOptionKeys::MAGENTO_EDITION) && $magentoVersion = $this->input->getOption(InputOptionKeys::MAGENTO_VERSION)) { |
||
| 169 | // use the Magento Edition that has been specified as option |
||
| 170 | $instance = $this->createConfiguration($this->getDefaultConfiguration($magentoEdition, $magentoVersion, $this->getEntityTypeCode())); |
||
| 171 | |||
| 172 | // override the Magento Edition/Version |
||
| 173 | $instance->setMagentoEdition($magentoEdition); |
||
| 174 | $instance->setMagentoVersion($magentoVersion); |
||
| 175 | } else { |
||
| 176 | // finally, query whether or not the installation directory is a valid Magento root directory |
||
| 177 | if (!$this->isMagentoRootDir($installationDir = $this->input->getOption(InputOptionKeys::INSTALLATION_DIR))) { |
||
| 178 | throw new \Exception( |
||
| 179 | sprintf( |
||
| 180 | 'Directory "%s" specified with option "--installation-dir" is not a valid Magento root directory', |
||
| 181 | $installationDir |
||
| 182 | ) |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | // load the Magento Edition from the Composer configuration file |
||
| 187 | $metadata = $this->getEditionMapping($installationDir); |
||
| 188 | |||
| 189 | // extract edition & version from the metadata |
||
| 190 | $magentoEdition = $metadata[SimpleConfigurationLoader::EDITION]; |
||
| 191 | $magentoVersion = $metadata[SimpleConfigurationLoader::VERSION]; |
||
| 192 | |||
| 193 | // use the Magento Edition that has been detected by the installation directory |
||
| 194 | $instance = $this->createConfiguration($this->getDefaultConfiguration($magentoEdition, $magentoVersion, $this->getEntityTypeCode())); |
||
| 195 | |||
| 196 | // override the Magento Edition/Version |
||
| 197 | $instance->setMagentoEdition($magentoEdition); |
||
| 198 | $instance->setMagentoVersion($magentoVersion); |
||
| 199 | } |
||
| 200 | |||
| 201 | // query whether or not a system name has been specified as command line option, if yes override the value from the configuration file |
||
| 202 | if (($this->input->hasOptionSpecified(InputOptionKeys::SYSTEM_NAME) && $this->input->getOption(InputOptionKeys::SYSTEM_NAME)) || $instance->getSystemName() === null) { |
||
| 203 | $instance->setSystemName($this->input->getOption(InputOptionKeys::SYSTEM_NAME)); |
||
| 204 | } |
||
| 205 | |||
| 206 | // query whether or not a PID filename has been specified as command line option, if yes override the value from the configuration file |
||
| 207 | if (($this->input->hasOptionSpecified(InputOptionKeys::PID_FILENAME) && $this->input->getOption(InputOptionKeys::PID_FILENAME)) || $instance->getPidFilename() === null) { |
||
| 208 | $instance->setPidFilename($this->input->getOption(InputOptionKeys::PID_FILENAME)); |
||
| 209 | } |
||
| 210 | |||
| 211 | // query whether or not a Magento installation directory has been specified as command line option, if yes override the value from the configuration file |
||
| 212 | if (($this->input->hasOptionSpecified(InputOptionKeys::INSTALLATION_DIR) && $this->input->getOption(InputOptionKeys::INSTALLATION_DIR)) || $instance->getInstallationDir() === null) { |
||
| 213 | $instance->setInstallationDir($this->input->getOption(InputOptionKeys::INSTALLATION_DIR)); |
||
| 214 | } |
||
| 215 | |||
| 216 | // query whether or not a Magento Edition has been specified as command line option, if yes override the value from the configuration file |
||
| 217 | if (($this->input->hasOptionSpecified(InputOptionKeys::MAGENTO_EDITION) && $this->input->getOption(InputOptionKeys::MAGENTO_EDITION)) || $instance->getMagentoEdition() === null) { |
||
| 218 | $instance->setMagentoEdition($this->input->getOption(InputOptionKeys::MAGENTO_EDITION)); |
||
| 219 | } |
||
| 220 | |||
| 221 | // query whether or not a Magento Version has been specified as command line option, if yes override the value from the configuration file |
||
| 222 | if (($this->input->hasOptionSpecified(InputOptionKeys::MAGENTO_VERSION) && $this->input->getOption(InputOptionKeys::MAGENTO_VERSION)) || $instance->getMagentoVersion() === null) { |
||
| 223 | $instance->setMagentoVersion($this->input->getOption(InputOptionKeys::MAGENTO_VERSION)); |
||
| 224 | } |
||
| 225 | |||
| 226 | // query whether or not a directory for the source files has been specified as command line option, if yes override the value from the configuration file |
||
| 227 | if (($this->input->hasOptionSpecified(InputOptionKeys::SOURCE_DIR) && $this->input->getOption(InputOptionKeys::SOURCE_DIR)) || $instance->getSourceDir() === null) { |
||
| 228 | $instance->setSourceDir($this->input->getOption(InputOptionKeys::SOURCE_DIR)); |
||
| 229 | } |
||
| 230 | |||
| 231 | // return the initialized configuration instance |
||
| 232 | return $instance; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Create and return a new configuration instance from the passed configuration filename |
||
| 237 | * after merging additional specified params from the commandline. |
||
| 238 | * |
||
| 239 | * @param string $filename The configuration filename to use |
||
| 240 | * |
||
| 241 | * @return \TechDivision\Import\ConfigurationInterface The configuration instance |
||
| 242 | */ |
||
| 243 | protected function createConfiguration($filename) |
||
| 244 | { |
||
| 245 | |||
| 246 | // initialize the params specified with the --params parameter |
||
| 247 | $params = null; |
||
| 248 | |||
| 249 | // try to load the params from the commandline |
||
| 250 | if ($this->input->hasOptionSpecified(InputOptionKeys::PARAMS) && $this->input->getOption(InputOptionKeys::PARAMS)) { |
||
| 251 | $params = $this->input->getOption(InputOptionKeys::PARAMS); |
||
| 252 | } |
||
| 253 | |||
| 254 | // initialize the params file specified with the --params-file parameter |
||
| 255 | $paramsFile = null; |
||
| 256 | |||
| 257 | // try to load the path of the params file from the commandline |
||
| 258 | if ($this->input->hasOptionSpecified(InputOptionKeys::PARAMS_FILE) && $this->input->getOption(InputOptionKeys::PARAMS_FILE)) { |
||
| 259 | $paramsFile = $this->input->getOption(InputOptionKeys::PARAMS_FILE); |
||
| 260 | } |
||
| 261 | |||
| 262 | // create the configuration and return it |
||
| 263 | return $this->configurationFactory->factory($filename, pathinfo($filename, PATHINFO_EXTENSION), $params, $paramsFile); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Return's the DI container instance. |
||
| 268 | * |
||
| 269 | * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance |
||
| 270 | */ |
||
| 271 | protected function getContainer() |
||
| 272 | { |
||
| 273 | return $this->container; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Return's the absolute path to the actual vendor directory. |
||
| 278 | * |
||
| 279 | * @return string The absolute path to the actual vendor directory |
||
| 280 | * @throws \Exception Is thrown, if none of the possible vendor directories can be found |
||
| 281 | */ |
||
| 282 | protected function getVendorDir() |
||
| 283 | { |
||
| 284 | return $this->getContainer()->getParameter(DependencyInjectionKeys::CONFIGURATION_VENDOR_DIR); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Return's the actual command name. |
||
| 289 | * |
||
| 290 | * @return string The actual command name |
||
| 291 | */ |
||
| 292 | protected function getCommandName() |
||
| 293 | { |
||
| 294 | return $this->input->getArgument('command'); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return's the command's entity type code. |
||
| 299 | * |
||
| 300 | * @return string The command's entity type code |
||
| 301 | * @throws \Exception Is thrown, if the command name can not be mapped |
||
| 302 | */ |
||
| 303 | protected function getEntityTypeCode() |
||
| 304 | { |
||
| 305 | |||
| 306 | // try to map the command name to a entity type code |
||
| 307 | if (array_key_exists($commandName = $this->getCommandName(), (array) $this->commandNameToEntityTypeCode)) { |
||
| 308 | return $this->commandNameToEntityTypeCode[$commandName]; |
||
| 309 | } |
||
| 310 | |||
| 311 | // throw an exception if not possible |
||
| 312 | throw new \Exception(sprintf('Can\' map command name %s to a entity type', $commandName)); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns the mapped Magento Edition from the passed Magento installation. |
||
| 317 | * |
||
| 318 | * @param string $installationDir The Magento installation directory |
||
| 319 | * |
||
| 320 | * @return array The array with the mapped Magento Edition (either CE or EE) + the Version |
||
| 321 | * @throws \Exception Is thrown, if the passed installation directory doesn't contain a valid Magento installation |
||
| 322 | */ |
||
| 323 | protected function getEditionMapping($installationDir) |
||
| 324 | { |
||
| 325 | |||
| 326 | // load the default edition mappings from the configuration |
||
| 327 | $editionMappings = $this->getContainer()->getParameter(DependencyInjectionKeys::APPLICATION_EDITION_MAPPINGS); |
||
| 328 | |||
| 329 | // load the composer file from the Magento root directory |
||
| 330 | $composer = json_decode(file_get_contents($composerFile = sprintf('%s/composer.json', $installationDir)), true); |
||
| 331 | |||
| 332 | // try to load and explode the Magento Edition identifier from the Composer name |
||
| 333 | $explodedEdition = explode('/', $composer[MagentoConfigurationKeys::COMPOSER_EDITION_NAME_ATTRIBUTE]); |
||
| 334 | |||
| 335 | // try to load and explode the Magento Edition from the Composer configuration |
||
| 336 | if (!isset($editionMappings[$possibleEdition = end($explodedEdition)])) { |
||
| 337 | throw new \Exception( |
||
| 338 | sprintf( |
||
| 339 | '"%s" detected in "%s" is not a valid Magento Edition, please set Magento Edition with the "--magento-edition" option', |
||
| 340 | $possibleEdition, |
||
| 341 | $composerFile |
||
| 342 | ) |
||
| 343 | ); |
||
| 344 | } |
||
| 345 | |||
| 346 | // try to load and explode the Magento Version from the Composer configuration |
||
| 347 | if (!isset($composer[MagentoConfigurationKeys::COMPOSER_EDITION_VERSION_ATTRIBUTE])) { |
||
| 348 | throw new \Exception( |
||
| 349 | sprintf( |
||
| 350 | 'Can\'t detect a version in "%s", please set Magento Version with the "--magento-version" option', |
||
| 351 | $composerFile |
||
| 352 | ) |
||
| 353 | ); |
||
| 354 | } |
||
| 355 | |||
| 356 | // return the array with the Magento Version/Edition data |
||
| 357 | return array( |
||
| 358 | SimpleConfigurationLoader::VERSION => $composer[MagentoConfigurationKeys::COMPOSER_EDITION_VERSION_ATTRIBUTE], |
||
| 359 | SimpleConfigurationLoader::EDITION => $editionMappings[$possibleEdition] |
||
| 360 | ); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Return's the default configuration for the passed Magento Edition/Version and the actual entity type. |
||
| 365 | * |
||
| 366 | * @param string $magentoEdition The Magento Edition to return the configuration for |
||
| 367 | * @param string $magentoVersion The Magento Version to return the configuration for |
||
| 368 | * @param string $entityTypeCode The entity type code to use |
||
| 369 | * |
||
| 370 | * @return string The path to the default configuration |
||
| 371 | */ |
||
| 372 | protected function getDefaultConfiguration($magentoEdition, $magentoVersion, $entityTypeCode) |
||
| 373 | { |
||
| 374 | |||
| 375 | // load the components the filename has to be concatenated with |
||
| 376 | $vendorDir = $this->getVendorDir(); |
||
| 377 | $libraryDir = $this->getDefaultConfigurationLibrary($magentoEdition, $entityTypeCode); |
||
| 378 | $filename = $this->getDefaultConfigurationFile($entityTypeCode); |
||
| 379 | |||
| 380 | // load the directories that equals the versions custom configuration files are available for |
||
| 381 | $versions = glob(sprintf('%s/%s/etc/*', $vendorDir, $libraryDir), GLOB_ONLYDIR); |
||
| 382 | |||
| 383 | // sort the directories descending by their version |
||
| 384 | usort($versions, 'version_compare'); |
||
| 385 | krsort($versions); |
||
| 386 | |||
| 387 | // explode the Magento version |
||
| 388 | $explodedMagentoVersion = explode('.', $magentoVersion); |
||
| 389 | |||
| 390 | // initialize the proposed filename with the default file in the library's root directory |
||
| 391 | $proposedfilename = $filename; |
||
| 392 | |||
| 393 | // iterate over the magento versions, try to find the matching configuration file |
||
| 394 | for ($i = sizeof($explodedMagentoVersion); $i > 0; $i--) { |
||
| 395 | foreach ($versions as $version) { |
||
| 396 | // create a version number |
||
| 397 | $level = implode('.', array_slice($explodedMagentoVersion, 0, $i)); |
||
| 398 | // try to match the version number against the directory |
||
| 399 | if (version_compare($versionBasname = basename($version), $level, '<=')) { |
||
| 400 | // we found the apropriate version directory and stop here |
||
| 401 | $proposedfilename = sprintf('%s/%s', $versionBasname, $filename); |
||
| 402 | break 2; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | // return the default configuration file |
||
| 408 | return sprintf('%s/%s/etc/%s.json', $vendorDir, $libraryDir, $proposedfilename); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Return's the name of the default configuration file. |
||
| 413 | * |
||
| 414 | * @param string $entityTypeCode The entity type code to return the default configuration file for |
||
| 415 | * |
||
| 416 | * @return string The name of the entity type's default configuration file |
||
| 417 | * @throws \Exception |
||
| 418 | */ |
||
| 419 | protected function getDefaultConfigurationFile($entityTypeCode) |
||
| 420 | { |
||
| 421 | |||
| 422 | // load the default configuration file mappings from the configuration |
||
| 423 | $defaultConfigurationFileMappings = $this->getContainer()->getParameter(DependencyInjectionKeys::APPLICATION_DEFAULT_CONFIGURATION_FILE_MAPPINGS); |
||
| 424 | |||
| 425 | // query whether or not a default configuration file for the passed entity type code exists |
||
| 426 | if (isset($defaultConfigurationFileMappings[$entityTypeCode])) { |
||
| 427 | return $defaultConfigurationFileMappings[$entityTypeCode]; |
||
| 428 | } |
||
| 429 | |||
| 430 | // throw an exception, if no default configuration file for the passed entity type is available |
||
| 431 | throw new \Exception( |
||
| 432 | sprintf( |
||
| 433 | 'Can\'t find a default configuration file for entity Type Code \'%s\' (MUST be one of catalog_product, catalog_product_price, catalog_product_inventory, catalog_category or eav_attribute)', |
||
| 434 | $entityTypeCode |
||
| 435 | ) |
||
| 436 | ); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Return's the Magento Edition and entity type's specific default library that contains |
||
| 441 | * the configuration file. |
||
| 442 | * |
||
| 443 | * @param string $magentoEdition The Magento Edition to return the default library for |
||
| 444 | * @param string $entityTypeCode The entity type code to return the default library file for |
||
| 445 | * |
||
| 446 | * @return string The name of the library that contains the default configuration file for the passed Magento Edition and entity type code |
||
| 447 | * @throws \Exception Is thrown, if no default configuration for the passed entity type code is available |
||
| 448 | */ |
||
| 449 | protected function getDefaultConfigurationLibrary($magentoEdition, $entityTypeCode) |
||
| 476 | ) |
||
| 477 | ); |
||
| 478 | } |
||
| 479 | } |
||
| 480 |