Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 49 | class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface, FileUploadSubjectInterface, UrlKeyAwareSubjectInterface, CleanUpColumnsSubjectInterface |
||
|
|
|||
| 50 | { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The trait that implements the export functionality. |
||
| 54 | * |
||
| 55 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
| 56 | */ |
||
| 57 | use ExportableTrait; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The trait that provides file upload functionality. |
||
| 61 | * |
||
| 62 | * @var \TechDivision\Import\Subjects\FileUploadTrait |
||
| 63 | */ |
||
| 64 | use FileUploadTrait; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The array with the pre-loaded entity IDs. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $preLoadedEntityIds = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Mappings for the table column => CSV column header. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $headerStockMappings = array( |
||
| 79 | 'qty' => array('qty', 'float'), |
||
| 80 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
| 81 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
| 82 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
| 83 | 'backorders' => array('allow_backorders', 'int'), |
||
| 84 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
| 85 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
| 86 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
| 87 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
| 88 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
| 89 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
| 90 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
| 91 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
| 92 | 'manage_stock' => array('manage_stock', 'int'), |
||
| 93 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
| 94 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
| 95 | 'qty_increments' => array('qty_increments', 'float'), |
||
| 96 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
| 97 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
| 98 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
| 99 | ); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The array with the available visibility keys. |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected $availableVisibilities = array( |
||
| 107 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
| 108 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
| 109 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
| 110 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
| 111 | ); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The default callback mappings for the Magento standard product attributes. |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | protected $defaultCallbackMappings = array( |
||
| 119 | 'visibility' => array('import_product.callback.visibility'), |
||
| 120 | 'tax_class_id' => array('import_product.callback.tax.class'), |
||
| 121 | 'bundle_price_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 122 | 'bundle_sku_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 123 | 'bundle_weight_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 124 | 'bundle_price_view' => array('import_product_bundle.callback.bundle.price.view'), |
||
| 125 | 'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type') |
||
| 126 | ); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The available entity types. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $entityTypes = array(); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The media roles loader instance. |
||
| 137 | * |
||
| 138 | * @var \TechDivision\Import\Loaders\LoaderInterface |
||
| 139 | */ |
||
| 140 | protected $mediaRolesLoader; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The entity type code mapper instance. |
||
| 144 | * |
||
| 145 | * @var \TechDivision\Import\Utils\Mappings\MapperInterface |
||
| 146 | */ |
||
| 147 | protected $entityTypeCodeMapper; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * BunchSubject constructor |
||
| 151 | * |
||
| 152 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 153 | * @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data |
||
| 154 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system loggers instances |
||
| 155 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
| 156 | * @param \TechDivision\Import\Loaders\LoaderInterface $mediaRolesLoader The media type loader instance |
||
| 157 | * @param \TechDivision\Import\Utils\Mappings\MapperInterface $entityTypeCodeMapper The entity type code mapper instance |
||
| 158 | */ |
||
| 159 | 18 | public function __construct( |
|
| 160 | RegistryProcessorInterface $registryProcessor, |
||
| 161 | GeneratorInterface $coreConfigDataUidGenerator, |
||
| 162 | Collection $systemLoggers, |
||
| 163 | EmitterInterface $emitter, |
||
| 164 | LoaderInterface $mediaRolesLoader, |
||
| 165 | MapperInterface $entityTypeCodeMapper |
||
| 166 | ) { |
||
| 167 | |||
| 168 | // set the loader for the media roles and the entity type code mapper |
||
| 169 | 18 | $this->mediaRolesLoader = $mediaRolesLoader; |
|
| 170 | 18 | $this->entityTypeCodeMapper = $entityTypeCodeMapper; |
|
| 171 | |||
| 172 | // pass the other instances to the parent constructor |
||
| 173 | 18 | parent::__construct($registryProcessor, $coreConfigDataUidGenerator, $systemLoggers, $emitter); |
|
| 174 | 18 | } |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 178 | * |
||
| 179 | * @param string $serial The serial of the actual import |
||
| 180 | * |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | 18 | public function setUp($serial) |
|
| 184 | { |
||
| 185 | |||
| 186 | // load the status of the actual import |
||
| 187 | 18 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
|
| 188 | |||
| 189 | // load the global data we've prepared initially |
||
| 190 | 18 | $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES]; |
|
| 191 | |||
| 192 | // initialize the flag whether to copy images or not |
||
| 193 | 18 | if ($this->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) { |
|
| 194 | $this->setCopyImages($this->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES)); |
||
| 195 | } |
||
| 196 | |||
| 197 | // initialize the flag whether to override images or not |
||
| 198 | 18 | if ($this->getConfiguration()->hasParam(ConfigurationKeys::OVERRIDE_IMAGES)) { |
|
| 199 | $this->setOverrideImages($this->getConfiguration()->getParam(ConfigurationKeys::OVERRIDE_IMAGES)); |
||
| 200 | } |
||
| 201 | |||
| 202 | // initialize media directory => can be absolute or relative |
||
| 203 | 18 | View Code Duplication | if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) { |
| 204 | try { |
||
| 205 | $this->setMediaDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY))); |
||
| 206 | } catch (\InvalidArgumentException $iae) { |
||
| 207 | $this->getSystemLogger()->debug($iae->getMessage()); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | // initialize images directory => can be absolute or relative |
||
| 212 | 18 | View Code Duplication | if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) { |
| 213 | try { |
||
| 214 | $this->setImagesFileDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY))); |
||
| 215 | } catch (\InvalidArgumentException $iae) { |
||
| 216 | $this->getSystemLogger()->debug($iae->getMessage()); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | // invoke the parent method |
||
| 221 | 18 | parent::setUp($serial); |
|
| 222 | 18 | } |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Clean up the global data after importing the bunch. |
||
| 226 | * |
||
| 227 | * @param string $serial The serial of the actual import |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public function tearDown($serial) |
||
| 232 | { |
||
| 233 | |||
| 234 | // invoke the parent method |
||
| 235 | parent::tearDown($serial); |
||
| 236 | |||
| 237 | // load the registry processor |
||
| 238 | $registryProcessor = $this->getRegistryProcessor(); |
||
| 239 | |||
| 240 | // update the status |
||
| 241 | $registryProcessor->mergeAttributesRecursive( |
||
| 242 | RegistryKeys::STATUS, |
||
| 243 | array( |
||
| 244 | RegistryKeys::PRE_LOADED_ENTITY_IDS => $this->preLoadedEntityIds, |
||
| 245 | ) |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Return's the default callback mappings. |
||
| 251 | * |
||
| 252 | * @return array The default callback mappings |
||
| 253 | */ |
||
| 254 | public function getDefaultCallbackMappings() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return's the mappings for the table column => CSV column header. |
||
| 261 | * |
||
| 262 | * @return array The header stock mappings |
||
| 263 | */ |
||
| 264 | 1 | public function getHeaderStockMappings() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Return's the visibility key for the passed visibility string. |
||
| 271 | * |
||
| 272 | * @param string $visibility The visibility string to return the key for |
||
| 273 | * |
||
| 274 | * @return integer The requested visibility key |
||
| 275 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
| 276 | */ |
||
| 277 | public function getVisibilityIdByValue($visibility) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Pre-load the entity ID for the passed product. |
||
| 296 | * |
||
| 297 | * @param array $product The product to be pre-loaded |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | public function preLoadEntityId(array $product) |
||
| 302 | { |
||
| 303 | $this->preLoadedEntityIds[$product[MemberNames::SKU]] = $product[MemberNames::ENTITY_ID]; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Return's the entity type for the configured entity type code. |
||
| 308 | * |
||
| 309 | * @return array The requested entity type |
||
| 310 | * @throws \Exception Is thrown, if the requested entity type is not available |
||
| 311 | */ |
||
| 312 | 1 | View Code Duplication | public function getEntityType() |
| 327 | |||
| 328 | /** |
||
| 329 | * Loads and returns the media roles. |
||
| 330 | * |
||
| 331 | * @return array The array with the media roles |
||
| 332 | */ |
||
| 333 | public function getMediaRoles(): array |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return's the entity type code to be used. |
||
| 340 | * |
||
| 341 | * @return string The entity type code to be used |
||
| 342 | */ |
||
| 343 | 18 | public function getEntityTypeCode() |
|
| 347 | } |
||
| 348 |