Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #23 from flagbit/attribute-blacklist
Browse files Browse the repository at this point in the history
Configure a blacklist of attributes that shouldn't be cloned
  • Loading branch information
hackwell authored Feb 21, 2020
2 parents 11cc727 + 805c203 commit 7249c76
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 15 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ After clicking on **clone** you will see this **dialog**:

Put in a new **product code** and click on **save**. After that check if all the data is correct for the new product.

### Configuration

You don't need to configure this bundle by default.
The default behaviour is to clone all product or product model attributes except the unique attributes.
In addition, you can specify a blacklist of attributes that shouldn't be cloned:

``` yaml
flagbit_product_cloner:
attribute_blacklist:
- your_attribute_code1
- your_attribute_code2
- your_attribute_code3
...
```

## Akeneo Compatibility ##

This extension supports the latest Akeneo PIM CE/EE stable versions:
Expand Down
14 changes: 12 additions & 2 deletions src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ abstract protected function getNormalizer() : NormalizerInterface;

abstract protected function getAttributeRepository() : AttributeRepositoryInterface;

protected function getAttributeCodeBlacklist() : array
{
return [];
}

protected function normalizeProduct(EntityWithFamilyVariantInterface $product)
{
$normalizedProduct = $this->getNormalizer()->normalize($product, 'external_api');
Expand All @@ -25,9 +30,14 @@ protected function normalizeProduct(EntityWithFamilyVariantInterface $product)
unset($normalizedProduct['values'][$value->getAttributeCode()]);
}
$product = $parent;
};
}

$ignoredAttributeCodes = array_merge(
$this->getAttributeRepository()->findUniqueAttributeCodes(),
$this->getAttributeCodeBlacklist()
);

