diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4cf61cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +.zsh_config +/.idea/ diff --git a/Component/Catalog/Updater/Setter/UnrestrictedCreateOptionValue.php b/Component/Catalog/Updater/Setter/UnrestrictedCreateOptionValue.php index 65336d8..6f48246 100755 --- a/Component/Catalog/Updater/Setter/UnrestrictedCreateOptionValue.php +++ b/Component/Catalog/Updater/Setter/UnrestrictedCreateOptionValue.php @@ -2,55 +2,38 @@ namespace Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter; -use Akeneo\Bundle\StorageUtilsBundle\Doctrine\Common\Saver\BaseSaver; -use Akeneo\Component\StorageUtils\Factory\SimpleFactoryInterface; -use Pim\Component\Catalog\Updater\AttributeOptionUpdater; -use Akeneo\Bundle\StorageUtilsBundle\Doctrine\Common\Detacher\ObjectDetacher; +use Akeneo\Pim\Structure\Component\Updater\AttributeOptionUpdater; +use Akeneo\Tool\Bundle\StorageUtilsBundle\Doctrine\Common\Detacher\ObjectDetacher; +use Akeneo\Tool\Bundle\StorageUtilsBundle\Doctrine\Common\Saver\BaseSaver; +use Akeneo\Tool\Component\StorageUtils\Factory\SimpleFactory; class UnrestrictedCreateOptionValue { - /** @var SimpleFactoryInterface */ - protected $optionValueFactory; - - /** - * @var ObjectDetacher - */ - protected $objectDetacher; - - /** - * UnrestrictedCreateOptionValue constructor. - * @param SimpleFactoryInterface $optionValueFactory - * @param AttributeOptionUpdater $attributeOptionUpdater - * @param BaseSaver $baseSaver - */ public function __construct( - SimpleFactoryInterface $optionValueFactory, - AttributeOptionUpdater $attributeOptionUpdater, - BaseSaver $baseSaver, - ObjectDetacher $objectDetacher - ) - { - $this->optionValueFactory = $optionValueFactory; - $this->attributeOptionUpdater = $attributeOptionUpdater; - $this->baseSaver = $baseSaver; - $this->objectDetacher = $objectDetacher; + private SimpleFactory $optionValueFactory, + private AttributeOptionUpdater $attributeOptionUpdater, + private BaseSaver $baseSaver, + private ObjectDetacher $objectDetacher + ) { + } - /** - * @param $codeAttribute - * @param $code - */ - public function createOptionValue($codeAttribute, $code) { + public function createOptionValue($codeAttribute, $code): void + { $attributeOptionValue = $this->optionValueFactory->create(); + //todo: make the label locales dynamic by detecting the enabled locales + //todo: make the sort_order dynamic by getting the highest sort_order available for this attribute $tab = [ 'attribute' => $codeAttribute, 'code' => $code, 'sort_order' => 2, 'labels' => [ - 'de_DE' => $code, + 'de_CH' => $code, + 'fr_CH' => $code, + 'it_CH' => $code, + 'en_GB' => $code, 'en_US' => $code, - 'fr_FR' => $code, ] ]; @@ -59,4 +42,4 @@ public function createOptionValue($codeAttribute, $code) { $this->objectDetacher->detach($attributeOptionValue); } -} \ No newline at end of file +} diff --git a/Component/Catalog/Updater/Setter/UnrestrictedMultiSelectAttributeSetter.php b/Component/Catalog/Updater/Setter/UnrestrictedMultiSelectAttributeSetter.php index 0d15670..9752c12 100755 --- a/Component/Catalog/Updater/Setter/UnrestrictedMultiSelectAttributeSetter.php +++ b/Component/Catalog/Updater/Setter/UnrestrictedMultiSelectAttributeSetter.php @@ -2,56 +2,29 @@ namespace Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter; -use Akeneo\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface; -use Pim\Component\Catalog\Builder\EntityWithValuesBuilderInterface; -use Pim\Component\Catalog\Model\AttributeInterface; -use Pim\Component\Catalog\Model\EntityWithValuesInterface; -use Pim\Component\Catalog\Repository\AttributeOptionRepositoryInterface; -use Pim\Component\Catalog\Updater\Setter\AttributeSetter as BaseMultiSelectAttributeSetter; +use Akeneo\Pim\Enrichment\Component\Product\Model\EntityWithValuesInterface; +use Akeneo\Pim\Structure\Bundle\Doctrine\ORM\Repository\AttributeOptionRepository; +use Akeneo\Pim\Enrichment\Component\Product\Updater\Setter\AttributeSetter as BaseMultiSelectAttributeSetter; +use Akeneo\Pim\Structure\Component\Model\AttributeInterface; class UnrestrictedMultiSelectAttributeSetter extends BaseMultiSelectAttributeSetter { - /** @var AttributeOptionRepositoryInterface */ - protected $attrOptionRepository; - - /** @var UnrestrictedCreateOptionValue */ - protected $unrestrictedCreateOptionValue; - - /** - * UnrestrictedMultiSelectAttributeSetter constructor. - * @param EntityWithValuesBuilderInterface $entityWithValuesBuilder - * @param $supportedTypes - * @param AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface - * @param \Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter\UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue - */ public function __construct( - EntityWithValuesBuilderInterface $entityWithValuesBuilder, - $supportedTypes, - AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface, - UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue - ) - { - $this->attrOptionRepository = $identifiableObjectRepositoryInterface; - $this->unrestrictedCreateOptionValue = $unrestrictedCreateOptionValue; + protected $entityWithValuesBuilder, + protected $supportedTypes, + private AttributeOptionRepository $attrOptionRepository, + private UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue + ) { parent::__construct($entityWithValuesBuilder, $supportedTypes); } - - /** - * @param EntityWithValuesInterface $entityWithValues - * @param AttributeInterface $attribute - * @param mixed $data - * @param array $options - */ public function setAttributeData( EntityWithValuesInterface $entityWithValues, - AttributeInterface $attribute, - $data, - array $options = [] - ) { - if (null === $data) { - $option = null; - } else { + AttributeInterface $attribute, + $data, + array $options = [] + ): void { + if ($data !== null) { $data = preg_replace('/[^a-zA-Z0-9\']/', '_', $data); $this->checkOption($attribute, $data); } @@ -59,13 +32,9 @@ public function setAttributeData( parent::setAttributeData($entityWithValues, $attribute, $data, $options); } - /** - * @param AttributeInterface $attribute - * @param $datas - */ - protected function checkOption(AttributeInterface $attribute, $datas) + protected function checkOption(AttributeInterface $attribute, $datas): void { - if (null === $datas) { + if ($datas === null) { return; } @@ -78,4 +47,4 @@ protected function checkOption(AttributeInterface $attribute, $datas) } } } -} \ No newline at end of file +} diff --git a/Component/Catalog/Updater/Setter/UnrestrictedSimpleSelectAttributeSetter.php b/Component/Catalog/Updater/Setter/UnrestrictedSimpleSelectAttributeSetter.php index b20a2ea..02e3248 100755 --- a/Component/Catalog/Updater/Setter/UnrestrictedSimpleSelectAttributeSetter.php +++ b/Component/Catalog/Updater/Setter/UnrestrictedSimpleSelectAttributeSetter.php @@ -2,59 +2,29 @@ namespace Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter; -use Akeneo\Component\StorageUtils\Detacher\ObjectDetacherInterface; -use Pim\Component\Catalog\Builder\EntityWithValuesBuilderInterface; -use Pim\Component\Catalog\Model\AttributeInterface; -use Pim\Component\Catalog\Model\EntityWithValuesInterface; -use Pim\Component\Catalog\Repository\AttributeOptionRepositoryInterface; -use Pim\Component\Catalog\Updater\Setter\AttributeSetter as BaseSimpleSelectAttributeSetter; +use Akeneo\Pim\Enrichment\Component\Product\Model\EntityWithValuesInterface; +use Akeneo\Pim\Structure\Bundle\Doctrine\ORM\Repository\AttributeOptionRepository; +use Akeneo\Pim\Enrichment\Component\Product\Updater\Setter\AttributeSetter as BaseSimpleSelectAttributeSetter; +use Akeneo\Pim\Structure\Component\Model\AttributeInterface; class UnrestrictedSimpleSelectAttributeSetter extends BaseSimpleSelectAttributeSetter { - /** @var \Doctrine\ORM\EntityRepository */ - protected $attrOptionRepository; - - /** @var UnrestrictedCreateOptionValue */ - protected $unrestrictedCreateOptionValue; - - /** @var ObjectDetacherInterface */ - protected $objectDetacher; - - /** - * UnrestrictedSimpleSelectAttributeSetter constructor. - * @param EntityWithValuesBuilderInterface $entityWithValuesBuilder - * @param $supportedTypes - * @param AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface - * @param \Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter\UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue - */ public function __construct( - EntityWithValuesBuilderInterface $entityWithValuesBuilder, - $supportedTypes, - AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface, - UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue - ) - { - $this->attrOptionRepository = $identifiableObjectRepositoryInterface; - $this->unrestrictedCreateOptionValue = $unrestrictedCreateOptionValue; + protected $entityWithValuesBuilder, + protected $supportedTypes, + private AttributeOptionRepository $attrOptionRepository, + private UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue + ) { parent::__construct($entityWithValuesBuilder, $supportedTypes); } - - /** - * @param EntityWithValuesInterface $entityWithValues - * @param AttributeInterface $attribute - * @param mixed $data - * @param array $options - */ public function setAttributeData( EntityWithValuesInterface $entityWithValues, - AttributeInterface $attribute, - $data, - array $options = [] - ) { - if (null === $data) { - $option = null; - } else { + AttributeInterface $attribute, + $data, + array $options = [] + ): void { + if ($data !== null) { $data = preg_replace('/[^a-zA-Z0-9\']/', '_', $data); $identifier = $attribute->getCode() . '.' . $data; @@ -67,4 +37,4 @@ public function setAttributeData( parent::setAttributeData($entityWithValues, $attribute, $data, $options); } -} \ No newline at end of file +} diff --git a/DependencyInjection/AutoAttributeOptionsSetterExtension.php b/DependencyInjection/AutoAttributeOptionsSetterExtension.php index e7477ab..efa0ce4 100755 --- a/DependencyInjection/AutoAttributeOptionsSetterExtension.php +++ b/DependencyInjection/AutoAttributeOptionsSetterExtension.php @@ -7,11 +7,6 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader; -/** - * This is the class that loads and manages your bundle configuration - * - * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} - */ class AutoAttributeOptionsSetterExtension extends Extension { /** diff --git a/Doctrine/ORM/Repository/AttributeOptionRepository.php b/Doctrine/ORM/Repository/AttributeOptionRepository.php index 2e4f253..ed7e332 100644 --- a/Doctrine/ORM/Repository/AttributeOptionRepository.php +++ b/Doctrine/ORM/Repository/AttributeOptionRepository.php @@ -2,7 +2,9 @@ namespace Niji\AutoAttributeOptionsSetterBundle\Doctrine\ORM\Repository; -use Pim\Bundle\CatalogBundle\Doctrine\ORM\Repository\AttributeOptionRepository as BaseAttributeOptionRepository; +use Akeneo\Pim\Structure\Bundle\Doctrine\ORM\Repository\AttributeOptionRepository as BaseAttributeOptionRepository; +use Doctrine\ORM\NonUniqueResultException; +use Doctrine\ORM\NoResultException; class AttributeOptionRepository extends BaseAttributeOptionRepository { @@ -15,14 +17,15 @@ class AttributeOptionRepository extends BaseAttributeOptionRepository * @return bool * TRUE if the option exists, FALSE otherwise. * - * @throws \Doctrine\ORM\NoResultException + * @throws NoResultException * In case of no results found. - * @throws \Doctrine\ORM\NonUniqueResultException - * In case of non unique result found. + * @throws NonUniqueResultException + * In case of non-unique result found. */ - public function optionExists($code) { + public function optionExists($code): bool + { if (null === $code) { - return FALSE; + return false; } list($attributeCode, $optionCode) = explode('.', $code); @@ -39,4 +42,4 @@ public function optionExists($code) { return (isset($count) && intval($count) > 0); } -} \ No newline at end of file +} diff --git a/EventListener/ClassMetadataListener.php b/EventListener/ClassMetadataListener.php index 7d0d0d1..2d5beb8 100644 --- a/EventListener/ClassMetadataListener.php +++ b/EventListener/ClassMetadataListener.php @@ -18,8 +18,9 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) $classMetadata = $eventArgs->getClassMetadata(); $entityName = $classMetadata->getName(); - if ('Pim\Bundle\CatalogBundle\Entity\AttributeOption' === $entityName) { + //if ($entityName === 'Pim\Bundle\CatalogBundle\Entity\AttributeOption') { + if ($entityName === 'Akeneo\Pim\Structure\Component\Model\AttributeOption') { $classMetadata->customRepositoryClassName = 'Niji\AutoAttributeOptionsSetterBundle\Doctrine\ORM\Repository\AttributeOptionRepository'; } } -} \ No newline at end of file +} diff --git a/README.md b/README.md index a3ecab1..e5e55a9 100644 --- a/README.md +++ b/README.md @@ -8,19 +8,12 @@ This bundle ensure that attribute options are created automatically when importi `composer require niji/auto-attribute-options-setter-bundle` -- In your app/AppKernel.php add a line to enable the bundle: +- In your bundles.php add a line to enable the bundle: ```php -public function registerProjectBundles() { - return [ - // your app bundles should be registered here, - .../... - new Niji\AutoAttributeOptionsSetterBundle\AutoAttributeOptionsSetterBundle(), - .../... - ]; -} +Niji\AutoAttributeOptionsSetterBundle\AutoAttributeOptionsSetterBundle::class => ['all' => true] ``` ## Credits -This bundle is brought to you by Niji - http://www.niji.fr. \ No newline at end of file +This bundle is brought to you by Niji - http://www.niji.fr And Diglin GmbH - https://www.diglin.com diff --git a/Resources/config/services.yml b/Resources/config/services.yml index c9ca99b..608404d 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -4,7 +4,7 @@ parameters: services: pim_catalog.repository.attribute_option: class: '%pim_catalog.repository.attribute_option.class%' - factory: 'doctrine.orm.entity_manager:getRepository' + factory: ['@doctrine.orm.entity_manager', 'getRepository'] arguments: ['%pim_catalog.entity.attribute_option.class%'] tags: - { name: 'pim_repository' } @@ -12,4 +12,4 @@ services: unrestricted_select_attribute_catalog.listener.class_metadata: class: Niji\AutoAttributeOptionsSetterBundle\EventListener\ClassMetadataListener tags: - - { name: doctrine.event_listener, event: loadClassMetadata } \ No newline at end of file + - { name: doctrine.event_listener, event: loadClassMetadata } diff --git a/composer.json b/composer.json index 4c5753b..2f1b5d3 100644 --- a/composer.json +++ b/composer.json @@ -1,51 +1,46 @@ - { - "name": "niji/auto-attribute-options-setter-bundle", - "type": "symfony-bundle", - "description": "Symfony AutoAttributeOptionsSetterBundle", - "keywords": [ - "Akeneo Attribute Options Creator" - ], - "license": "MIT", - "authors": [ - { - "name": "Aymeric Simon", - "email": "aymeric.simon@niji.fr" + "name": "niji/auto-attribute-options-setter-bundle", + "type": "symfony-bundle", + "description": "Symfony AutoAttributeOptionsSetterBundle", + "keywords": [ + "Akeneo Attribute Options Creator" + ], + "license": "MIT", + "authors": [ + { + "name": "Aymeric Simon", + "email": "aymeric.simon@niji.fr" + }, + { + "name": "Bertrand Presles", + "email": "bertrand.presles@niji.fr" + }, + { + "name": "Benjamin Groleau", + "email": "benjamin.groleau@niji.fr" + }, + { + "name": "Diglin GmbH", + "email": "support@diglin.com" + } + ], + "require": { + "akeneo/pim-community-dev": "^7.0" }, - { - "name": "Bertrand Presles", - "email": "bertrand.presles@niji.fr" + "config": { + "sort-packages": true }, - { - "name": "Benjamin Groleau", - "email": "benjamin.groleau@niji.fr" - } - ], - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/akeneo/pim-community-dev.git", - "branch": "master" - } - ], - "require": { - "php": "^7.0", - "akeneo/pim-community-dev": "^2.3" - }, - "config": { - "sort-packages": true - }, - "autoload": { - "psr-4": { - "Niji\\AutoAttributeOptionsSetterBundle\\": "" + "autoload": { + "psr-4": { + "Niji\\AutoAttributeOptionsSetterBundle\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "autoload-dev": { - "psr-4": { - "Niji\\AutoAttributeOptionsSetterBundle\\Tests\\": "/Tests/" + "autoload-dev": { + "psr-4": { + "Niji\\AutoAttributeOptionsSetterBundle\\Tests\\": "/Tests/" + } } - } -} \ No newline at end of file +}