foreach ($this->getAttributeRepository()->findUniqueAttributeCodes() as $attributeCode) {
foreach ($ignoredAttributeCodes as $attributeCode) {
unset($normalizedProduct['values'][$attributeCode]);
}
unset($normalizedProduct['identifier']);
Expand Down
29 changes: 28 additions & 1 deletion src/Controller/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,72 @@ class ProductController extends AbstractController
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var ObjectUpdaterInterface
*/
private $productUpdater;

/**
* @var SaverInterface
*/
private $productSaver;

/**
* @var NormalizerInterface
*/
private $normalizer;

/**
* @var ValidatorInterface
*/
private $validator;

/**
* @var UserContext
*/
private $userContext;

/**
* @var ProductBuilderInterface
*/
private $productBuilder;

/**
* @var AttributeConverterInterface
*/
private $localizedConverter;

/**
* @var FilterInterface
*/
private $emptyValuesFilter;

/**
* @var ConverterInterface
*/
private $productValueConverter;

/**
* @var NormalizerInterface
*/
private $constraintViolationNormalizer;

/**
* @var ProductBuilderInterface
*/
private $variantProductBuilder;

/**
* @var AttributeRepositoryInterface
*/
private $attributeRepository;

/**
* @var string[]
*/
private $attributeCodeBlacklist;

public function __construct(
ProductRepositoryInterface $productRepository,
AttributeRepositoryInterface $attributeRepository,
Expand All @@ -87,7 +104,8 @@ public function __construct(
FilterInterface $emptyValuesFilter,
ConverterInterface $productValueConverter,
NormalizerInterface $constraintViolationNormalizer,
ProductBuilderInterface $variantProductBuilder
ProductBuilderInterface $variantProductBuilder,
array $attributeCodeBlacklist = []
) {

$this->productRepository = $productRepository;
Expand All @@ -103,6 +121,7 @@ public function __construct(
$this->constraintViolationNormalizer = $constraintViolationNormalizer;
$this->variantProductBuilder = $variantProductBuilder;
$this->attributeRepository = $attributeRepository;
$this->attributeCodeBlacklist = $attributeCodeBlacklist;
}

/**
Expand Down Expand Up @@ -216,6 +235,14 @@ private function updateProduct(ProductInterface $product, array $data)
$this->productUpdater->update($product, $data);
}

/**
* @return string[]
*/
protected function getAttributeCodeBlacklist() : array
{
return $this->attributeCodeBlacklist;
}

protected function getNormalizer() : NormalizerInterface
{
return $this->normalizer;
Expand Down
28 changes: 16 additions & 12 deletions src/Controller/ProductModelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,17 @@ class ProductModelController extends AbstractController
* @var NormalizerInterface
*/
private $violationNormalizer;

/**
* @var AttributeRepositoryInterface
*/
private $attributeRepository;

/**
* DefaultController constructor.
*
* @param ProductModelRepositoryInterface $productModelRepository
* @param NormalizerInterface $normalizer
* @param SimpleFactoryInterface $productModelFactory
* @param ObjectUpdaterInterface $productModelUpdater
* @param SaverInterface $productModelSaver
* @param ValidatorInterface $validator
* @param NormalizerInterface $violiationNormalizer
* @param AttributeRepositoryInterface $attributeRepository
* @var string[]
*/
private $attributeCodeBlacklist;

public function __construct(
ProductModelRepositoryInterface $productModelRepository,
AttributeRepositoryInterface $attributeRepository,
Expand All @@ -75,16 +69,18 @@ public function __construct(
ObjectUpdaterInterface $productModelUpdater,
SaverInterface $productModelSaver,
ValidatorInterface $validator,
NormalizerInterface $violiationNormalizer
NormalizerInterface $violationNormalizer,
array $attributeCodeBlacklist = []
) {
$this->productModelRepository = $productModelRepository;
$this->normalizer = $normalizer;
$this->productModelFactory = $productModelFactory;
$this->productModelUpdater = $productModelUpdater;
$this->productModelSaver = $productModelSaver;
$this->validator = $validator;
$this->violationNormalizer = $violiationNormalizer;
$this->violationNormalizer = $violationNormalizer;
$this->attributeRepository = $attributeRepository;
$this->attributeCodeBlacklist = $attributeCodeBlacklist;
}

/**
Expand Down Expand Up @@ -153,6 +149,14 @@ public function cloneAction(Request $request) : JsonResponse
}
}

/**
* @return string[]
*/
protected function getAttributeCodeBlacklist() : array
{
return $this->attributeCodeBlacklist;
}

protected function getNormalizer() : NormalizerInterface
{
return $this->normalizer;
Expand Down
36 changes: 36 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Flagbit\Bundle\ProductClonerBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see
* {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$root = $treeBuilder->root('flagbit_product_cloner');

$root->children()
->arrayNode('attribute_blacklist')
->info('A list of attribute codes that need to be left out for the clone')
->defaultValue([])
->scalarPrototype()
->info('Valid attribute codes')
->end()
->end()
->end();

return $treeBuilder;
}
}
5 changes: 5 additions & 0 deletions src/DependencyInjection/FlagbitProductClonerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class FlagbitProductClonerExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$processedConfig = $this->processConfiguration($configuration, $configs);

$container->setParameter('flagbit_product_cloner.attribute_blacklist', $processedConfig['attribute_blacklist']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
- '@pim_catalog.saver.product_model'
- '@pim_catalog.validator.product'
- '@pim_enrich.normalizer.violation'
- '%flagbit_product_cloner.attribute_blacklist%'
flagbit_product_cloner.controller.product:
class: Flagbit\Bundle\ProductClonerBundle\Controller\ProductController
arguments:
Expand All @@ -26,3 +27,4 @@ services:
- '@pim_enrich.converter.enrich_to_standard.product_value'
- '@pim_enrich.normalizer.product_violation'
- '@pim_catalog.builder.product'
- '%flagbit_product_cloner.attribute_blacklist%'

0 comments on commit 7249c76

Please sign in to comment.